我需要根据资产类别收益建模和估计方差-协方差矩阵,因此我查看了https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for第 6 章中给出的股票收益示例-黑客
这是我的简单实现,我从使用具有已知均值和方差-协方差矩阵的多元法线的样本开始。然后我尝试使用非信息性先验来估计它。
估计与已知的先验不同,所以我不确定我的实现是否正确。如果有人能指出我做错了什么,我将不胜感激?
import numpy as np
import pandas as pd
import pymc as pm
p=3
mu=[.03,.05,-.02]
cov_matrix= [[.025,0.0075, 0.00175],[0.0075,.007,0.00135],[0.00175,0.00135,.00043]]
n_obs=10000
x=np.random.multivariate_normal(mu,cov_matrix,n_obs)
prior_mu=np.ones(p)
prior_sigma = np.eye(p)
post_mu = pm.Normal("returns",prior_mu,1,size=p)
post_cov_matrix_inv = pm.Wishart("cov_matrix_inv",n_obs,np.linalg.inv(cov_matrix))
obs = pm.MvNormal( "observed returns", post_mu, post_cov_matrix_inv, observed = True, value = x )
model = pm.Model( [obs, post_mu, post_cov_matrix_inv] )
mcmc = pm.MCMC()
mcmc.sample( 5000, 2000, 3 )
mu_samples = mcmc.trace("returns")[:]
mu_samples.mean(axis=0)
cov_inv_samples = mcmc.trace("cov_matrix_inv")[:]
mean_covariance_matrix = np.linalg.inv( cov_inv_samples.mean(axis=0) )