Bayes tank problem HW02 ICFP 2019.py

From Werner KRAUTH

(Difference between revisions)
Jump to: navigation, search
Revision as of 23:55, 13 September 2016
Werner (Talk | contribs)

← Previous diff
Revision as of 14:03, 30 September 2018
Werner (Talk | contribs)
(Bayes tank problem HW02 ICFP 2016.py moved to Bayes tank problem HW02 ICFP 2018.py)
Next diff →

Revision as of 14:03, 30 September 2018

This is the program Bayes_tank.py that is useful for Homework 2 of the ICFP 2016 course in Statistical physics. Note that this program is written in Python 2.X. Note that the commented line allows you to toggle between the two subsequent questions in homework 2.

import random, math, pylab
histovec = []
Lambda = 0.1
while (len(histovec) < 1000):
    N = int(- math.log(random.uniform(0.0, 1.0)) / Lambda)
    if N >= 4:
        data = range(1, N + 1)
        random.shuffle(data)
        data[0: 4] = sorted(data[0:4])
#       if data[3] == 14: 
        if data[0: 4] == [1, 2, 4, 14]:
            histovec.append(N)
            print data
Nmax = max(histovec)
pylab.hist(histovec, range=(0, Nmax), bins=Nmax, normed=True)
pylab.title("Bayes approach to the tank problem, $\lambda = $" + str(Lambda))
pylab.xlabel('$N$')
pylab.ylabel('$p(N)$')
pylab.savefig('bayes_max.png')
pylab.show()