On my wishlist for the deckeditor is the same ol', same ol' mana curve. Hopefully making it look pretty with GLScene, and some cool stacked graphs etc. Anyway... another is to incorporate my Excel "combo" calculator.

For this however, I had to implement the Hyper Geometric Distribution in pascal... I read the wikipedia article... My statistics are a little bit rusty, and I was really struggling. However, I followed the external links, to nerdbucket. He has an implementation for C++ as well as one for Ruby. But what *really* interested me, was the exponent / ln mathematics.

This enabled me to create a decent implementation of Hyper Geometric Distribution for Pascal... and here it follows:

Function lnFactorial(N:Integer):Double;
var i : integer;
aValue : double;
begin
if N = 0 then begin result := 1; exit; end;
aValue := 0;
for i := 1 to N do
aValue := aValue + ln(i);
Result := aValue;
end;


function BinomialCoef(n,k:integer):double;
var aValue : double;
begin
aValue := exp(lnFactorial(n) - (lnFactorial(k) + lnFactorial(n-k)));
BinomialCoef:=aValue;
end;

Function HypGeomDist(RequiredHits, PopulationSize, Targets, Attempts:integer):double;
begin
Result :=
BinomialCoef
(Targets,RequiredHits)
*
BinomialCoef
(PopulationSize-Targets,Attempts - RequiredHits)
/
BinomialCoef
(PopulationSize,Attempts);
end;

Please consider the code as public domain.

2 comments:

:-(

Google removes my beautifully indented code. Very :-(

October 7, 2008 at 12:13 AM  

http://formatmysourcecode.blogspot.com/ gets it formatted, but not with highlighting.

October 7, 2008 at 8:51 PM  

Newer Post Older Post Home

Blogger Template by Blogcrowds.