3

I have tried below simple code of PyMC3 in Python 3.7 in order to generate lambda value of exponential function.

But I am getting below error instead.

Could you please let me what the problem is?

Code

import pymc3 as pm

lambda_1 = pm.Exponential('lambda_1', 1)


lambda_2 = pm.Exponential('lambda_2', 1)

Error

TypeError: No model on context stack, which is needed to instantiate distributions. Add variable inside a 'with model:' block, or use the '.dist' syntax for a standalone distribution.

Jatin Singh Bhati
  • 133
  • 1
  • 3
  • 14
saul
  • 81
  • 10

2 Answers2

2

Try this:

import pymc3 as pm

with pm.Model() as model:
    lambda_1 = pm.Exponential('lambda_1', 1)
    lambda_1 = pm.Exponential('lambda_2', 1)

I guarantee that it will remove your error! Happy Coding!

merv
  • 67,214
  • 13
  • 180
  • 245
Jatin Singh Bhati
  • 133
  • 1
  • 3
  • 14
0

To also state the obvious:

import pymc3 as pm
lambda_1 = pm.Exponential.dist(lam=1)  
lambda_2 = pm.Exponential.dist(lam=1)

Also works fine because the dist class method returns a standalone distribution object that can be used outside of a PyMC model.

usεr11852
  • 285
  • 10
  • 19