【问题标题】:Performing a Principal Component Analysis to reconstruct time series creates more values than expected执行主成分分析以重建时间序列会产生比预期更多的值
【发布时间】:2021-02-04 12:15:00
【问题描述】:

我想在this notebook 之后进行主成分分析,以从其成分(使用 Quandl 找到)重建 DJIA(我正在使用 alpha_ventage)。然而,在重建将主成分乘以其权重的值时,我创建的值似乎比原始数据框多。

kernel_pca = KernelPCA(n_components=5).fit(df_z_components)
pca_5 = kernel_pca.transform(-daily_df_components)

weights = fn_weighted_average(kernel_pca.lambdas_)
reconstructed_values = np.dot(pca_5, weights)

确实,daily_df_components 是由 quandl API 从 DJIA 的组件创建的,它似乎比我用来获取 DJIA 指数的库 alpha_ventage 拥有更多的数据。

这是完整的代码

"""
Obtaining the components data from quandl
"""
import quandl

QUANDL_API_KEY = 'MYKEY'
quandl.ApiConfig.api_key = QUANDL_API_KEY

SYMBOLS = [
        'AAPL', 'MMM', 'BA', 'AXP', 'CAT',
        'CVX', 'CSCO', 'KO', 'DD', 'XOM',
        'GS', 'HD', 'IBM', 'INTC', 'JNJ',
        'JPM', 'MCD', 'MRK', 'MSFT', 'NKE', 
        'PFE', 'PG', 'UNH', 'UTX', 'TRV',
        'VZ', 'V', 'WMT', 'WBA', 'DIS'
]

wiki_symbols = ['WIKI/%s'%symbol for symbol in SYMBOLS]
df_components = quandl.get(
    wiki_symbols, 
    start_date='2017-01-01', 
    end_date='2017-12-31', 
    column_index=11)
df_components.columns = SYMBOLS

filled_df_components = df_components.fillna(method='ffill')
daily_df_components = filled_df_components.resample('24h').ffill()
daily_df_components = daily_df_components.fillna(method='bfill')


"""
Download the all-time DJIA dataset
"""
from alpha_vantage.timeseries import TimeSeries

# Update your Alpha Vantage API key here...
ALPHA_VANTAGE_API_KEY = 'MYKEY'

ts = TimeSeries(key=ALPHA_VANTAGE_API_KEY, output_format='pandas')
df, meta_data = ts.get_intraday(symbol='DIA',interval='1min', outputsize='full')

# Finding eigenvectors and eigen values
fn_weighted_average = lambda x: x/x.sum()
weighted_values = fn_weighted_average(fitted_pca.lambdas_)[:5]

from sklearn.decomposition import KernelPCA

fn_z_score = lambda x: (x - x.mean())/x.std()

df_z_components = daily_df_components.apply(fn_z_score)
fitted_pca = KernelPCA().fit(df_z_components)

# Reconstructing the Dow Average with PCA
import numpy as np

kernel_pca = KernelPCA(n_components=5).fit(df_z_components)
pca_5 = kernel_pca.transform(-daily_df_components)

weights = fn_weighted_average(kernel_pca.lambdas_)
reconstructed_values = np.dot(pca_5, weights)

# Combine PCA and Index to compare
df_combined = djia_2020_weird.copy()
df_combined['pca_5'] = reconstructed_values

但它会返回:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-100-2808dc14f789> in <module>()
      9 # Combine PCA and Index to compare
     10 df_combined = djia_2020_weird.copy()
---> 11 df_combined['pca_5'] = reconstructed_values
     12 df_combined = df_combined.apply(fn_z_score)
     13 df_combined.plot(figsize=(12,8));

3 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in sanitize_index(data, index)
    746     if len(data) != len(index):
    747         raise ValueError(
--> 748             "Length of values "
    749             f"({len(data)}) "
    750             "does not match length of index "

ValueError: Length of values (361) does not match length of index (14)

确实,reconstructed_values 的长度为 361,df_combined 的长度为 14 个值... 这是最后一个数据框:

            DJI
date    
2021-01-21  NaN
2021-01-22  311.37
2021-01-23  310.03
2021-01-24  310.03
2021-01-25  310.03
2021-01-26  309.01
2021-01-27  309.49
2021-01-28  302.17
2021-01-29  305.25
2021-01-30  299.20
2021-01-31  299.20
2021-02-01  299.20
2021-02-02  302.13
2021-02-03  307.86

可能是因为notebook作者可以拿到他感兴趣的一整年的数据,我跑数据的时候好像只有两个月的时间?

【问题讨论】:

    标签: python-3.x pca finance alpha-vantage


    【解决方案1】:

    哎呀,我是笔记本的作者。在撰写本文后,Quandl 似乎不再提供 DJIA 的历史价格,并且未授予版权以重新分发数据。对于研究,您可以考虑使用其他免费股票代码来代理 DJIA。

    repo 中的 example usages have been updated 用于演示 KernelPCA,如 here 所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-15
      • 2018-11-11
      • 2017-04-22
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2021-12-15
      相关资源
      最近更新 更多