Skip to content

Commit

Permalink
typo in FRP se calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
a91quaini committed Apr 8, 2024
1 parent 4398782 commit 8712e26
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 34 deletions.
2 changes: 1 addition & 1 deletion inst/examples/example.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ ggplot2::ggplot(df, ggplot2::aes(

ggplot2::ggsave(
"inst/examples/risk_premia.png",
width = 7,
width = 9,
height = 5,
dpi=600
)
Expand Down
Binary file modified inst/examples/risk_premia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 15 additions & 31 deletions src/frp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ Rcpp::List ReturnFRPCpp(
returns,
factors,
beta,
covariance_factors_returns,
variance_returns,
mean_returns,
hac_prewhite
Expand All @@ -125,7 +124,6 @@ Rcpp::List ReturnFRPCpp(
returns,
factors,
beta,
covariance_factors_returns,
variance_returns,
mean_returns,
hac_prewhite
Expand Down Expand Up @@ -195,7 +193,6 @@ arma::vec StandardErrorsFRPCpp(
const arma::mat& returns,
const arma::mat& factors,
const arma::mat& beta,
const arma::mat& covariance_factors_returns,
const arma::mat& variance_returns,
const arma::vec& mean_returns,
const bool hac_prewhite
Expand All @@ -204,44 +201,32 @@ arma::vec StandardErrorsFRPCpp(
// Compute the matrix H as the inverse of beta' * beta
const arma::mat h_matrix = InvSympd(beta.t() * beta);

// Compute matrix A used in the standard error calculation
const arma::mat a_matrix = h_matrix * beta.t();

// Center the returns and factors
const arma::mat returns_centred = returns.each_row() - mean_returns.t();
const arma::vec mean_factors = arma::mean(factors).t();
const arma::mat factors_centred = factors.each_row() - mean_factors.t();

// Intermediate matrices for standard error calculation
const arma::mat gamma = returns_centred * a_matrix.t();
const arma::vec gamma_true = a_matrix * mean_returns;
const arma::mat phi = gamma - factors;
const arma::mat phi_centred = phi.each_row() - (gamma_true - mean_factors).t();

// Compute variance of factors and solve for fac_centred_var_fac_inv
const arma::mat variance_factors = arma::cov(factors);
const arma::mat fac_centred_var_fac_inv = SolveSympd(
variance_factors,
const arma::mat factors_centred = factors.each_row() - arma::mean(factors);

// term1: mean returns
const arma::mat term1 = returns_centred * beta * h_matrix;

// term2: beta
const arma::mat phi_centred = term1 - factors_centred;
const arma::mat fac_cen_var_fac_inv = SolveSympd(
arma::cov(factors),
factors_centred.t()
).t();

// Compute matrix Z used in standard error calculation
const arma::mat z = fac_centred_var_fac_inv.each_col() % (
// term3: misspecification
const arma::mat z = fac_cen_var_fac_inv.each_col() % (
returns_centred * (mean_returns - beta * frp)
);

arma::mat series =
// mean term
(gamma.each_row() - (a_matrix * mean_returns).t()) -
term1 -
// beta term
phi_centred.each_col() % (factors_centred * SolveSympd(
variance_factors,
gamma_true
)) +
// error term
(fac_centred_var_fac_inv.each_col() % (
returns_centred * (mean_returns - beta * frp)
)) * h_matrix;
phi_centred.each_col() % (fac_cen_var_fac_inv * frp) +
// misspecification term
z * h_matrix;

// Return the HAC standard errors of the estimator
return HACStandardErrorsCpp(series, hac_prewhite) /
Expand All @@ -257,7 +242,6 @@ arma::vec StandardErrorsKRSFRPCpp(
const arma::mat& returns,
const arma::mat& factors,
const arma::mat& beta,
const arma::mat& covariance_factors_returns,
const arma::mat& variance_returns,
const arma::vec& mean_returns,
const bool hac_prewhite
Expand Down
2 changes: 0 additions & 2 deletions src/frp.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ arma::vec StandardErrorsFRPCpp(
const arma::mat& returns,
const arma::mat& factors,
const arma::mat& beta,
const arma::mat& covariance_factors_returns,
const arma::mat& variance_returns,
const arma::vec& mean_returns,
const bool hac_prewhite = false
Expand All @@ -129,7 +128,6 @@ arma::vec StandardErrorsKRSFRPCpp(
const arma::mat& returns,
const arma::mat& factors,
const arma::mat& beta,
const arma::mat& covariance_factors_returns,
const arma::mat& variance_returns,
const arma::vec& mean_returns,
const bool hac_prewhite = false
Expand Down

0 comments on commit 8712e26

Please sign in to comment.