1
$\begingroup$

I am trying to create a portfolio optimization with the DWave Quantum Computer. I wrote some code trying to somehow reconstruct the following Ising model paper: enter image description here enter image description here enter image description here enter image description here

Ai is the maximum amount of money that can be invested in the i-th asset. B is the total budget. Ri denote the random variable representing the return from asset i.

This is how I tried to code it:

    import datetime
    import pandas as pd
    import fix_yahoo_finance as yf
    import pandas_datareader.data as web
    import numpy as np
    import neal
    import dimod
    from dwave.system import DWaveSampler
    import random
    import hybrid
    
    def cov(a,b):
        return a.cov(b)
    
    def hi(price, returns, cov):
        #mean price
        Ai = np.mean(price)
    
        #mean expected return
        E = np.mean(returns)
    
        # hi = -(1/2)((1/3)*cov(Ri,Rj) + (1/3)Ai^2 - (1/3)E(Ri) - 2B(1/3)*Ai)
        h = (-(1/2)*((1/3)*cov + (1/3)* (Ai ** 2) - (1/3)* E - 2*100*(1/3)*Ai))
        return h
    
    yf.pdr_override()
    
    start = datetime.datetime(2018,1,3)       
    end = datetime.datetime(2021,1,1)
    all_data = {ticker: web.get_data_yahoo(ticker,start,end)
              for ticker in ['AAPL','IBM','MSFT','GOOGL']}    #Note: GOOG has become GOOGL
    price = pd.DataFrame({ticker:data['Adj Close']
                        for ticker,data in all_data.items()})
    volume = pd.DataFrame({ticker:data['Volume']
                         for ticker,data in all_data.items()})
    returns = price.pct_change()      #calculate the percentage of the price
    
    returns = returns.dropna()
    
    print(returns.tail())
        
    a = cov(returns['AAPL'], returns['IBM'])
    b = cov(returns['IBM'], returns['MSFT'])
    c = cov(returns['MSFT'], returns['GOOGL'])
    d = cov(returns['GOOGL'], returns['AAPL'])
    
    apple = hi(price['AAPL'],returns['AAPL'], a)
    ibm = hi(price['IBM'],returns['IBM'], b)
    microsoft = hi(price['MSFT'],returns['MSFT'], c)
    google = hi(price['GOOGL'],returns['GOOGL'], d)
    
    
    sampler = neal.SimulatedAnnealingSampler()
    
    #qpu = DWaveSampler()
    
    h = {apple: 0.0, ibm: 0.0, microsoft: 0.0, google: 0.0}
    #energy changes when bias value changes
    J = {(apple, ibm): 0.0, (ibm, microsoft): 0.0, (google, apple): 0.0, (apple, microsoft): 0.0, (ibm, google): 0.0}
    sampleset = sampler.sample_ising(h, J, num_reads=10, annealing_time=2000)
    print(sampleset)

And this is the output sampleset:

enter image description here

I was wondering what the numbers on top meant, so the -224463.77916595488 1414.5773363996423 etc. and if this is correct

$\endgroup$

1 Answer 1

1
$\begingroup$

I am not familiar with the application you are trying to implement but see a general misunderstanding in the setting of the terms h_i and J_i,j . The numbers in the top of the output are the names of the variables you have defined. You are defining the values of the variables apple, ibm, microsoft, google as variable names. The setting of the biases h_i and coupling strengths J_i,j can be done as dictionaries. But you have to use strings as variable names (keys) and the term is the actual value. So instead of h[apple]=0.0, you need to use something like h["apple"]=apple. The same goes for J_i,j.

In your current implementation all terms are set as 0.0 . This is why all "solutions" have an energy of zero and are trivially minimal.

$\endgroup$
1
  • $\begingroup$ Thank you! I figured this out later haha, but now that I have confirmation, I know for sure. $\endgroup$ Jun 8, 2021 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.