Thanks to everyone in advance for their time!
I am trying to run a TVP-VAR for a panel in the statespace mlemodels in statsmodel. I get an error while trying to fit the model. Not only that, but I cannot really locate the part of the code the error comes from. How could I do that ? I use Spyder as IDE and the only type error is showing is:
TypeError: '>=' not supported between instances of 'method' and 'float'
File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/numpy/core/_methods.py", line 113, in _clip_dep_invoke_with_casting
return ufunc(*args, out=out, **kwargs)
The errors are pumping up when I am trying to run a preliminary estimation of the model
preliminary = tvppanelvarmodel.fit(maxiter=1000)
enter code here
Why is it so? Is there a way to understand the reason of these and advice on how to set it up.
When I open Numpy _methos.py in order to see the error it reads
def _clip_dep_invoke_with_casting(ufunc, *args, out=None, casting=None, **kwargs):
# normal path
if casting is not None:
return ufunc(*args, out=out, casting=casting, **kwargs)
# try to deal with broken casting rules
try:
return ufunc(*args, out=out, **kwargs)
except _exceptions._UFuncOutputCastingError as e:
# Numpy 1.17.0, 2019-02-24
warnings.warn(
"Converting the output of clip from {!r} to {!r} is deprecated. "
"Pass `casting=\"unsafe\"` explicitly to silence this warning, or "
"correct the type of the variables.".format(e.from_, e.to),
DeprecationWarning,
stacklevel=2
)
return ufunc(*args, out=out, casting="unsafe", **kwargs)
The full code I am running is:
class TVPVAR(sm.tsa.statespace.MLEModel):
def start_params(self):
start_params = [.1, .1, 100, 100, 100]
def __init__(self, y):
# Create a matrix with [y_t' : y_{t-1}'] for t = 2, ..., T
augmented = sm.tsa.lagmat(y, 1, trim='both', original='in', use_pandas=True)
# Separate into y_t and z_t = [1 : y_{t-1}']
p = y.shape[1]
y_t = augmented.iloc[:, :p]
z_t = sm.add_constant(augmented.iloc[:, p:])
nobs = y.shape[0]
T=y.shape[0]
# Recall that the length of the state vector is p * (p + 1)
k_states = p * (p + 1)
super(TVPVAR,self).__init__(y_t, exog=None, k_states=k_states,k_posdef=k_states)
self.k_y = p
self.k_states = p * (p + 1)
self.nobs = T
self['design'] = np.zeros((self.k_y, self.k_states, 1))
self['transition'] = np.eye(k_states) # G
self['selection'] = np.eye(k_states) # R=1
def update_variances(self, obs_cov, state_cov_diag):
self['obs_cov'] = obs_cov
self['state_cov'] = np.diag(state_cov_diag) # W
init = initialization.Initialization(self.k_states)
init.set((0, 2), 'diffuse')
init.set((2, 4), 'stationary')
self.ssm.initialize(init)
def constrain_stationary_multivariate(unconstrained, variance,
transform_variance=False,
prefix=None):
unconstrained =np.zeros_like(k_y * k_y * order)
variance=np.zeros_like(k_y * k_y)
order = k_y
prefix = find_best_blas_type(
[unconstrained, variance])
dtype = prefix_dtype_map[prefix]
unconstrained = np.asfortranarray(unconstrained, dtype=dtype)
variance = np.asfortranarray(variance, dtype=dtype)
# Step 1: convert from arbitrary matrices to those with singular values
# less than one.
# sv_constrained = _constrain_sv_less_than_one(unconstrained, order,
# k_y, prefix)
sv_constrained = prefix_sv_map[prefix](unconstrained, order, k_y)
# Step 2: convert matrices from our "partial autocorrelation matrix"
# space (matrices with singular values less than one) to the space of
# stationary coefficient matrices
constrained, variance = prefix_pacf_map[prefix](
sv_constrained, variance, transform_variance, order, k_y)
constrained = np.zeros_like(constrained, dtype=dtype)
variance = np.zeros_like(variance, dtype=dtype)
return constrained, variance
def unconstrain_stationary_multivariate(constrained, error_variance):
constrained= np.zeros_like(k_y * k_y * order)
error_variance=np.zeros_like(k_y * k_y)
# Step 1: convert matrices from the space of stationary
# coefficient matrices to our "partial autocorrelation matrix" space
# (matrices with singular values less than one)
partial_autocorrelations = _compute_multivariate_pacf_from_coefficients(
constrained, error_variance, order, k_y)
unconstrained = _unconstrain_sv_less_than_one(
partial_autocorrelations, order, k_y)
return unconstrained, error_variance
def update(self, params, **kwargs):
params = super().update(params, **kwargs)
self['transition', 2,2] = params[0]
self['transition', 3,2] = params[1]
self['state_cov'] = np.diag([params[2]**2, params[3]**2, params[4]**2]) # W
@property
def state_names(self):
state_names = np.empty((self.k_y, self.k_y + 1), dtype=object)
for i in range(self.k_y):
endog_name = self.y__names[i]
state_names[i] = (
['intercept.%s' % y_name] +
['L1.%s->%s' % (other_name, y_name) for other_name in self.y_names])
return state_names.ravel().tolist()
tvppanelvarmodel = TVPVAR(y)
index=year
preliminary = tvppanelvarmodel.fit(maxiter=1000)
res = tvppanelvarmodel.fit(preliminary.params, method='nm', disp=0, maxiter=1000)
print(res.summary())
# Impluse Response
ax = res.impulse_responses(10, orthogonalized=True, impulse=[1, 0]).plot(figsize=(13,3))
ax.set(xlabel='t', title='Responses to a shock to `xxxx`');
I have seen the previous answer by Chad Fulton(@cfulton) on the initial conditions for statespace mlemodel, my code looks inline