Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Curved Random Distribution

by blokhead (Monsignor)
on Oct 18, 2005 at 17:08 UTC ( [id://501067]=note: print w/replies, xml ) Need Help??


in reply to Curved Random Distribution

If all you care about is that your probability distribution qualitatively looks like the classic bell-curve, you can use this quick-and-dirty approximation:
sub almost_normal { my ($mean, $variance) = @_; return sqrt($variance) * atan2( rand(2) - 1, 1 ) + $mean; }
The reason it works is because the normal distribution is shaped like exp(-x2). A reasonable first-order approximation of exp(x) is (1+x), so we can try using 1/(1+x2) to approximate the normal curve. This function's integral (accumulating probability distribution) is none other than arctan.

This gives you a bell-shaped curve in the shape of 1/(1+x2), which is qualitatively "bell-shaped", but has a lot more probability in its "tails" than the normal distribution.

Update: Augh, this is completely backwards, please ignore. In any case, it should be taking tangents of a random angle (or something?), but I can't get the scaling factors to work out right now. So much for a quick and easy alternative. ;)

Can someone who actually knows this math say (correctly) what I was trying to say? I want the accumulating probability distribution to be shaped like arctan.

Update 2: (trying to salvage this node) After thinking about it for a while, this seems to work (stealing code from BrowserUk's reply):

#!/usr/bin/perl -slw my $PI = 4*atan2(1,1); sub almost_normal { my ($mean, $variance) = @_; my $x = rand(2*$PI); return sqrt($variance) * sin($x)/cos($x) + $mean; } our $PRECISION ||= 1; our $ITERS = 1000 * $PRECISION; my %plot; $plot{ int( $PRECISION * almost_normal( 100, 5 ) ) / $PRECISION }++ for 1 .. $ITERS; print "Rand 100 +-5"; printf "%7.2f : %-3d : %s\n", $_, $plot{ $_ }, '#' x ( $plot{ $_ } / $ITERS * 100 * $PRECISION ) for sort{ $a <=> $b } keys %plot;
The variance is pretty wide, though ... you might be well-served to scale it down a bit. Although it's nice that the tails do extend really far.

blokhead

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://501067]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found