title: Conditional probability with sympy
body: <p>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</p>
  
  <p>I want to use Sympy to calculate the following:</p>
  
  <p><a href="https://i.stack.imgur.com/0U6nc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0U6nc.png" alt="posterior equation"></a></p>
  
  <p>This is the code I'm using in Sympy</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Now I want to </p>
  
  <ol>
  <li>be able to print the equation for the posterior distribution (I expect sigma and x to be the only unknown)</li>
  <li>Plot the posterior by fixing sigma with a known value</li>
  </ol>
  
  <p>I tried to print the equation by asking the density with</p>
  
  <pre><code>density(post)(Symbol('x'))
  </code></pre>
  
  <p>And I get the following error</p>
  
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-6-9b0e1c9ada72&gt; in &lt;module&gt;()
  ----&gt; 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 
  --&gt; 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
  --&gt; 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
  --&gt; 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) &lt; sum(len(space.symbols) for space in spaces):
  --&gt; 280             raise ValueError("Overlapping Random Variables")
      281 
      282         if all(space.is_Finite for space in spaces):
  
  ValueError: Overlapping Random Variables
  </code></pre>
  
  <p>Even if I subs sigma with a constant I get the same error. I (obviouslu) don't understand what I'm doing wrong.</p>