1

我不断收到numpy.ndarray对象不可调用错误。我知道发生这个错误是因为使用 np.array 而不是函数。我的代码中的问题是我确实使用了一个函数来运行minimizepython 函数。

有人可以让我知道发生了什么吗?

代码在这里:

# -*- 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)
4

1 回答 1

1

正如 Warren Weckesser 在评论中提到的,您将调用的结果mle(a_old, p_old, Z, gamma, theta, y, bds)(一个浮点值)作为minimize()函数的第一个参数传递。根据scipy文档,第一个参数minimize()应该是一个可调用函数,所以对于初学者来说,你需要更改调用它,所以它是这样的:

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

但是,您将遇到新问题,因为您的mle()函数不接受向量作为第一个参数,而这是您传递给的函数所必需的minimize()——因此您还需要相应地修改其定义。

不幸的是,我对您实际尝试完成的工作了解不够,无法建议您应该如何做到这一点。

于 2015-10-15T13:42:30.247 回答