PHY 411-506 Home Home  |  Course Outline  |  Lectures  |  Homework  |  Files

Lecture 3: January 22


Darts and the Poisson distribution

The following applet animates problem 7.4 on page 191:

This applet is generated by Darts.java:

// Darts.java

import comphys.*;
import comphys.graphics.*;

public class Darts extends Animation {

    int L = 100;                // equal sized regions on dart board
    int N = 50;                 // number of darts
    int nTrial = 1000;          // number of trials of N throws each

    int[] B;                    // dart board
    int[] H;                    // histogram of hits in one trial
    int[] SumH;                 // sum of histograms
    double[] P;                 // probability distribution
    double nAverage;            // 
    double nSquaredAverage;     // 
    int darts = 0;              // darts thrown
    int trials = 0;             // trials completed

    void throwDart () {
        int i = (int) (L * Math.random());
        ++B[i];
        ++darts;
    }

    void histogram () {
        // compute H(n) for this trial
        for (int i = 0; i < L; i++) {
            int n = B[i];
            ++H[n];
        }
        // accumulate H(n) values
        double norm = 0;
        for (int n = 0; n <= N; ++n) {
            SumH[n] += H[n];
            norm += SumH[n];
        }
        // compute averages
        nAverage = nSquaredAverage = 0;
        for (int n = 0; n <= N; n++) {
            P[n] = SumH[n] / norm;
            nAverage += n * P[n];
            nSquaredAverage += n * n * P[n];
        }
    }

    void resetBoard () {
        for (int n = 0; n <= N; n++)
            H[n] = 0;
        for (int i = 0; i < L; i++)
            B[i] = 0;
        ++trials;
        darts = 0;
    }

    void initial () {
        B = new int[L];
        H = new int[N + 1];
        SumH = new int[N + 1];
        P = new double[N + 1];
        nAverage = nSquaredAverage = 0;
        darts = trials = 0;
    }

    class Board extends Plot {
        Board () {
            setSize(300, 300);
            setBackground("lightGray");
        }

        String[] colors = {"white", "green", "blue", "red",
                           "yellow", "cyan", "magenta", "black"};

        public void paint () {
            clear();
            int k = 1;
            while (k * k <= L)
                ++k;
            setWindow(0, k, 0, k);
            k = (int) Math.sqrt(L);
            for (int i = 0; i < L; i++) {
                int y = (int) (i / (double) k);    // row
                int x = i % k;                     // column
                int color = B[i];
                if (color >= colors.length)
                    color = colors.length - 1;
                setColor(colors[color]);
                boxArea(x + 0.05, x + 0.95, y + 0.05, y + 0.95);
            }
        }
    }

    class Graph extends Plot {
        Graph () {
            setSize(300, 300);
            setBackground("white");
        }

        public void paint () {
            clear();
            double yMax = 0.01;
            int nMax = 0;
            for (int n = 0; n <= N; n++) {
                if (P[n] > yMax)
                    yMax = P[n];
                if (P[n] > 0)
                    nMax = n;
            }
            setColor("black");
            drawAxes(0, nMax + 1, 0, 1.2 * yMax);
            setColor("cyan");
            for (int n = 0; n <= nMax; n++) {
                boxArea(n, n + 1, 0, P[n]);
            }
            setColor("blue");
            double x = 0.6 * (nMax + 1);
            double y = 1.1 * yMax;
            double dy = 0.1 * yMax;
            plotString("Trial = " + trials, x, y);
            y -= dy;
            plotString(" = " + Easy.format(nAverage, 4), x, y);
            y -= dy;
            plotString(" = " + Easy.format(nSquaredAverage, 4), x, y);
            y -= dy;
            double sigma = Math.sqrt(nSquaredAverage - nAverage * nAverage);
            plotString("Std Dev = " + Easy.format(sigma, 4), x, y);
            setColor("magenta");
            plotString("P(n) vs. n", 0, 1.25 * yMax); 

            // plot Poisson distribution
            setColor("red");
            double p0 = 0;
            for (int n = 0; n < nMax; n++) {
                double p = Math.pow(nAverage, n) * Math.exp(-nAverage);
                for (int i = 1; i <= n; i++)
                    p /= i;
                plotLine(n, p0, n, p);
                plotLine(n, p, n + 1, p);
                p0 = p;
            }
        }
    }

    Board board;
    Graph graph;
    Reader LReader, NReader, nTrialReader;

    boolean fast;
    class FastButton extends HotButton {
        public void action () {
            fast = !fast;
            if (fast)
                fastButton.setLabel("Slow");
            else
                fastButton.setLabel("Fast");
        }
    }
    FastButton fastButton;

    public void init () {
        initial();
        add(board = new Board());
        add(graph = new Graph());
        add(LReader = new Reader("Regions L =", L));
        add(NReader = new Reader("Darts N =", N));
        add(nTrialReader = new Reader("# Trials", nTrial));
        add(fastButton = new FastButton());
        fastButton.setLabel("Fast");
        addControlPanel();
    }

    public void step () {
        if (darts == N)
            resetBoard();
        if (trials == nTrial)
            stopAnimation();
        else {
            if (fast)
                while (darts < N - 1)
                    throwDart();
            throwDart();
            board.repaint();
            if (darts == N) {
                histogram();
                graph.repaint();
            }
        }
    }

    public void reset () {
        L = LReader.readInt();
        N = NReader.readInt();
        nTrial = nTrialReader.readInt();
        initial();
        board.repaint();
        graph.repaint();
    }

    public static void main (String[] args) {
        Darts darts = new Darts();
        darts.frame("Darts and the Poisson distribution", 630, 450);
    }
}

Another Poisson applet

"Poisson" means "Fish" in French. Click here for an applet which simulates the distribution of numbers of fish observed in an area of ocean in successive one-hour time periods.

The Poisson distribution is actually named for Siméon Denis Poisson, who is also known for Poisson's equation in electrostatics and Poisson brackets in classical mechanics.


UB Physics Home Questions or comments: phygons@acsu.buffalo.edu