Statistical Functions in ASP?

# Thread: Statistical Functions in ASP?

1. Senior Member
Join Date
Dec 1969
Posts
161

## Statistical Functions in ASP?

I have a need for some statistical functions in ASP, especially <BR><BR>Combin(f,n), <BR><BR>which equals:<BR><BR> f!<BR>------<BR>n!*(f-n)!<BR><BR>Now this function is mentioned on Microsofts website:<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/xhtml/SEB62.asp<BR><BR>(Must be an office97 function.)<BR><BR>But the function does not exist in ASP. Is that function written out some place? I need it, but I don&#039;t seem to know where to get it. If you have experience in getting this off of Microsoft&#039;s website so that it can work within an asp page, please let me know what I am missing here.<BR><BR>Thanks,<BR><BR>Edward<BR>;-)

2. Senior Member
Join Date
Dec 1969
Posts
161

## RE: Statistical Functions in ASP? - HELP!

A better function than the one just mentioned would be:<BR>HYPGEOMDIST <BR>shown here:<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/xhtml/SEBBD.asp<BR>Please let me know if you know how to make this function work in asp.<BR><BR>THANK YOU and happy new year!<BR><BR>Edward<BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## No...but easy to write...

&#060;%<BR>Function Factorial(n)<BR>&nbsp; &nbsp; dim i, p<BR>&nbsp; &nbsp; p = 1<BR>&nbsp; &nbsp; For i = 2 To n<BR>&nbsp; &nbsp; &nbsp; &nbsp; p = p * i<BR>&nbsp; &nbsp; Next<BR>&nbsp; &nbsp; Factorial = p<BR>End Function<BR><BR>Function Combin( f, n )<BR>&nbsp; &nbsp; Combin = Factorial(f) / ( Factorial(n) * Factorial(f-n) )<BR>End Function<BR>%&#062;<BR><BR>If you recognize that<BR>f!<BR>---<BR>n! <BR>can be done via a shortcut, you can write code that&#039;s a bit faster. But maybe stick to the straightforward way when starting out?<BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## More code, but not...

...really more complicated.<BR><BR>Function hypgeomdist( x, y, M, N ) <BR>&nbsp; &nbsp; Dim t1, t2, t3<BR>&nbsp; &nbsp; t1 = Combin( M, x )<BR>&nbsp; &nbsp; t2 = Combin( N-M, y-x )<BR>&nbsp; &nbsp; t3 = Combin( N, y )<BR>&nbsp; &nbsp; hypgeomdist = t1 * t2 / t3<BR>End Function <BR><BR>Assuming I read that correctly!<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
161

## OVERFLOW

I have tried writing code like that, but I keep getting overflow errors (not enough stacks, etc.)<BR><BR>The reason I am looking to reproduce the function HYPGEOMDIST (available to Microsoft office) is that there are many simplifications possible in the fraction form, that using VBscript cannot do. So, I end up with the HUGE numbers that finally create overflow errors. If I could copy this function, written by some hotshot microsoft techie, I am pretty sure I would not run into the trouble. Please take a look at this page:<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/xhtml/SEBBD.asp<BR><BR>And this page:<BR>http://www.lottery.state.mn.us/hypergeo.html<BR><BR>Thank you Bill!<BR><BR>Edward<BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Worked for me...

*********** FILE: hypTest.asp **********<BR>&#060;HTML&#062;&#060;BODY&#062;<BR> &#060;% <BR>Function Factorial(n) <BR>&nbsp; &nbsp; dim i, p <BR>&nbsp; &nbsp; p = 1 <BR>&nbsp; &nbsp; For i = 2 To n <BR>&nbsp; &nbsp; &nbsp; &nbsp; p = p * i <BR>&nbsp; &nbsp; Next <BR>&nbsp; &nbsp; Factorial = p <BR>End Function <BR><BR>Function Combin( f, n ) <BR>&nbsp; &nbsp; Combin = Factorial(f) / ( Factorial(n) * Factorial(f-n) ) <BR>End Function <BR><BR>Function hypgeomdist( x, y, M, N ) <BR>&nbsp; &nbsp; Dim t1, t2, t3 <BR>&nbsp; &nbsp; t1 = Combin( M, x ) <BR>&nbsp; &nbsp; t2 = Combin( N-M, y-x ) <BR>&nbsp; &nbsp; t3 = Combin( N, y ) <BR>&nbsp; &nbsp; hypgeomdist = t1 * t2 / t3 <BR>End Function <BR><BR>%&#062; <BR><BR>hypgeomdist( 1, 4, 8, 20 ) is &#060;%= hypgeomdist( 1, 4, 8, 20 ) %&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR>******************************** *********<BR><BR>If you are getting overflow, then you might need to pull the trick I mentioned.<BR><BR>Recognize that <BR><BR>f!<BR>--- <BR>n!<BR><BR>can be done all in one operation via:<BR>&nbsp; &nbsp; p = 1<BR>&nbsp; &nbsp; For i = n+1 To f<BR>&nbsp; &nbsp; &nbsp; &nbsp; p = p * i<BR>&nbsp; &nbsp; Next<BR><BR>Do you see why?<BR><BR>suppose f is 7 and n is 3.<BR><BR>7! = 7 * 6 * 5 * 4 * 3 * 2 * 1<BR>------------------------------------<BR>3! = 3 * 2 * 1<BR><BR>So if you just start at 4 and go to 7 in your building of the product, you have it.<BR><BR>Play with improving the number of loops, etc., in the Combin function and you should get it to work. There&#039;s no magic math formula to get the factorial of a number. It&#039;s all just hard work.<BR><BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## If you get *STACK* overflow...

...it is almost surely because you wrote your factorial function using recursion.<BR><BR>Tch. Doing factorial by recursion is a cute trick to demo how it works, but it&#039;s a horrible way to get a factorial in real life.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Indeed, *do* look at that page...

http://www.lottery.state.mn.us/hypergeo.html<BR><BR>Look at the example at the bottom, where they reduce the combinatorials to the *actual* multiplies needed! For example, instead of having to calculate 44! and 42!, they end up just multiplying 44*43 -- TA DA.<BR><BR>If you write your own COMBIN function to pull those kinds of tricks, it will work in most any *real* case.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•