How to implement simple Monte Carlo function in pymc
<p>I'm trying to get my head around how to implement a Monte Carlo function in python using pymc to replicate a spreadsheet by Douglas Hubbard in his book <a href="http://www.howtomeasureanything.com/wp-content/uploads/2015/03/HTMA_3rd_Ch6.xlsx" rel="nofollow">How to Measure Anything</a></p>

<p>My attempt was:</p>

<pre><code>import numpy as np
import pandas as pd
from pymc import DiscreteUniform, Exponential, deterministic, Poisson, Uniform, Normal, Stochastic, MCMC, Model


maintenance_saving_range = DiscreteUniform('maintenance_saving_range', lower=10, upper=21)
labour_saving_range = DiscreteUniform('labour_saving_range', lower=-2, upper=9)
raw_material_range = DiscreteUniform('maintenance_saving_range', lower=3, upper=10)
production_level_range = DiscreteUniform('maintenance_saving_range', lower=15000, upper=35000)



@deterministic(plot=False)
def rate(m = maintenance_saving_range, l = labour_saving_range, r=raw_material_range, p=production_level_range):
    return (m + l + r) * p

model = Model([rate, maintenance_saving_range, labour_saving_range, raw_material_range, production_level_range])
mc = MCMC(model)
</code></pre>

<p>Unfortunately, I'm getting an error: <code>ValueError: A tallyable PyMC object called maintenance_saving_range already exists. This will cause problems for some database backends.</code></p>

<p>What have I got wrong?</p>

<p>Ah, it was a copy and paste error. </p>

<p>I'd called three distributions by the same name.</p>

<p>Here's the code that works.</p>

<pre><code>import numpy as np
import pandas as pd
from pymc import DiscreteUniform, Exponential, deterministic, Poisson, Uniform, Normal, Stochastic, MCMC, Model
%matplotlib inline
import matplotlib.pyplot as plt

maintenance_saving_range = DiscreteUniform('maintenance_saving_range', lower=10, upper=21)
labour_saving_range = DiscreteUniform('labour_saving_range', lower=-2, upper=9)
raw_material_range = DiscreteUniform('raw_material_range', lower=3, upper=10)
production_level_range = DiscreteUniform('production_level_range', lower=15000, upper=35000)


@deterministic(plot=False, name="rate")
def rate(m = maintenance_saving_range, l = labour_saving_range, r=raw_material_range, p=production_level_range):
    #out = np.empty(10000)
    out = (m + l + r) * p
    return out
model = Model([rate, maintenance_saving_range, labour_saving_range, raw_material_range])
mc = MCMC(model)
mc.sample(iter=10000)
</code></pre>

python pymc