简体   繁体   中英

'numpy.ndarray' object is not callable when using a CALLABLE function in minimization

I keep getting the numpy.ndarray object is not callable error. I know that this mistake happens because one uses an np.array instead of a function. The problem in my code, is that I am indeed using a function to run the minimize python function.

Could someone please let me know what is happening?

The code is here:

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 15 06:27:54 2015


"""

# -*- coding: utf-8 -*-
"""
Created on Mon Oct 12 20:22:27 2015


"""

# Midterm Macroeconometrics

import numpy as np
from numpy import log
import numpy.linalg as linalg
from scipy import *
from scipy.optimize import fminbound, broyden1, brentq, bisect, minimize
from scipy import interp
import pylab as pl
#from numdifftools import Gradient, Jacobian, Derivative
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
import scipy.io as sio
import os

"""
IMPORTING DATA FROM PANDAS

"""
#Importing data from text file- using Pandas.
os.chdir(r'/Users/camilahenao/Dropbox/UIUC Phd Econ/Year 3/Fall/macroeconometrics shin/Homework/ps3-MIDTERM')
os.path.abspath(os.path.curdir)

data=pd.read_csv(r'midterm2015.csv', header= None)
data.columns = ['GDP_I', 'GDP_E']
GDP_I=np.array(data.GDP_I)
GDP_E=np.array(data.GDP_E)

y= np.vstack((GDP_I,GDP_E))

def kalman2(a_old, p_old, Z, gamma, theta, y):
    mu, rho, h_I, h_E, h_G = theta[0], theta[1], np.log(theta[2]), np.log(theta[3]), np.log(theta[4])
    sigma_I= np.exp(h_I)
    sigma_E= np.exp(h_E)
    sigma_G= np.exp(h_G)

    H = np.array([[sigmaI,0],[0, sigmaE]])
    H=np.matrix(H)
    list_a = np.array([a_old])
    list_p = np.array([p_old])
    list_f = np.array([])
    list_v = np.array([])
    log_likelihood_Y= np.array([ ])
    list_log_like_sum = np.array([])

    for i in range(y[0].size):
        N=y.shape[0]
        Time=y[0].size
        inv= np.matrix(linalg.inv(Z*p_old*Z.T+H))
        cosa= Z.T*inv
        temp= p_old*cosa
        a_new= np.array(a_old +temp*(np.array([[y[0][i]],[y[1][i]]])-Z*a_old-gamma*w))[0]
        list_a=np.hstack((list_a,a_new))

        p_new= np.array(p_old - temp* Z*p_old)[0]
        list_p=np.hstack((list_p, p_new))

        #Transform the previous posterior into prior-
        a_old=T*a_new
        a_old=a_old[0]
        p_old=T*p_new*T + R*Q*R #25

        #Moments for log-likelihood:
        f= np.linalg.det(Z*p_old*Z.T + H)
        list_f= np.hstack((list_f,f))
        #print list_f

        v= np.array([[y[0][i]],[y[1][i]]])-Z*a_old - gamma*w
        v_element= np.array((v.T *np.matrix(np.linalg.inv(Z*p_old*Z.T + H)) *v))[0]
        list_v=np.hstack((list_v,v_element))
        #print list_v

        #Log likelihood function for each period of time:
        log_like= (-N*(Time-1)/2*np.log(2*pi)-(1/2)*sum(log(list_f)) -(1/2)*sum(list_v))
        log_likelihood_Y=np.hstack((log_likelihood_Y, log_like))

        #Create the sum over all Time of the log-likelihood
        log_like_sum=np.sum(log_likelihood_Y)
        list_log_like_sum=np.hstack((list_log_like_sum, log_like_sum))

    return list_a, list_p, log_likelihood_Y, list_log_like_sum

#Define the "callable function"

def mle(a_old, p_old, Z, gamma, theta, y, bds):
    a, P, py, py_sum = kalman2(a_old, p_old, Z, gamma, theta, y)
    mle= -1*py_sum

    return mle

#Run the minimization algorithm
theta2=(.8, 3.0, 5.0, 5.0, 5.0)
a_old=0.0
p_old= sigmaG/(1-rho**2)
Z=np.array([[1.0],[1.0]])
gamma=np.array([[1.0],[1.0]])
bds = [[-10e100, 10e100], [-10e100, 10e100], [1e-6, 10e100], [1e-6, 10e100], [1e-6, 10e100]]
theta_guess = [3, 0.8, np.sqrt(5), np.sqrt(5), np.sqrt(5)]

result = minimize(mle(a_old, p_old, Z, gamma, theta, y, bds), theta_guess, bounds = bds)

As Warren Weckesser mentioned in a comment, you're passing the result of calling mle(a_old, p_old, Z, gamma, theta, y, bds) — which is a floating point value — as the first argument to the minimize() function. According to the scipy documentation the first argument to minimize() should be a callable function, so for starters you're going to need to change the call it so it's something like this:

result = minimize(mle, (a_old, p_old, Z, gamma, theta, y, bds), 
                  theta_guess, bounds=bds)

However you're going to run into new problems because your mle() function doesn't accept a vector as its first argument, which is required of the function you pass to minimize() — so you're also going to need to modify its definition accordingly.

Unfortunately I don't understand enough of what you're actually trying to accomplish to suggest how you should do that.

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