Python scikit学习PCA以增加历史VaR计算中缺失的数据
我有一系列的时间序列数据,我想用来计算大型股票投资组合的历史VaR 投资组合中有大量缺少时间序列数据的工具,我需要一种生成合理缺失值的系统方法 我正在考虑PCA,以便在有足够数据计算因子风险的情况下增加缺失的数据,并尝试了以下Python实现(): 所附示例假设IndexData.csv包含包括DAX在内的多个欧洲指数的价格数据。在实践中,我希望在高度相关的国家/部门篮子上进行操作 问题Python scikit学习PCA以增加历史VaR计算中缺失的数据,python,scikit-learn,pca,missing-data,Python,Scikit Learn,Pca,Missing Data,我有一系列的时间序列数据,我想用来计算大型股票投资组合的历史VaR 投资组合中有大量缺少时间序列数据的工具,我需要一种生成合理缺失值的系统方法 我正在考虑PCA,以便在有足够数据计算因子风险的情况下增加缺失的数据,并尝试了以下Python实现(): 所附示例假设IndexData.csv包含包括DAX在内的多个欧洲指数的价格数据。在实践中,我希望在高度相关的国家/部门篮子上进行操作 问题 我将/应该使用什么样的西格玛和平均值来重新调整计算的回报 在另一个Python库中已经有这样做的功能了吗 [
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
# get index time series returns
df = pd.read_csv('IndexData.csv',index_col=0)
df = df.fillna(method='ffill').pct_change().dropna(how='all')
rics = df.columns.tolist()
# add 'missing' data
df['GDAXI'].iloc[0:30] = None
# see stack overflow reference below
pipeline = make_pipeline(StandardScaler(), PCA(n_components = len(rics) - 1))
# Step 1 - PCA for sub-period with GDAXI data - training period
dfSub = df.iloc[30:]
pipeline.fit(np.array(dfSub))
sub_components = pipeline._final_estimator.components_
# Step 2 - PCA for entire period with no GDAXI -
dfFull = df.loc[:,df.columns != 'GDAXI']
full_transf = pipeline.fit_transform(np.array(dfFull))
# Step 3 - Apply missing asset factor exposures in stage 1 to stage 2
# to augment missing data
synthetic = np.dot(full_transf, sub_components[:,rics.index('GDAXI')])
# rescaling??
df['GDAXI'].iloc[0:30] = synthetic[0:30]