lindermanlab / ssm

Bayesian learning and inference for state space models
MIT License
540 stars 196 forks source link

LinAlg error when sampling from trained hmm #130

Open vcorbit opened 3 years ago

vcorbit commented 3 years ago

Hi,

I have an HMM that I trained and tested on some joint data. I'm trying now to sample from the HMM to simulate data and test some further analyses. However, every time I run hmm.sample(timebins) with my model, I get this error:

originalstates, sampledobs = trainedhmm.sample(timebins) t:\updatedssm\ssm\ssm\observations.py:491: RuntimeWarning: divide by zero encountered in true_divide sqrt_Sigma = np.linalg.cholesky(Sigmas[z] / tau) if with_noise else 0 Traceback (most recent call last):

File "", line 1, in originalstates, sampledobs = trainedhmm.sample(timebins)

File "t:\updatedssm\ssm\ssm\hmm.py", line 221, in sample data[0] = self.observations.sample_x(z[0], data[:0], input=input[0], with_noise=with_noise)

File "t:\updatedssm\ssm\ssm\observations.py", line 491, in sample_x sqrt_Sigma = np.linalg.cholesky(Sigmas[z] / tau) if with_noise else 0

File "C:\ProgramData\Anaconda3\lib\site-packages\autograd\tracer.py", line 48, in f_wrapped return f_raw(*args, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 759, in cholesky r = gufunc(a, signature=signature, extobj=extobj)

File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 100, in _raise_linalgerror_nonposdef raise LinAlgError("Matrix is not positive definite")

LinAlgError: Matrix is not positive definite

When I create a new HMM from scratch using the same K and D values, I don't get this error. So it seems like something is going wrong with my trained HMM but I'm not sure what it could be, since the trained HMM works fine for other functions like getting the LL or predicting states on new observations. Not sure if this is a bug or something I'm doing wrong on my end.

Any advice you could give would be greatly appreciated!!

Thanks!