title: How to perform Stochastic Optimization using PyMC3?
body: <p>I am trying to combine cvxopt (an optimization solver) and PyMC3 (a sampler) to solve convex stochastic optimization problems.</p>
  
  <p>I have tried to use the following code (PyMC, version 2) as a baseline. But I couldn't make it work.</p>
  
  <p><a href="https://stackoverflow.com/questions/25076711/stochastic-optimization-in-python">Stochastic Optimization in Python</a></p>
  
  <p>First, when I tried to just replacing pymc with pymc3, I got the following error:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Then I add my code inside a "with" statement as follows, and got an "AttributeError".</p>
  
  <blockquote>
    <p>AttributeError: module 'pymc3' has no attribute 'deterministic'</p>
  </blockquote>
  
  <pre><code>import numpy as np, pymc3, cvxopt as co
  
  # suppress cvxopt solver output, since it will be inside MCMC loop
  co.solvers.options['show_progress'] = False
  
  
  with pymc3.Model() as model:
      c1 = pymc3.Normal('c1', mu=-4, tau=0.5**-2)
  
      @pm.deterministic
      def x(c1=c1):
          c = co.matrix([float(c1), -5.])
          G = co.matrix([[2., 1., -1., 0.], [1., 2., 0., -1.]])
          h = co.matrix([3., 3., 0., 0.])
          sol = co.solvers.lp(c, G, h)
          solution = np.array(sol['x'],dtype=float).flatten()
          return solution
  
      m = pymc3.MCMC(dict(c1=c1, x=x))
      m.sample(20000, 10000, 10)
  </code></pre>
  
  <p>I decided to comment "#@pymc3.deterministic", and then I got the following error:</p>
  
  <blockquote>
    <p>AttributeError: module 'pymc3' has no attribute 'MCMC'</p>
  </blockquote>
  
  <p>So, I am stuck at this point. I don't know what would be the equivalent of calling MCMC using pymc3. Everything I tried so far is giving me the following message:</p>
  
  <blockquote>
    <p>c = co.matrix([float(c1), -5.]): TypeError: float() argument must be a string or a number, not 'FreeRV'</p>
  </blockquote>
  
  <p>Let me know if you need more details.</p>