Utility Functions
BayesianFactorZoo.construct_weight_matrix
— Functionconstruct_weight_matrix(R::Matrix{Float64}, f::Matrix{Float64},
type::String="OLS", kappa::Float64=1e6)
Construct weighting matrix for GMM estimation of linear SDF models.
Arguments
- R: Matrix of test assets with dimension $t \times N$
- f: Matrix of factors with dimension $t \times k$
- type: "OLS" or "GLS", default="OLS"
- kappa: Large constant for factor moment conditions, default=1e6
Details
Constructs a $(N+k) \times (N+k)$ block diagonal weighting matrix W:
\[W = \begin{bmatrix} W_R & 0_{N\times k} \\ 0_{k\times N} & \kappa I_k \end{bmatrix}\]
where:
OLS (type="OLS"):
$W_R = I_N$
GLS (type="GLS"):
$W_R = \Sigma_R^{-1}$
The structure reflects GMM moment conditions:
\[E[g_t(\lambda_c,\lambda_f,\mu_f)] = E[(R_t - \lambda_c1_N - R_t(f_t - \mu_f)'\lambda_f), (f_t - \mu_f)] = [0_N, 0_k]\]
Returns
Returns a Matrix{Float64} of size $(N+k) \times (N+k)$ containing the weighting matrix W with structure:
- Upper-left block: Identity (OLS) or inverse return covariance (GLS)
- Lower-right block: $\kappa I_k$
- Off-diagonal blocks: Zero matrices
Note: The returned matrix matches the dimension requirements of SDF_gmm function
Notes
- Input matrices R and f must have the same number of rows (time periods)
- The GLS version requires a well-conditioned return covariance matrix
- κ should be large enough to ensure accurate factor mean estimation
- Output matches dimensions required by SDF_gmm function
- Block structure is optimal under conditional homoskedasticity
References
Bryzgalova S, Huang J, Julliard C (2023). "Bayesian solutions for the factor zoo: We just ran two quadrillion models." Journal of Finance, 78(1), 487–557.
Hansen, Lars Peter (1982). "Large Sample Properties of Generalized Method of Moments Estimators." Econometrica, 50(4), 1029-1054.
Examples
# Construct OLS weighting matrix
W_ols = construct_weight_matrix(R, f, "OLS")
# Construct GLS weighting matrix
W_gls = construct_weight_matrix(R, f, "GLS")
# Use custom kappa value
W_custom = construct_weight_matrix(R, f, "OLS", 1e8)
# Use in GMM estimation
results_ols = SDF_gmm(R, f, W_ols)
results_gls = SDF_gmm(R, f, W_gls)
See Also
SDF_gmm
: Main function using these weighting matrices
BayesianFactorZoo.psi_to_priorSR
— Functionpsi_to_priorSR(R::Matrix{Float64}, f::Matrix{Float64};
psi0::Union{Nothing,Float64}=nothing,
priorSR::Union{Nothing,Float64}=nothing,
aw::Float64=1.0, bw::Float64=1.0)
Map between prior tightness parameter $\psi$ and prior Sharpe ratio.
Arguments
R
: Matrix of test assets with dimension $t \times N$f
: Matrix of factors with dimension $t \times k$psi0
: Prior tightness parameter to convert to SRpriorSR
: Target SR to convert to psi0aw,bw
: Beta prior parameters
Returns
Returns a Float64 value either:
- The implied prior Sharpe ratio if psi0 provided
- The required psi0 value if priorSR provided
Note: Returns error message string if neither or both arguments are provided
Notes
- Exactly one of psi0 or priorSR must be provided
- Input matrices R and f must have the same number of rows (time periods)
- The mapping helps choose priors based on economic intuition about achievable Sharpe ratios
- Default aw=bw=1 implies 50% prior probability of factor inclusion
- The relationship is monotonic: higher ψ implies higher prior Sharpe ratio
- Useful for calibrating priors in continuoussssdf and continuoussssdf_v2
References
Bryzgalova S, Huang J, Julliard C (2023). "Bayesian solutions for the factor zoo: We just ran two quadrillion models." Journal of Finance, 78(1), 487–557.
Examples
# Load test data
# Convert psi0 to implied prior Sharpe ratio
implied_sr = psi_to_priorSR(R, f; psi0=5.0)
println("Psi0 = 5.0 implies prior SR = $implied_sr")
# Find psi0 needed for target Sharpe ratio
required_psi = psi_to_priorSR(R, f; priorSR=0.5)
println("For prior SR = 0.5, need psi0 = $required_psi")
# Use custom Beta prior parameters
psi_sparse = psi_to_priorSR(R, f;
priorSR=0.3,
aw=1.0,
bw=9.0) # Prior favoring sparsity
# Helper functions also available:
using BayesianFactorZoo: calculate_prior_SR, find_psi_for_target_SR
# Get prior SR for a given psi
sr = calculate_prior_SR(5.0, R, f)
# Get psi for a target SR
psi = find_psi_for_target_SR(0.5, R, f)
See Also
continuous_ss_sdf
: Main function using this prior calibrationcontinuous_ss_sdf_v2
: Version for tradable factors using this calibration