1

I am trying to run a TVP-VAR on statsmodel, but seems to run in a problem when I am trying to validate the vector matrix and the vector shape. Particularly, in the update parameter definition mostly on the dimension and the structure of the update parameters

My model is a TVP-VAR in a Normal Linear State- Space Model composed of the State Equation and the Measurement Equation. This is a big data, therefore I am using large number of variables in the model. My model equations are:

Y_{t}=\tilde{X_{t}}\vartheta_{t}+u_{t}

θ_{t}=θ_{t-1}+w_{t}

where \tilde{X_{t}}=X_{t}\Xi and u_{t}=X'{t}+u{t} with U_{t}\sim N(0,(I+\sigma^{2}X'{t}X{t}))× \Sigma_{t}

The tracebak is :

Traceback (most recent call last):

  File "/Users/user/Documents/PYTHON/Spider/tvp/tvpstandard5.py", line 246, in <module>
    preliminary = mod.fit(maxiter=1000)

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 704, in fit
    mlefit = super(MLEModel, self).fit(start_params, method=method,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/model.py", line 563, in fit
    xopt, retvals, optim_settings = optimizer._fit(f, score, start_params,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/optimizer.py", line 241, in _fit
    xopt, retvals = func(objective, gradient, start_params, fargs, kwargs,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/optimizer.py", line 651, in _fit_lbfgs
    retvals = optimize.fmin_l_bfgs_b(func, start_params, maxiter=maxiter,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/lbfgsb.py", line 197, in fmin_l_bfgs_b
    res = _minimize_lbfgsb(fun, x0, args=args, jac=jac, bounds=bounds,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/lbfgsb.py", line 306, in _minimize_lbfgsb
    sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/optimize.py", line 261, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 140, in __init__
    self._update_fun()

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 233, in _update_fun
    self._update_fun_impl()

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 137, in update_fun
    self.f = fun_wrapped(self.x)

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/scipy/optimize/_differentiable_functions.py", line 134, in fun_wrapped
    return fun(np.copy(x), *args)

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/base/model.py", line 531, in f
    return -self.loglike(params, *args) / nobs

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 933, in loglike
    self.update(params, transformed=True, includes_fixed=True,

  File "/Users/user/Documents/PYTHON/Spider/tvp/tvpstandard5.py", line 218, in update
    self['state_cov'] = np.diag([params[2]**2, params[3]**2, params[4]**2])  # W

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 239, in __setitem__
    return self.ssm.__setitem__(key, value)

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/representation.py", line 420, in __setitem__
    setattr(self, key, value)

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/representation.py", line 54, in __set__
    value = self._set_matrix(obj, value, shape)

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/representation.py", line 68, in _set_matrix
    validate_matrix_shape(

  File "/opt/anaconda3/envs/spyder-env/lib/python3.10/site-packages/statsmodels/tsa/statespace/tools.py", line 1474, in validate_matrix_shape
    raise ValueError('Invalid dimensions for %s matrix: requires %d'

ValueError: Invalid dimensions for state covariance matrix matrix: requires 702 rows, got 3

I am providing code to check with you, what have I done wrong :

    class TVPVAR(sm.tsa.statespace.MLEModel):
    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()
    
    
    @property
    def start_params(self):
       return np.r_[0, 0, 1e-5, 1e-5, 1e-5]
   
    
    @property
    def param_names(self):
        return ['level0', 'phi', 'sigma2.ar1', 'sigma2.level', 'sigma2.slope']
    
    
mod = TVPVAR(y)
index=year

preliminary = mod.fit(maxiter=1000)



res = mod.fit(preliminary.params, method='nm', disp=0, maxiter=1000) 
print(res.summary())

Thanks to everyone for their time in helping me with this.

HelenA
  • 11
  • 2

0 Answers0