I'm trying to do the Space-Time Auto-Regression ( STAR ). The code below basically defines the objective function above that I need to minimize, where Y is an N-by-K matrix and D an N-by-N matrix.
import numpy as np
from sys import exit
def obj_func(Y, D, Phi):
# check what went wrong
if not D.shape[0]==D.shape[1]:
print("D =", D)
exit()
if not Y.shape[0]==D.shape[0]:
print("Y =", Y)
print("D =", D)
exit()
if Y.shape[1]<len(Phi):
print("Y =", Y)
print("T =", len(Phi)-1)
exit()
T = len(Phi) - 1
N = Y.shape[0]
K = Y.shape[1]
c = Phi[0] * np.ones((N,1))
loss = 0
for j in range(T,K):
y = Y[:,j].reshape((N,1))
v = y - c
for tau in range(1,T+1):
y = Y[:, j-tau].reshape((N,1))
v = v - Phi[tau] * D.dot(y)
loss += np.linalg.norm(v)
return(loss / (K-T))
The optimization went wrong, and so the first chunk was just added to check which part went wrong exactly. I use scipy.optimize.minimize( )
to minimize the objective function.
from scipy.optimize import minimize
def STAR_pm(Y, D, T):
phi = np.random.normal(loc=0, scale=5, size=T+1)
result = minimize(obj_func, x0=phi, args=(Y,D,), )
if not result.success:
print("No convergence. Try again.")
exit()
return(result.x)
However, when I ran the following, the optimization failed and printed out the matrix D. Turned out that somehow, Y had been assigned to D and so D was not no more an N-by-N matrix.
Y = np.random.randint(0,10, (3,10))
D = np.random.rand(3,3)
STAR_pm(Y, D, T=2)
I find it very confusing. Why is D changed at all? Has this happened to someone else? Anybody help me out here?
In scipy.optimize.minimize
the first argument of the objective function is always the design vector. The extra arguments are given with args, it will be called as obj_func(x, *args)
. From your example I assume, that your design vector is Phi
, All you need to do is to rewrite the signature of your objective as obj_func(Phi, Y, D)
.
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.