Reputation: 71
Since it is not a math related question but about using a library to do symbolic computation, SO is better suited to answer this than {math|stats}.stackexchange.com
I want to use Sympy to calculate the following:
This is the code I'm using in Sympy
from sympy.stats import Normal, density, DiscreteUniform, P, given
from sympy import Symbol, pprint, symbols, Symbol, Eq
sigma = Symbol("sigma", positive=True)
mu = DiscreteUniform('mu', [1,2])
N = Normal('normal', mu, sigma)
sampling_dist = given(N, Eq(mu, 1))
prior = P(Eq(mu, 1))
marginal = P(Eq(mu, 1))*given(N, Eq(mu, 1))+P(Eq(mu, 2))*given(N, Eq(mu,2))
post = prior * sampling_dist / marginal
Now I want to
I tried to print the equation by asking the density with
density(post)(Symbol('x'))
And I get the following error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-9b0e1c9ada72> in <module>()
----> 1 density(post)(Symbol('x'))
/Users/alexis/anaconda3/lib/python3.4/site-packages/sympy/stats/rv.py in density(expr, condition, evaluate, numsamples, **kwargs)
692 **kwargs)
693
--> 694 return Density(expr, condition).doit(evaluate=evaluate, **kwargs)
695
696
/Users/alexis/anaconda3/lib/python3.4/site-packages/sympy/stats/rv.py in doit(self, evaluate, **kwargs)
643 isinstance(pspace(expr), SinglePSpace)):
644 return expr.pspace.distribution
--> 645 result = pspace(expr).compute_density(expr, **kwargs)
646
647 if evaluate and hasattr(result, 'doit'):
/Users/alexis/anaconda3/lib/python3.4/site-packages/sympy/stats/rv.py in pspace(expr)
430 return rvs[0].pspace
431 # Otherwise make a product space
--> 432 return ProductPSpace(*[rv.pspace for rv in rvs])
433
434
/Users/alexis/anaconda3/lib/python3.4/site-packages/sympy/stats/rv.py in __new__(cls, *spaces)
278 # Overlapping symbols
279 if len(symbols) < sum(len(space.symbols) for space in spaces):
--> 280 raise ValueError("Overlapping Random Variables")
281
282 if all(space.is_Finite for space in spaces):
ValueError: Overlapping Random Variables
Even if I subs sigma with a constant I get the same error. I (obviouslu) don't understand what I'm doing wrong.
Upvotes: 3
Views: 1525
Reputation: 398
I don't fully understand what you're trying to accomplish, but I ran your code in a Jupyter notebook and examined the output of post
with sympy.init_printing() enabled, which displays the following:
The exception that gets thrown, ValueError: Overlapping Random Variables
, suggests that SymPy simply doesn't know what to do with the expression. I suggest you double-check the construction of your expression, testing the output in a more piecemeal way.
Upvotes: 0