TransWikia.com

Qiskit Portfolio Optimization Application

Quantum Computing Asked by Lana on May 6, 2021

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

# 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. I want to keep the number of assets the same, using Microsoft "MSFT", Disney "DIS", Nike "NKE", and Home Depot "HD" stocks. How might I apply this financial from Quandl to the tutorial? 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.

One Answer

I changed the stock parameter to a list of strings and added the line stockmarket = StockMarket.NASDAQ as such:

num_assets = 4

# Generate expected return and covariance matrix from (random) time-series
stocks = ['MSFT', 'DIS', 'NKE', 'HD']
data = WikipediaDataProvider(
                             token="xeesvko2fu6Bt9jg-B1T",
                             tickers = stocks,
                             stockmarket = StockMarket.NASDAQ,
                             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()
print(mu)
print(sigma)```

Answered by Lana on May 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP