Bayes tank problem HW02 ICFP 2019.py

From Werner KRAUTH

Revision as of 14:56, 2 October 2019; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

This is the program Bayes_tank.py that is useful for Homework 2 of the ICFP 2019 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()
Personal tools