简体   繁体   English

Qiskit 投资组合优化应用程序

[英]Qiskit Portfolio Optimization Application

I recently got flung into the world of quantum computing and I'm a beginner at coding.我最近被扔进了量子计算的世界,我是编码的初学者。 I was assigned to do the Portfolio Optimization tutorial of the Qiskit Finance Tutorials and input real data.我被指派做 Qiskit Finance 教程的投资组合优化教程并输入真实数据。 Truth be told, I'm clueless.说实话,我一无所知。 It's my understanding that I have to replace the "TICKER" and "RandomDataProvider" parts of the code in order to generate a real-life portfolio.我的理解是,我必须替换代码的“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()

I've imported Quandl and WikipediaDataProvider.我已经导入了 Quandl 和 WikipediaDataProvider。 I want to keep the number of assets the same, using Microsoft "MSFT", Disney "DIS", Nike "NKE", and Home Depot "HD" stocks.我想保持资产数量不变,使用微软“MSFT”、迪士尼“DIS”、耐克“NKE”和家得宝“HD”股票。 How might I apply this financial from Quandl to the tutorial?我如何将 Quandl 的财务数据应用到教程中? I've tried this so far:到目前为止,我已经尝试过:

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()

But get the error:但得到错误:

---------------------------------------------------------------------------
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

I apologize for my limited coding skills - I'm very new to all of this.我为我有限的编码技能道歉——我对这一切都很陌生。 Thank you in advance.先感谢您。

The stocks parameter should be a list of strings.股票参数应该是一个字符串列表。 If you try:如果你试试:

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

It will work.它会起作用的。 Just make sure you have the latest Qiskit installed.只要确保您安装了最新的 Qiskit。 I ran myself and printed mu and sigma:我自己运行并打印 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]]

I signed up for Quandl but it wouldn't give me anything beyond 2016 for free.我注册了 Quandl,但它不会在 2016 年之后免费给我任何东西。 Yahoo seemed to work for me without any token, and with the latest data.雅虎似乎在没有任何令牌的情况下为我工作,并且使用了最新数据。

data = YahooDataProvider(
             tickers = ["AAPL", "MSFT","WORK","TEAM"],
             start=datetime.datetime(2020, 9, 1),
             end=datetime.datetime(2020, 9, 30))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM