Weblog

Thursday, 22 October 2009

  • Troy and B.S. expertise go hand in hand

    I am inspired by this wise xkcd comic strip.
    Troy's Heuristic for English Language Survival:  "In regards to literary criticism, smoothly executed B.S. may be considered sufficiently indistinguishable from the real deal -- at least for the purpose of finishing that darn essay in the wee hours of the morning."
    (To an extent, this is the humanities equivalent of POGE.)

Thursday, 03 September 2009

  • Probability Sampling

    The homework today for AP Statistics was to toss a coin a hundred times and then record the result.  Hmm ... what busy work (for a computer).
    #!/usr/bin/perl -w
    # Usage: ./$0 $sample_size $event_name $probability [$
    event_name $probability ..]
    use strict;

    my @tmp = @ARGV;
    die "Not enough arguments" if scalar(@tmp) < 3;
    my $total = shift @tmp;
    my %
    events = @tmp;
    my $sum = 0;
    for (values %
    events) {
    $sum += $_;
    }
    die "Sum of probabilities $sum != 1" unless $sum == 1;

    my @keys = sort keys(%events);
    for(my $count = 1; $count <= $total; $count++) {
    my $result = rand;
    my $lower = 0;
    my $name = undef;
    foreach (@keys) {
    my $upper = $lower +
    $events{$_};
    if($lower <= $result && $result < $upper) {
    $name = $_;
    last;
    }
    else {
    $lower = $upper;
    }
    }
    die "Out of range" unless defined $name;
    print "${count}: ${name}\n";
    }
    exit 0;

    In all: ./randsamp 100 heads 0.5 tails 0.5
    The Perl script can work with a variable number of arguments, assuming that the first denotes the sample size and the rest are pairs of event names and probabilities.  Of course, the sum of the probabilities must exactly equal 1 or else the script dies.
    Remember to adjust the shebang line to point to the path of your Perl installation.

Saturday, 23 May 2009

Friday, 22 May 2009

Saturday, 09 May 2009

A0u

  • Visit A0u's Xanga Site
    • Name: A0u
    • Gender: Male
    • Member Since: 8/21/2007

Weblog Archives

Don't worry - your calendar is here… to see it in action just click "Save" above and refresh the page.