Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covariance matrix is singular or near-singular, with condition number nan. Standard errors may be unstable. #5384

Open
ZhonghuaGao opened this issue Nov 14, 2018 · 4 comments

Comments

@ZhonghuaGao
Copy link

ZhonghuaGao commented Nov 14, 2018

Hi I am trying to use customized LocalLinearTrend. I have one observation variable and 12 unobserved states + 1 intercept vector with dimension as 12*1. Here is the code and error.

class LocalLinearTrend(sm.tsa.statespace.MLEModel):
def init(self, endog):
# Model order
k_states = k_posdef = 12

    # Initialize the statespace
    super(LocalLinearTrend, self).__init__(
        endog, k_states=k_states, k_posdef=k_posdef,
        initialization='approximate_diffuse',
        loglikelihood_burn=k_states
    )

    # Initialize the matrices
    self.ssm['design'] = np.array([1,1,1,1,1,1,1,1,1,1,1,1])
    self.ssm['transition'] = np.array([[1,0,0,0,0,0,0,0,0,0,0,0],
                                   [0,1,0,0,0,0,0,0,0,0,0,0],
                                   [0,0,1,0,0,0,0,0,0,0,0,0],
                                   [0,0,0,1,0,0,0,0,0,0,0,0],
                                   [0,0,0,0,1,0,0,0,0,0,0,0],
                                   [0,0,0,0,0,1,0,0,0,0,0,0],
                                   [0,0,0,0,0,0,1,0,0,0,0,0],
                                   [0,0,0,0,0,0,0,1,0,0,0,0],
                                   [0,0,0,0,0,0,0,0,1,0,0,0],
                                   [0,0,0,0,0,0,0,0,0,1,0,0],
                                   [0,0,0,0,0,0,0,0,0,0,1,0],
                                   [0,0,0,0,0,0,0,0,0,0,0,1]])
    self.ssm['selection'] = np.eye(k_states)
    self.ssm['state_intercept']=optimum_cost_sale_quarter_201604.values

    # Cache some indices
    self._state_cov_idx = ('state_cov',) + np.diag_indices(k_posdef)

@property
def param_names(self):
    return ['sigma2.measurement', 'sigma2.level', 'sigma2.trend']

@property
def start_params(self):
    return [np.std(self.endog)]*13

def transform_params(self, unconstrained):
    return unconstrained**2

def untransform_params(self, constrained):
    return constrained**0.5

def update(self, params, *args, **kwargs):
    params = super(LocalLinearTrend, self).update(params, *args, **kwargs)
    
    # Observation covariance
    self.ssm['obs_cov',0,0] = params[0]

    # State covariance
    self.ssm[self._state_cov_idx] = params[1:]

mod=LocalLinearTrend(sales_time_201604)
res1=mod.fit()
print (res1.summary())

@ChadFulton
Copy link
Member

I'm guessing that the problem is that with a diffuse prior, this model is not identified. Any of the 12 level series you have could be substituted for another one. You can probably fix the identification issue by initializing the series as known and equal to zero, e.g.:

super(LocalLinearTrend, self).__init__(
        endog, k_states=k_states, k_posdef=k_posdef)
self.ssm.initialize_known(np.zeros(k_states), np.zeros((k_states, k_states)))

What is the shape of optimum_cost_sale_quarter_201604.values?

@ChadFulton
Copy link
Member

You can probably fix the identification issue by initializing the series as known and equal to zero

(Note that I'm not sure that it is advisable to try to estimate 12 different random walk components for a single observable series, but my advice is in case you have some good reason for doing this)

@ZhonghuaGao
Copy link
Author

ZhonghuaGao commented Nov 15, 2018 via email

@ChadFulton
Copy link
Member

If optimum_cost_sale_quarter_201604 is just a scalar, then even initializing the states as zero, your model will not be identified, since the 12 random walk components are identical. I can't see what you're trying to get at with this model, so I don't have anything else to suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants