PowerBASIC Forums
  Cafe PowerBASIC
  Dicing With Probability (Page 4)

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone!
This topic is 10 pages long:   1  2  3  4  5  6  7  8  9  10 
next newest topic | next oldest topic
Author Topic:   Dicing With Probability
James Graham-Eagle
Member
posted January 25, 2007 04:23 PM     Click Here to See the Profile for James Graham-Eagle     Edit/Delete Message   Reply w/Quote
What's really been discussed here is the Weak Law of Large Numbers ...
in this context it says that, as you toss the coin more and more the
probability that the average number of heads will deviate from 0.5 by
more than any fixed amount (no matter how small) tends to zero.

See http://mathworld.wolfram.com/WeakLawofLargeNumbers.html

------------------

IP: Logged

David Roberts
Member
posted January 26, 2007 03:07 AM     Click Here to See the Profile for David Roberts     Edit/Delete Message   Reply w/Quote
I cannot help thinking that this is stating the obvious. That is as the sample approaches the population then the sample mean approaches the population mean. What else would it do?

[This message has been edited by David Roberts (edited January 26, 2007).]

IP: Logged

Emil Menzel
Member
posted January 26, 2007 12:50 PM     Click Here to See the Profile for Emil Menzel     Edit/Delete Message   Reply w/Quote
I'd say that we have strayed into the domain of the Law of Large
Numbers but that Charles' opening problem was the Binomial
Theorem.

The Wolfram web site on the Weak Law of Large Numbers also lists
a number of other interesting sites, including:

The Law of Truly Large Numbers: "With a large enough sample, any
outrageous thin is likely to happen..."

The Frivolous Theorem of Arithmetic: "Almost all natural numbers are
very, very, very large numbers."

None of the above, coincidentally, is a joke.

But if we consider "50-50" to be a natural number 0.5..... it is
among those numbers that so qualify as very large and indeed it may
be said to have an infinite number of decimal places. And in
that case the Law of Large Numbers gets into trouble, does it not?
I.e., can even an infinitely large random sample of data arrive
at an infinitely precise estimate of 0.5....? Two decimal places
was good enough for Grandpa and it is usually good enough for me if
I'm talking about everyday things, but that is irrelevant in the
"hard sciences" or math.

Here's a quote from one theoretical physicist that I think relevant.
It should, however, be rated "PG" if not "R" for David since it
does drop the G** word once.
http://edge.org/q2005/q05_8.html#susskind

------------------

IP: Logged

Emil Menzel
Member
posted January 26, 2007 12:53 PM     Click Here to See the Profile for Emil Menzel     Edit/Delete Message   Reply w/Quote
[delete this post, please]

[This message has been edited by Emil Menzel (edited January 26, 2007).]

IP: Logged

Charles Pegge
Member
posted January 26, 2007 12:54 PM     Click Here to See the Profile for Charles Pegge     Edit/Delete Message   Reply w/Quote
Generating PI by tossing coins

where p= probability of getting exactly 50:50 in n flips

given
p=(2/(pi*n))^.5
and
p=n!/( (n!/2)^2 * (2^n) )

n*pi/2 = ( ( (n/2)^2*2^n )/n! )^2

pi = ( ( (n/2)!^2*2^n ) / n! )^2*2/n

Since the PC gags on large factorials very rapidly, this has to be turned into a
computer-friendly algorithm. With n>=100,000 how would you do this in PB?

Hint: use logs wherever you can!

------------------
www.pegge.net

IP: Logged

David Roberts
Member
posted January 26, 2007 01:21 PM     Click Here to See the Profile for David Roberts     Edit/Delete Message   Reply w/Quote
quote:
It should, however, be rated "PG" if not "R" for David since it does drop the G** word once.

Emil, you are a gentleman. I immediately broke out the garlic. The usage, however, was benign and I didn't suffer any after effects.

IP: Logged

David Roberts
Member
posted January 26, 2007 01:41 PM     Click Here to See the Profile for David Roberts     Edit/Delete Message   Reply w/Quote
sqrt(2/(pi*n)) was put forward as a good approximation for large n.

Equally, given a probability then the equation will give a good approximation to n.

I wouldn't have thought it useful in anyway for estimating pi. There are other methods which would give more accurate results and tax the CPU very much less.

IP: Logged

Charles Pegge
Member
posted January 26, 2007 01:49 PM     Click Here to See the Profile for Charles Pegge     Edit/Delete Message   Reply w/Quote
David, I agree, this is a most inefficient way of estimating pi, it
was more of a philosophical quest.

By the way, there are 2 G words in the article, are you still feeling alright?

------------------
www.pegge.net

IP: Logged

David Roberts
Member
posted January 26, 2007 08:12 PM     Click Here to See the Profile for David Roberts     Edit/Delete Message   Reply w/Quote
The first 'G' word was used with 'g' in lower case and used as an exclamation - they come in below the radar and are innocuous.

IP: Logged

Emil Menzel
Member
posted January 27, 2007 11:49 AM     Click Here to See the Profile for Emil Menzel     Edit/Delete Message   Reply w/Quote
Charles:
Neat formula! I have seen a dozen or more simulated coin-tossing
routines on various web sites, but nothing like your formula.
By all means push it farther. Efficiency is for business
administrators, not scholars. Indeed, I once saw a web site that
boasted the least efficient known formula for computing Pi. It
would be interesting to know whether methods based on randomization
could surpass that Holy Grail... As a once-famous philosopher once
said, Yesterday's metaphysics is today's physics and tomorrow's
common-sense.


