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

Chapter 7 Error - LinAlgError: Matrix is not positive definite #76

Closed
gbrunkhorst opened this issue Jan 18, 2021 · 2 comments
Closed

Chapter 7 Error - LinAlgError: Matrix is not positive definite #76

gbrunkhorst opened this issue Jan 18, 2021 · 2 comments

Comments

@gbrunkhorst
Copy link

Hi - when I run following code from Chapter 7:

X_new = np.linspace(np.floor(x_1.min()), np.ceil(x_1.max()), 200)[:, None]
with model_iris:
    f_pred = gp.conditional('f_pred', X_new)
    pred_samples = pm.sample_posterior_predictive(
        trace_iris, vars=[f_pred], samples=1000)

I get the following error:

LinAlgError: Matrix is not positive definite

Let me know if you have any thoughts. fpred seems to be all zeros and I'm not sure how to fix it. I'm on Windows.

This is an outstanding book, thanks!

Here is the whole error message if helpful:

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
<ipython-input-27-47a0dbdef087> in <module>
      4     f_pred = gp.conditional('f_pred', X_new)
      5     pred_samples = pm.sample_posterior_predictive(
----> 6         trace_iris, vars=[f_pred], samples=1000)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\sampling.py in sample_posterior_predictive(trace, samples, model, vars, var_names, size, keep_size, random_seed, progressbar)
   1730                 param = _trace[idx % len_trace]
   1731 
-> 1732             values = draw_values(vars, point=param, size=size)
   1733             for k, v in zip(vars, values):
   1734                 ppc_trace_t.insert(k.name, v, idx)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in draw_values(params, point, size)
    781                     # This may fail for autotransformed RVs, which don't
    782                     # have the random method
--> 783                     value = _draw_value(next_, point=point, givens=temp_givens, size=size)
    784                     givens[next_.name] = (next_, value)
    785                     drawn[(next_, size)] = value

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\distribution.py in _draw_value(param, point, givens, size)
    945             return point[param.name]
    946         elif hasattr(param, "random") and param.random is not None:
--> 947             return param.random(point=point, size=size)
    948         elif (
    949             hasattr(param, "distribution")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\model.py in __call__(self, *args, **kwargs)
    104 
    105     def __call__(self, *args, **kwargs):
--> 106         return getattr(self.obj, self.method_name)(*args, **kwargs)
    107 
    108 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\distributions\multivariate.py in random(self, point, size)
    278 
    279         if self._cov_type == "cov":
--> 280             chol = np.linalg.cholesky(param)
    281         elif self._cov_type == "chol":
    282             chol = param

<__array_function__ internals> in cholesky(*args, **kwargs)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\linalg\linalg.py in cholesky(a)
    762     t, result_t = _commonType(a)
    763     signature = 'D->D' if isComplexType(t) else 'd->d'
--> 764     r = gufunc(a, signature=signature, extobj=extobj)
    765     return wrap(r.astype(result_t, copy=False))
    766 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_nonposdef(err, flag)
     89 
     90 def _raise_linalgerror_nonposdef(err, flag):
---> 91     raise LinAlgError("Matrix is not positive definite")
     92 
     93 def _raise_linalgerror_eigenvalues_nonconvergence(err, flag):
@alexander-pv
Copy link

alexander-pv commented Aug 9, 2021

Hello, @gbrunkhorst,

I faced the same issue. You can try the trick with WhiteNoise kernel suggested further in the book during model building:
cov=pm.gp.cov.ExpQuad(1, l) + pm.gp.cov.WhiteNoise(1e-5)

@gbrunkhorst
Copy link
Author

gbrunkhorst commented Aug 9, 2021 via email

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

No branches or pull requests

3 participants