Bayes tank problem HW02 ICFP 2019.py
From Werner KRAUTH
(Difference between revisions)
Revision as of 14:55, 2 October 2019 Werner (Talk | contribs) (Bayes tank problem HW02 ICFP 2018.py moved to Bayes tank problem HW02 ICFP 2019.py) ← Previous diff |
Current revision Werner (Talk | contribs) |
||
Line 1: | Line 1: | ||
- | 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. | + | 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 | import random, math, pylab |
Current revision
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()