I am trying to combine cvxopt (an optimization solver) and PyMC3 (a sampler) to solve convex stochastic optimization problems.
I have tried to use the following code (PyMC, version 2) as a baseline. But I couldn't make it work.
Stochastic Optimization in Python
First, when I tried to just replacing pymc with pymc3, I got the following 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.
Then I add my code inside a "with" statement as follows, and got an "AttributeError".
AttributeError: module 'pymc3' has no attribute 'deterministic'
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)
I decided to comment "#@pymc3.deterministic", and then I got the following error:
AttributeError: module 'pymc3' has no attribute 'MCMC'
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:
c = co.matrix([float(c1), -5.]): TypeError: float() argument must be a string or a number, not 'FreeRV'
Let me know if you need more details.