Algorithms for Graphical Models (AGM)

Practical 07: Parameter estimation


  1. Coin tossing
  2. Maximum likelihood estimation
  3. Bayesian parameter estimation

Coin tossing

Without using any gPy modules, write a Python program coin.py which simulates tossing a biassed coin an arbitrary number of times. The probability of heads and the number of tosses are specified on the command line. So that python coin.py 0.4 1000 is an acceptable way of running the program. The output should be written to standard output.

Hint: Look at argv in the Python sys module. Also look at the random module.

Solution


Maximum likelihood estimation

Alter coin.py so that it outputs a `running estimate' for the probability of heads using the sample it is creating. So after each sampled toss a new estimate is produced and written on a new line of your output. Use gnuplot to get a plot. If your running estimate is in a file called runests.txt, you can do the following. Start up gnuplot (just call it from the command line) and type plot 'runests.txt' with lines at the gnuplot prompt.

Solution


Bayesian parameter estimation

Have a look at beta.py which provides a rudimentary function for plotting Beta distribution curves. Alter your coin-tossing program so that now it generates a Beta distribution for the probability of heads after each coin toss. The prior α and β values should be provided on the command line, along with the true probability and the number of tosses to simulate.

Solution


Last modified: Fri Dec 11 09:14:51 GMT 2009

Valid XHTML 1.1!