View source: R/BayesianFamaMacBeth.R
| BayesianFM | R Documentation |
This function provides the Bayesian Fama-MacBeth regression.
BayesianFM(f, R, sim_length)
f |
A matrix of factors with dimension |
R |
A matrix of test assets with dimension |
sim_length |
The length of MCMCs; |
BayesianFM is similar to another twin function in this package, BayesianSDF,
except that we estimate factors' risk premia rather than risk prices in this function.
Unlike BayesianSDF, we use factor loadings, \beta_f, instead of covariance exposures, C_f,
in the Fama-MacBeth regression. In particular, after we obtain the posterior draws of \mu_{Y} and \Sigma_{Y}
(details can be found in the section introducing BayesianSDF function),
we calculate \beta_f as follows: \beta_f = C_f \Sigma_f^{-1}, and \beta = (1_N, \beta_f).
Bayesian Fama-MacBeth (BFM)
The posterior distribution of \lambda conditional on \mu_{Y}, \Sigma_{Y}, and the data, is a Dirac distribution at
(\beta^\top \beta)^{-1} \beta^\top \mu_R.
Bayesian Fama-MacBeth GLS (BFM-GLS)
The posterior distribution of \lambda conditional on \mu_{Y}, \Sigma_{Y}, and the data, is a Dirac distribution at
(\beta^\top \Sigma_R^{-1} \beta)^{-1} \beta^\top \Sigma_R^{-1} \mu_R .
The return of BayesianFM is a list of the following elements:
lambda_ols_path: A sim_length\times (k+1) matrix of OLS risk premia estimates (Each row represents a draw.
Note that the first column is \lambda_c corresponding to the constant term.
The next k columns are the risk premia estimates of the k factors);
lambda_gls_path: A sim_length\times (k+1) matrix of the risk premia estimates \lambda (GLS);
R2_ols_path: A sim_length\times 1 matrix of the R^2_{OLS};
R2_gls_path: A sim_length\times 1 matrix of the R^2_{GLS}.
## <-------------------------------------------------------------------------------->
## Example: Bayesian Fama-MacBeth
## <-------------------------------------------------------------------------------->
library(reshape2)
library(ggplot2)
# Load Data
data("BFactor_zoo_example")
HML <- BFactor_zoo_example$HML
lambda_ols <- BFactor_zoo_example$lambda_ols
R2.ols.true <- BFactor_zoo_example$R2.ols.true
sim_f <- BFactor_zoo_example$sim_f
sim_R <- BFactor_zoo_example$sim_R
uf <- BFactor_zoo_example$uf
## <-------------------Case 1: strong factor---------------------------------------->
# the Frequentist Fama-MacBeth
# sim_f: simulated factor, sim_R: simulated return
# sim_f is the useful (i.e., strong) factor
results.fm <- Two_Pass_Regression(sim_f, sim_R)
# the Bayesian Fama-MacBeth with 10000 simulations
results.bfm <- BayesianFM(sim_f, sim_R, 2000)
# Note that the first element correspond to lambda of the constant term
# So we choose k=2 to get lambda of the strong factor
k <- 2
m1 <- results.fm$lambda[k]
sd1 <- sqrt(results.fm$cov_lambda[k,k])
bfm<-results.bfm$lambda_ols_path[1001:2000,k]
fm<-rnorm(20000,mean = m1, sd=sd1)
data<-data.frame(cbind(fm, bfm))
colnames(data)<-c("Frequentist FM", "Bayesian FM")
data.long<-melt(data)
p <- ggplot(aes(x=value, colour=variable, linetype=variable), data=data.long)
p+
stat_density(aes(x=value, colour=variable),
geom="line",position="identity", size = 2, adjust=1) +
geom_vline(xintercept = lambda_ols[2], linetype="dotted", color = "#8c8c8c", size=1.5)+
guides(colour = guide_legend(override.aes=list(size=2), title.position = "top",
title.hjust = 0.5, nrow=1,byrow=TRUE))+
theme_bw()+
labs(color=element_blank()) +
labs(linetype=element_blank()) +
theme(legend.key.width=unit(4,"line")) +
theme(legend.position="bottom")+
theme(text = element_text(size = 26))+
xlab(bquote("Risk premium ("~lambda[strong]~")")) +
ylab("Density" )
## <-------------------Case 2: useless factor--------------------------------------->
# uf is the useless factor
# the Frequentist Fama-MacBeth
results.fm <- Two_Pass_Regression(uf, sim_R)
# the Bayesian Fama-MacBeth with 10000 simulations
results.bfm <- BayesianFM(uf, sim_R, 2000)
# Note that the first element correspond to lambda of the constant term
# So we choose k=2 to get lambda of the useless factor
k <- 2
m1 <- results.fm$lambda[k]
sd1 <- sqrt(results.fm$cov_lambda[k,k])
bfm<-results.bfm$lambda_ols_path[1001:2000,k]
fm<-rnorm(20000,mean = m1, sd=sd1)
data<-data.frame(cbind(fm, bfm))
colnames(data)<-c("Frequentist FM", "Bayesian FM")
data.long<-melt(data)
p <- ggplot(aes(x=value, colour=variable, linetype=variable), data=data.long)
p+
stat_density(aes(x=value, colour=variable),
geom="line",position="identity", size = 2, adjust=1) +
geom_vline(xintercept = lambda_ols[2], linetype="dotted", color = "#8c8c8c", size=1.5)+
guides(colour = guide_legend(override.aes=list(size=2),
title.position = "top", title.hjust = 0.5, nrow=1,byrow=TRUE))+
theme_bw()+
labs(color=element_blank()) +
labs(linetype=element_blank()) +
theme(legend.key.width=unit(4,"line")) +
theme(legend.position="bottom")+
theme(text = element_text(size = 26))+
xlab(bquote("Risk premium ("~lambda[strong]~")")) +
ylab("Density" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.