简体   繁体   中英

optimize.fmin error: IndexError: too many indices for array

I am trying to optimize a function in python, using optimize.fmin from scipy. The function should optimize a vector of parameters, given initial conditions and arguments. However, I keep receiving the following error when I try to run the optimization, while running the function itself works:

IndexError: too many indices for array, line 1, in parametrization

In brief, my code is like:

import numpy as np # import numpy library
import pandas as pd # import pandas library
from scipy import optimize # import optimize from scipy library
from KF_GATSM import KF_GATSM # import script with Kalman filter

yields=pd.read_excel('data.xlsx',index_col=None,header=None) # Import observed yields
Omega0=pd.read_excel('parameters.xlsx') # Import initial parameters

# Function to optimize
def GATSM(Omega,yields,N):

     # recover parameters
     Omega=np.matrix(Omega)
     muQ,muP=parametrization(N,Omega) # run parametrization

Y=muQ+muP # or any other function

return Y

# Parametrization of the function
def parametrization(nstate,N,Omega):

     muQ=np.matrix([[Omega[0,0],0,0]]).T # intercept risk-neutral world
     muP=np.matrix([[Omega[1,0],Omega[2,0],Omega[3,0]]]).T # intercept physical world

     return muQ,muP

# Run optimization
def MLE(data,Omega0):

    # extract number of observations and yields maturities
    N=np.shape(yields)[1]

    # local optimization
     omega_opt=optimize.fmin(GATSM,np.array(Omega0)[:,0],args=(yields,N)) 

    return Y

I solved the issue. It seems that I cannot select the element of an array as follows in Scipy (although it works in Numpy):

Omega[0,0]
Omega[0]

The trick is to use:

Omega.item(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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