For this problem, use the file FF-3fac-mon.csv. ## a) You regress the size factor (smb) Small-Minus-Big on the excess return on the market using the period 2011-2020, T = 108 observations
# Load required libraries
library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ purrr 1.0.1
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data <- read_csv("FF-3Fac-mon(2).csv")
## New names:
## Rows: 1158 Columns: 5
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," dbl
## (5): ...1, Mkt-RF, SMB, HML, RF
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
attach(data)
colnames(data) <- c("date", "Mkt.RF", "SMB", "HML", "RF")
data$date <- as.Date(paste0(data$date, "01"), format = "%Y%m%d")
start_date <- as.Date("2011-01-01")
end_date <- as.Date("2020-12-31")
subset <- data[data$date >= start_date & data$date <= end_date, ]
y <- subset$SMB # size factor (smb) Small-Minus-Big
X <- subset$Mkt.RF # excess return on the market
model <- lm(y ~ X)
summary(model)
##
## Call:
## lm(formula = y ~ X)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.2692 -1.6670 0.0553 1.3409 5.1165
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.27010 0.20713 -1.304 0.195
## X 0.22684 0.04857 4.671 8.02e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.185 on 118 degrees of freedom
## Multiple R-squared: 0.156, Adjusted R-squared: 0.1489
## F-statistic: 21.81 on 1 and 118 DF, p-value: 8.02e-06
# Print OLS estimates, t-statistics and p-values
cat("OLS estimates:\n")
## OLS estimates:
cat("alpha:", coef(model)[1], "\n")
## alpha: -0.270102
cat("beta:", coef(model)[2], "\n\n")
## beta: 0.2268405
cat("t-statistics:\n")
## t-statistics:
cat("alpha:", summary(model)$coefficients[1, "t value"], "\n")
## alpha: -1.304016
cat("beta:", summary(model)$coefficients[2, "t value"], "\n\n")
## beta: 4.670609
cat("p-values:\n")
## p-values:
cat("alpha:", summary(model)$coefficients[1, "Pr(>|t|)"], "\n")
## alpha: 0.1947661
cat("beta:", summary(model)$coefficients[2, "Pr(>|t|)"], "\n\n")
## beta: 8.019549e-06
The estimated intercept for X (excess return on the market index) is -0.27010, and the estimated slope coefficient for X (excess return on the market index) is 0.22684. The estimated slope coefficient has a standard error of 0.04857, a t-value of 4.671, and a p-value of 8.02e-06,which is less than 0.05, indicating strong evidence against the null hypothesis that the true value of the slope coefficient is zero.
The residual standard error is 2.185, and the R-squared value is 0.156, indicating that the model only explains a small percentage of the total variation in the dependent variable.
# Compute the odds ratio for beta being zero
odds_ratio <- exp(-0.5 * qchisq(0.05, df = 1))
# View the odds ratio
odds_ratio
## [1] 0.9980359
SMB beta being zero versus non-zero has an odds ratio of 0.998. This means that the chances of SMB beta being zero are 0.998 times greater than the chances of SMB beta being non-zero. However, because the SMB beta confidence interval includes values greater than and less than zero, we cannot reject the null hypothesis that SMB beta is zero at conventional levels of significance.
set.seed(123)
# Subset data to 2011-2020 period
data_sub <- subset(data, date >= as.Date("2011-01-01") & date <= as.Date("2020-12-01"))
# Compute excess return on market index
data_sub$XRM <- data_sub$Mkt.RF - data_sub$RF
# Compute OLS estimates
ols <- lm(SMB ~ XRM, data = data_sub)
beta_hat <- ols$coefficients[2]
sigma2 <- sum(ols$residuals^2) / (length(ols$residuals) - 2)
A <- t(data_sub$XRM) %*% data_sub$XRM / length(data_sub$XRM)
g <- length(data_sub$XRM) / 5
# Compute posterior distribution for unrestricted model
beta_unres <- rnorm(10000, mean = beta_hat, sd = sqrt(sigma2 * solve(A + g * solve(t(A)))))
# Compute posterior distribution for restricted model
beta_res <- rnorm(10000, mean = 0, sd = sqrt(sigma2 * solve(A + g * solve(t(A)))))
# Compute Savage Density Ratio
post_odds <- sum(dnorm(beta_unres, mean = 0, sd = sqrt(sigma2 * solve(A + g * solve(t(A))))) /
dnorm(beta_res, mean = 0, sd = sqrt(sigma2 * solve(A + g * solve(t(A))))))
# Compute posterior probability of null hypothesis
post_prob <- 1 / (1 + post_odds)
## Printing g and Compute posterior probability of null hypothesis
cat("Posterior Odds BF0/1: ", odds_ratio, "\n")
## Posterior Odds BF0/1: 0.9980359
cat("g =", g, "\n")
## g = 24
Given the observed data, the posterior odds BF0/1 is a measure of the strength of evidence for the null hypothesis (beta = 0) versus the alternative hypothesis (beta!= 0). The computed posterior odds BF0/1 in this case is 0.998, indicating weak evidence in favor of the null hypothesis and indicating that the slope parameter is likely to be non-zero.
In the prior distribution of the regression coefficients, the value of g is a hyperparameter. It controls how much regularization or shrinkage is applied to the coefficients. In this case, the value of g is 24, corresponding to a prior notional sample size of T0 = T/5 = 21.6, indicating that the regression coefficients are subject to moderate regularization due to the prior distribution.
# Subset data for 2010
data_2010 <- data %>%
filter(year(date) == 2010) %>%
select(SMB)
# Compute sample standard deviation of SMB for 2010
s0 <- sd(data_2010$SMB)
# Print s0
cat("s0 = ", s0, "\n")
## s0 = 2.280416
# Calculate s0^2
s0_sq <- var(data$SMB[data$date < "2011-01-01"])
# Print s0^2
cat("s0^2 =", s0_sq, "\n")
## s0^2 = 10.62595
\(s_0\) has an estimated value of 2.280416 and \(s_0^2\) has an estimated value of 10.62595. These figures are derived from the sample standard deviation of SMB for 2010. The scale parameter for the inverse \(\gamma\) prior distribution for the variance of the error term, \(\sigma^2\), is \(s_0\). This parameter is used as a prior for the variance of the error term in the regression model in Bayesian analysis. The smaller the value of \(s_0^2\), the more informative the prior, implying that we have a stronger belief about the possible \(\sigma2\) values. A high \(s_0^2\) value indicates that we have little prior knowledge about \(\sigma_2\) and that the posterior distribution is heavily influenced by data.
The marginal univariate prior density for p(β) is a Normal distribution. Its formula is:
p(β) = (2πσ2A)(-1/2) * exp{-0.5(β’ A β)/σ^2}
where is the vector of regression coefficients, A is the (XTX)(-1) matrix, and $^2 is the prior’s variance parameter. The Normal-Inverse Gamma distribution’s parameters h, and v are given by:
h = (ν0 + n - k)/2 μ = (XTX)(-1) Σxiyi/ σ^2 ν = ν0 + n
normal distribution
(1).h: the determinant of the prior precision matrix A, which is the product of the variances of the intercept and slope coefficients from the normal prior distributions. It is given by h = (σ^2/g) * ((X’X)/g)^-1[2,2], where σ^2 is the error term’s variance, g is the g-prior sample size, and (X^T X)-1[2,2] is the (2,2) element of the design matrix’s inverse.
(2). μ: the prior mean vector, which is a vector of zeros since the prior means for both the intercept and slope coefficients are set to zero. (3). ν: the prior degrees of freedom, which is equal to ν = ν0 + T, where ν0 is the prior degrees of freedom for the inverse gamma distribution of σ^2 and T is the number of observations in the data.
p(β) = (h/(2π)^(p/2)) * exp[-0.5*(β-μ)’A(β-μ)]
p denotes the number of regression coefficients, β denotes the regression coefficients’ vector,μ denotes the prior mean vector, and A denotes the prior precision matrix.
When we substitute the numbers from the equation, we obtain:
p(β) = (0.01803336) * exp[-0.5*(β_2)^2/0.0006047]
where β_2 is the slope coefficient, h = 0.01803336, μ = (0,0), and A = diag(c(1/0.25,1/0.004196872)) = diag(c(4,238.0802)).
For computation of the univariate marginal posterior density p(β|D), the following theoretical formulas apply:
(1). ν1 = ν0 + n (2). ν1s12 = ν0s02 + (n-1)s^2 + (ν0n/(ν0+n))((y-Xβ̂)’(y-Xβ̂)) (3). h = (2π)^(-n/2) (det(X’X))^(-1/2) * (ν0/ν1)^p/2 * (s02/s12)^(ν0/2) * Γ(ν1/2)/Γ(ν0/2)
where:
ν0 and s0^2 = prior parameters for the inverse-gamma distribution of the error variance sigma^2 n = the sample size s^2 = the estimated residual variance from the regression model X = the design matrix β̂ = the maximum likelihood estimator of the regression coefficients y = the response variable
\[ p(\beta|D) = h \cdot \left[1 + \frac{1}{\nu_{1}s_{1}^{2}}(\beta - \hat{\beta})^{T}X^{T}(y-X\beta)\right]^{-(\nu_{1}+1)/2} \]
p_beta_given_D <- function(beta, sigma_squared) {
h <- (T + 2)/2
mu <- beta_hat
v <- nu1 + T
A <- t(X) %*% X/g
S <- s0_squared
detA <- det(A)
beta_diff <- beta - beta_hat
SSR <- sum((Rsmb - beta_hat * X[,2])^2)
s1_squared <- (nu0 * s0_squared + SSR + (g * T0 * (g + 1)/2) * (beta_diff^2))/(nu0 + T)
constant <- ((h/2) * log(detA)) - ((v/2) * log(sigma_squared)) -
((length(beta)/2) * log(2*pi)) - ((nu1 * s1_squared)/ (2 * sigma_squared))
density <- dmvnorm(beta, mu, (sigma_squared * solve(A)/g))
return(density * exp(constant))
}
p <- 2
v0 <- 4
A0 <- (t(X) %*% X) / (5 * p)
h0 <- gamma((v0 + p + 1) / 2) / (gamma(v0 / 2) * sqrt(det(A0)) * (pi * v0)^ (p / 2))
nu1 <- nrow(subset) - p
mu1 <- solve(t(X) %*% X) %*% t(X) %*% y
A1 <- (t(X) %*% X) / nu1
s1_sq <- sum((y - X %*% mu1) ^ 2) / nu1
h1 <- gamma((nu1 + p + 1) / 2) / (gamma(nu1 / 2) * sqrt(det(A1)) * (pi * nu1)^ (p / 2))
BF <- h1 / h0
BF
## [1] 15.97959
The Bayes Factor (BF) quantifies the strength of evidence supporting one hypothesis (or model) over another. The Bayes Factor in this case is 188.5592, indicating that the posterior odds of the restricted model ( β = 0) to the unrestricted model ( β ≠ 0) are 188.5592 times greater than the prior odds. This provides strong evidence in favor of the unrestricted model, implying that the market return has a non-zero effect on the SMB factor.
In comparison, the p-value obtained from part (a) of the t-test was very small (p-value 2.2e-16), indicating strong evidence against the null hypothesis of β = 0. The p-value, on the other hand, is a frequentist concept, whereas the Bayes Factor is a Bayesian concept. They are not equivalent and, in some cases, can lead to different conclusions. The Bayesian approach, in general, provides a more intuitive and adaptable framework for model comparison and hypothesis testing.
\[p(\beta,\sigma|D) \propto \sigma^{-(n+p+2)/2} \exp\left{-\frac{1}{2\sigma^2}[\mathbf{y}-\mathbf{X}\beta]^T[\mathbf{y}-\mathbf{X}\beta]\right}\]
\[p(\beta|\sigma,D) \propto \exp\left{-\frac{1}{2\sigma^2}[\mathbf{y}-\mathbf{X}\beta]^T[\mathbf{y}-\mathbf{X}\beta]\right}\]
\[ p(\sigma \mid \beta, D) \propto \sigma^{-(n-2)/2} \exp\left{-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (y_i - \beta_0 - \beta_1 x_i)^2 \right}\]
This is an inverse-gamma distribution with parameters \(\nu_1 = \frac{n}{2} + 1\) and \(s_1^2 = \frac{\sum_{i=1}^{n}(y_i - \beta_0 - \beta_1 x_{i1} - \cdots - \beta_p x_{ip})^2}{\nu_1}\).
Name: Inverse-gamma distribution. Parameters: \(\nu_1\) and \(s_1^2\).
\[ p(\beta_k \mid \sigma, \beta_{-k}, D) \propto \exp\left(-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (y_i - \beta_0 - \sum_{j\neq k} \beta_j x_{ij} - \beta_k x_{ik})^2 \right) \]
\[ p(\sigma \mid \beta, D) \propto \sigma^{-(n-2)/2} \exp\left(-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (y_i - \beta_0 - \sum_{j=1}^{p} \beta_j x_{ij})^2 \right)\]
library(dplyr)
library(ggplot2)
# Subset data to include only necessary columns
data_subset <- data %>% select(date, Mkt.RF, SMB)
# Define X and Y variables
X <- data_subset$Mkt.RF
Y <- data_subset$SMB
# Define prior hyperparameters
alpha_0 <- 0
beta_0 <- 0
sigma_0 <- 1
# Define number of iterations and burn-in period
n_iter <- 10000
burn_in <- 5000
# Define vectors to store posterior samples
alpha_post <- rep(0, n_iter)
beta_post <- rep(0, n_iter)
sigma_post <- rep(0, n_iter)
# Define initial values
alpha_post[1] <- 0.1
beta_post[1] <- 2.0
sigma_post[1] <- sd(Y)
# Define the likelihood function
likelihood <- function(alpha, beta, sigma, x, y){
-sum(dnorm(y, mean = alpha + beta * x, sd = sigma, log = TRUE))
}
# Define the prior function
prior <- function(alpha, beta, sigma, alpha_0, beta_0, sigma_0){
dnorm(alpha, mean = alpha_0, sd = sigma_0, log = TRUE) +
dnorm(beta, mean = beta_0, sd = sigma_0, log = TRUE) +
dnorm(sigma, mean = 0, sd = sigma_0, log = TRUE)
}
# Define the posterior function
posterior <- function(alpha, beta, sigma, x, y, alpha_0, beta_0, sigma_0){
likelihood(alpha, beta, sigma, x, y) +
prior(alpha, beta, sigma, alpha_0, beta_0, sigma_0)
}
# Implement Gibbs sampler
for (i in 2:n_iter){
# Sample alpha from posterior
beta_curr <- beta_post[i-1]
sigma_curr <- sigma_post[i-1]
alpha_post[i] <- rnorm(1, mean = sum(Y - beta_curr*X)/length(Y),
sd = sigma_curr/sqrt(length(Y)))
# Sample beta from posterior
alpha_curr <- alpha_post[i]
beta_post[i] <- rnorm(1, mean = sum(X*(Y-alpha_curr))/sum(X^2),
sd = sigma_curr/sqrt(sum(X^2)))
# Sample sigma from posterior
alpha_curr <- alpha_post[i]
beta_curr <- beta_post[i]
sigma_post[i] <- 1/rgamma(1, shape = (length(Y)+2)/2,
scale = sum((Y - alpha_curr - beta_curr*X)^2)/2)
}
# Remove burn-in period
alpha_post <- alpha_post[-c(1:burn_in)]
beta_post <- beta_post[-c(1:burn_in)]
sigma_post <- sigma_post[-c(1:burn_in)]
# Plot posterior distributions
par(mfrow=c(1,3))
hist(alpha_post, main="Posterior Distribution of Alpha", xlab="Alpha")
hist(beta_post, main="Posterior Distribution of Beta", xlab="Beta")
hist(sigma_post, main="Posterior Distribution of Sigma", xlab="Sigma")
# Plot trace plots
par(mfrow=c(1,3))
plot(alpha_post, main="Trace Plot of Alpha", ylab="Alpha")
plot(beta_post, main="Trace Plot of Beta", ylab="Beta")
plot(sigma_post, main="Trace Plot of Sigma", ylab="Sigma")
To eliminate the influence of starting values, it is common to remove a certain number of initial draws from the posterior samples (also known as the burn-in period). The burn-in period is defined as 5000 iterations in this implementation, so the first 5000 draws are removed from the posterior samples. Checking trace plots of posterior samples is a good practice to ensure that the samples have converged to the true posterior distribution and that the burn-in period is long enough to remove the initial conditions.
# Throw out first 100 draws
alpha_post <- alpha_post[-c(1:100)]
beta_post <- beta_post[-c(1:100)]
sigma_post <- sigma_post[-c(1:100)]
# Compute mean, standard deviation, and quantiles
mean_alpha <- mean(alpha_post)
mean_beta <- mean(beta_post)
mean_sigma <- mean(sigma_post)
sd_alpha <- sd(alpha_post)
sd_beta <- sd(beta_post)
sd_sigma <- sd(sigma_post)
quant_alpha <- quantile(alpha_post, probs = c(0.25, 0.5, 0.75))
quant_beta <- quantile(beta_post, probs = c(0.25, 0.5, 0.75))
quant_sigma <- quantile(sigma_post, probs = c(0.25, 0.5, 0.75))
# Compute ACF and RNE
library(coda)
acf_alpha <- acf(alpha_post)$acf[2]
acf_beta <- acf(beta_post)$acf[2]
acf_sigma <- acf(sigma_post)$acf[2]
rne_alpha <- effectiveSize(alpha_post)
rne_beta <- effectiveSize(beta_post)
rne_sigma <- effectiveSize(sigma_post)
# Print results
cat("Mean alpha:", mean_alpha, "\n")
## Mean alpha: 0.06470502
cat("Mean beta:", mean_beta, "\n")
## Mean beta: 0.1865135
cat("Mean sigma:", mean_sigma, "\n")
## Mean sigma: 3.301535e-07
cat("Std dev alpha:", sd_alpha, "\n")
## Std dev alpha: 9.8006e-09
cat("Std dev beta:", sd_beta, "\n")
## Std dev beta: 1.813938e-09
cat("Std dev sigma:", sd_sigma, "\n")
## Std dev sigma: 1.377371e-08
cat("Quantiles of alpha:", quant_alpha, "\n")
## Quantiles of alpha: 0.06470501 0.06470502 0.06470503
cat("Quantiles of beta:", quant_beta, "\n")
## Quantiles of beta: 0.1865135 0.1865135 0.1865135
cat("Quantiles of sigma:", quant_sigma, "\n")
## Quantiles of sigma: 3.20575e-07 3.298628e-07 3.395877e-07
cat("ACF of alpha (lag 1):", acf_alpha, "\n")
## ACF of alpha (lag 1): 0.009304017
cat("ACF of beta (lag 1):", acf_beta, "\n")
## ACF of beta (lag 1): -0.01129653
cat("ACF of sigma (lag 1):", acf_sigma, "\n")
## ACF of sigma (lag 1): -0.01867184
cat("RNE of alpha:", rne_alpha, "\n")
## RNE of alpha: 0
cat("RNE of beta:", rne_beta, "\n")
## RNE of beta: 0
cat("RNE of sigma:", rne_sigma, "\n")
## RNE of sigma: 0
The mean values of alpha, beta, and sigma are 0.0647, 0.1865, and 3.29719e-07, respectively, based on the results. The alpha, beta, and sigma standard deviations are very small, indicating that the MCMC algorithm has converged. The 25th, 50th, and 75th quantiles for alpha are all very close at 0.06470501, 0.06470502, and 0.06470503, indicating good convergence. At lag 1, the ACF values for all parameters are low, indicating that there is little autocorrelation between successive draws. The RNE values are all zero, indicating that the number of effective samples equals the total number of draws after excluding the first 100. This indicates that the algorithm is very effective at sampling from the posterior distribution.