0
$\begingroup$

I'm performing Bayesian Analysis and I have written this piece of code

successes = 6; trials = 16;
samples = 20000;
(*prior = BinomialDistribution[trials, 0.333];*)
prior = UniformDistribution[{0, 1}];
priorsamples = RandomVariate[prior, samples];
model[theta_] := BinomialDistribution[trials, theta];
delta = 0.05;
bins = Table[i, {i, 0, 1, delta}];
priorbin = BinCounts[priorsamples, {bins}];
generated = {priorsamples, 
    RandomVariate[#] & /@ (model[#] & /@ priorsamples)}\[Transpose];
posteriorsamples = (Select[
      generated, #[[2]] == successes &]\[Transpose])[[1]];
posteriorbins = BinCounts[posteriorsamples, {bins}];
Show[
 Plot[-1, {x, 0, 1}],
 Graphics[{EdgeForm[Black], FaceForm[{Opacity[0.5], Red}], 
   Rectangle[{#[[1]], 
       0}, {#[[1]] + delta, #[[2]]/samples/
        delta}] & /@ ({bins[[1 ;; -2]], priorbin}\[Transpose])}],
 Graphics[{EdgeForm[Black], FaceForm[{Opacity[0.5], Blue}], 
   Rectangle[{#[[1]], 
       0}, {#[[1]] + delta, #[[2]]/Length[posteriorsamples]/
        delta}] & /@ ({bins[[1 ;; -2]], 
       posteriorbins}\[Transpose])}],
 PlotRange -> {{0, 1}, {0, 5}}
 ]

using UniformDistribution I have no problem. Using BinomialDistribution (uncomment the corresponding line) I got the following error

enter image description here

that should occur when Mathematica computes generated by processing RandomVariate and Map. I'm runnning version 10.3 under Ubuntu 16.04. Could you reproduce this error?

Thanks for the help!

$\endgroup$
  • 1
    $\begingroup$ Yes, I reproduce the error. Is it so strange? If prior = BinomialDistribution[trials, 0.333] then sampling from prior will certainly give integers, and almost certainly some larger than 1. You then feed these samples back to model as the probabilities p for a single "success" in a binomial sampling. Is this really intended? It does not make sense to me. $\endgroup$ – Marius Ladegård Meyer Apr 19 '17 at 19:57
  • 1
    $\begingroup$ you're right. I did not mean to do that, I meant feeding a beta! Now I see. $\endgroup$ – Fabio Apr 19 '17 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.