0

I have defined a log-likelihood function and I have one variable being sampled over a uniform distribution. I made sure the log-likelihood function returns the same result for same input. But when I sample, every time the distribution is somewhat different (over the same range).

What is going on?

import pymc3 as mc
import theano.tensor as tt

SAMPLES = 1000
TUNING_SAMPLES = 100
N_CORES = 10
N_CHAINS = 2

#(logl_ThetaFromChoices is defined above with the input)

# use PyMC3 to sampler from log-likelihood
with mc.Model() as modelFindTheta:
    theta = mc.Uniform('theta', lower=-200.0, upper=200.0)

    # convert m and c to a tensor vector
    theta = tt.as_tensor_variable(theta)

    def callOp(v):
        return logl_ThetaFromChoices(v)
    mc.DensityDist('logl_ThetaFromChoices', callOp, observed={'v': theta})

    step1 = mc.Metropolis()
    trace_theta = mc.sample(SAMPLES,
                            tune=TUNING_SAMPLES,
                            discard_tuned_samples=True,
                            chains=N_CHAINS,
                            cores=N_CORES,
                            step=step1)

'alpha' == Theta here

enter image description here

$\alpha$

merv
  • 67,214
  • 13
  • 180
  • 245
user1581390
  • 1,900
  • 5
  • 25
  • 38

1 Answers1

2

Since it involves random number generation, one needs to set a seed to obtain reproducible results. For PyMC3, this is done with the random_seed argument in the pymc3.sampling.sample() method.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Where can I find a clear description of the algorithms they use? I don't follow how the result would change. The distribution is embedded in the data itself, not the method...? – user1581390 Nov 20 '19 at 23:55
  • @user1581390 sorry I'm not sure I follow what is confusing you. By definition you are calling a method to generate random samples from a probabilistic model (`theta` is a random variable). MCMC generally creates random sequences using evaluations of posterior likelihoods, and Metropolis-Hastings is the particular algorithmic implementation you used. – merv Nov 21 '19 at 07:55