0
|
1 |
#======================================================================
|
|
2 |
# L I B R W A L K . P L
|
|
3 |
# doc: Tue Oct 5 21:34:37 2010
|
|
4 |
# dlm: Tue Oct 5 21:47:28 2010
|
|
5 |
# (c) 2010 A.M. Thurnherr
|
|
6 |
# uE-Info: 26 26 NIL 0 0 72 2 2 4 NIL ofnI
|
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
sub fac($)
|
|
10 |
{ return ($_[0] < 2) ? $_[0] : $_[0]*fac($_[0]-1); }
|
|
11 |
|
|
12 |
#----------------------------------------------------------------------
|
|
13 |
# From: http://mathworld.wolfram.com/RandomWalk1-Dimensional.html
|
|
14 |
# Let N steps of equal length be taken along a line. Let p be the
|
|
15 |
# probability of taking a step to the right, q the probability of taking a
|
|
16 |
# step to the left, n1 the number of steps taken to the right, and n2 the
|
|
17 |
# number of steps taken to the left.
|
|
18 |
# The following calculates the probability of taking exactly n1 steps out
|
|
19 |
# of N (= n1+n2) to the right.
|
|
20 |
#----------------------------------------------------------------------
|
|
21 |
|
|
22 |
sub pNSteps(@)
|
|
23 |
{
|
|
24 |
my($n1,$N,$p,$q) = @_;
|
|
25 |
$p = $q = 0.5 unless defined($p);
|
|
26 |
return fac($n1+($N-$n1)) / (fac($n1)*fac($N-$n1)) * $p**$n1 * $q**($N-$n1);
|
|
27 |
}
|
|
28 |
|
|
29 |
1;
|