Error when using PyMC3's Exponential function
Question:
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.
Answers:
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!
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.
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.
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!
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.