'Pi_est.BAS
'Language: PBCC4.02
'Note that N must be even or answer will be zero
'Max N is not much more than 10,000
'but Stirling's estimate of log factorial might improve on that.
'Try different values of N & draw a graph of the program's various
'estimates of N. And of course remember the Law of Large Numbers.

#COMPILE EXE
#DIM ALL

DECLARE FUNCTION LogFactorial(num AS EXT) AS EXT
DECLARE FUNCTION PiEst(N AS EXT) AS EXT
FUNCTION PBMAIN () AS LONG
DIM A AS STRING, N AS EXT, Pi AS EXT, Pi2 AS EXT
LOCATE 10,1
LINE INPUT "N? ";A$
N=VAL(A$)
Pi=ATN(1)*4
a$ = USING$("&=#.##############", "PB's best Pi", Pi)
' returns "Pi=3.14159265358979"
PRINT A$

Pi2=PiEst(N)
PRINT
PRINT
PRINT "Pi by Charles Pegge's finagle factor"
a$ = USING$("&=#.##############", "Estimated Pi", Pi2)
PRINT A$
WAITKEY$
END FUNCTION

FUNCTION PiEst (N AS EXT) AS EXT
DIM LogPi AS EXT
DIM X1 AS EXT,X2 AS EXT,X3 AS EXT, X4 AS EXT, X5 AS EXT
X1=LogFactorial(N/2)
X2=LOG(2^N)
X3=LogFactorial(N)
X4=((X1*2+X2)- X3)
X5=LOG(2/N)
LogPi=X4*2+X5
'pi = ( ( (n/2)!^2*2^n ) / n! )^2*2/n 'cf Charles Pegge PB forums
FUNCTION=EXP(LogPi)
END FUNCTION

FUNCTION LogFactorial(Num AS EXT) AS EXT
DIM I AS EXT, X AS EXT, XX AS EXT
XX=0
FOR I=1 TO Num
XX=XX+LOG(I)
NEXT
FUNCTION=XX
END FUNCTION

------------------

IP: Logged

James Graham-Eagle
Member
posted January 27, 2007 12:26 PM     Click Here to See the Profile for James Graham-Eagle     Edit/Delete Message   Reply w/Quote
The formula given by Charles above is known as Wallis's Porduct
and can be written in the delightful form ...

2 2 4 4 6 6 8 8
- - - - - - - - ... = pi/2
1 3 3 5 5 7 7 9

See http://mathworld.wolfram.com/WallisFormula.html

------------------

IP: Logged

Emil Menzel
Member
posted January 27, 2007 12:49 PM     Click Here to See the Profile for Emil Menzel     Edit/Delete Message   Reply w/Quote
In FUNCTION PiEst, change the line that reads
X2=LOG(2^N) to
X2=LOG(2)* N
and you will be able to increase N to 10 million or more.
Of course you might have to wait a few seconds to get your
results. But the Stirling approximation for LogFactorial would
speed things up.

James:
Does your earlier formula for p also have a name?
Isn't the Wallis formula an exact one rather than an estimate
based on random numbers? (Of course one's answer will be only an
approximation, insofar as N falls short of infinity.)

Formula 12 in the Wallis reference comes closest to BASIC language
and I'm glad that it was included.

------------------

[This message has been edited by Emil Menzel (edited January 27, 2007).]

IP: Logged

Charles Pegge
Member
posted January 27, 2007 05:00 PM     Click Here to See the Profile for Charles Pegge     Edit/Delete Message   Reply w/Quote
Thanks Emil and James.

This is a method I came up with last night. It follows Wallis's Product quite closely.
Just by combining the logfactorials and 2^ in one loop. I am road-testing my scripting
language at the same time. I hope it makes sense. This is what it looks like:


new n=1e6, m=n*2, nn=n, b=0
{
b+log(n--/m); m-4; if m GT 0 then repeat
}
$ pi by factorials result: `2/(exp(b*2)*nn)` compare with `atan(1)*4`

[result]
pi by factorials result: 3.14159419221193 compare with 3.14159265358979

I could call it Lazy-Fingers.

------------------
www.pegge.net

IP: Logged

Brad D Byrne
Member
posted January 27, 2007 05:28 PM     Click Here to See the Profile for Brad D Byrne     Edit/Delete Message   Reply w/Quote
You guys are going to LOVE!! this!


arctan(1/2)/Pi in base 2 is 0.0010010111001000....


the binary expansion of arctan(1/2)/Pi by Simon Plouffe
http://www.cs.uwaterloo.ca/journals/JIS/construction503.gif

also see, http://www.cs.uwaterloo.ca/journals/JIS/compass.html

------------------
Washington DC Area
Borje's "Poff's" is likely the BEST tool for learning PB.. http://www.reonis.com/POFFS/index.htm
And a few PBTool's & Beginner Help:http://sweetheartgames.com/PBTools/JumpStart.html
& Another Good Resource: http://www.fredshack.com/docs/powerbasic.html

IP: Logged

James Graham-Eagle
Member
posted January 27, 2007 05:34 PM     Click Here to See the Profile for James Graham-Eagle     Edit/Delete Message   Reply w/Quote
Emil,

The formula I gave for the probability of a 50:50 split in terms
of pi does not have a name per se. However, most of the posts
are dancing around the same idea - we are really all talking
about the central binomial coefficient (2n) Choose (n). The
above formula comes from using Stirling's approximation on the
factorials in that coefficient. Wallis's formula is really just
another form for the same coefficient squared and rearranged
slightly. Walli's formula is exact in the limit.

------------------

IP: Logged


This topic is 10 pages long:   1  2  3  4  5  6  7  8  9  10 

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2007 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c