0

我最近被扔进了量子计算的世界,我是编码的初学者。我被指派做 Qiskit Finance 教程的投资组合优化教程并输入真实数据。说实话,我一无所知。我的理解是,我必须替换代码的“TICKER”和“RandomDataProvider”部分才能生成真实的投资组合。

# Generate expected return and covariance matrix from (random) time-series
stocks = [("TICKER%s" % i) for i in range(num_assets)]
data = RandomDataProvider(tickers=stocks,
                 start=datetime.datetime(2016,1,1),
                 end=datetime.datetime(2016,1,30))
data.run()
mu = data.get_period_return_mean_vector()
sigma = data.get_period_return_covariance_matrix()

我已经导入了 Quandl 和 WikipediaDataProvider。我想保持资产数量不变,使用微软“MSFT”、迪士尼“DIS”、耐克“NKE”和家得宝“HD”股票。我如何将 Quandl 的财务数据应用到教程中?到目前为止,我已经尝试过:

num_assets = 4

# Generate expected return and covariance matrix from (random) time-series
stocks = [("MSFT%s" , "DIS%s" , "NKE%s" , "HD%s" % i) for i in range(num_assets)]
data = WikipediaDataProvider(tickers=stocks,
                 token="xeesvko2fu6Bt9jg-B1T",
                 start=datetime.datetime(2016,1,1),
                 end=datetime.datetime(2016,1,30))
data.run()
mu = data.get_period_return_mean_vector()
sigma = data.get_period_return_covariance_matrix()

但得到错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-19e4d9cde1e3> in <module>
      3 # Generate expected return and covariance matrix from (random) time-series
      4 stocks = [("MSFT%s" , "DIS%s" , "NKE%s" , "HD%s" % i) for i in range(num_assets)]
----> 5 data = WikipediaDataProvider(tickers=stocks,
      6                  token="xeesvko2fu6Bt9jg-B1T",
      7                  start=datetime.datetime(2016,1,1),

TypeError: Can't instantiate abstract class WikipediaDataProvider with abstract methods run

我为我有限的编码技能道歉 - 我对这一切都很陌生!先感谢您。

4

2 回答 2

1

股票参数应该是一个字符串列表。如果你试试:

stocks = ['MSFT', 'DIS', 'NKE', 'HD']

它会起作用的。只要确保您安装了最新的 Qiskit。我自己运行并打印 mu 和 sigma:

mu: [ 0.00057085 -0.00379642  0.00057495 -0.00209479]
sigma: [[0.00059268 0.00036507 0.00022995 0.00025648]
 [0.00036507 0.00041735 0.00016424 0.00027058]
 [0.00022995 0.00016424 0.0002836  0.00022028]
 [0.00025648 0.00027058 0.00022028 0.00042107]]
于 2020-08-12T18:00:07.000 回答
0

我注册了 Quandl,但它不会在 2016 年之后免费给我任何东西。雅虎似乎在没有任何令牌的情况下为我工作,并且使用了最新数据。

data = YahooDataProvider(
             tickers = ["AAPL", "MSFT","WORK","TEAM"],
             start=datetime.datetime(2020, 9, 1),
             end=datetime.datetime(2020, 9, 30))
于 2020-10-04T07:42:12.933 回答