MacRumors Forums JavaScript normal distribution

 Mar 5, 2013, 04:12 PM #1 moonman239 macrumors 65816   Join Date: Mar 2009 JavaScript normal distribution Code: ```// The function below generates a list of approximately normally distributed z-scores. The z-scores are between -4 and 4. This approximation of the normal distribution is good enough for most purposes. function zScoreDistribution() { var averages = new Array(); for (i=1;i<=2000;i++) { var x=0; var currentTotal = 0; for (x=1;x<=200;x++) { var newValue = Math.floor(Math.random() * 5); var negative = (Math.floor(Math.random() * 2) == 1); if (negative == true) { newValue = newValue * -1; } currentTotal = currentTotal + newValue; }; var currentAverage = currentTotal / x; averages.push(currentAverage); }; return averages; } // The function below finds the probability that a number that is randomly generated from a normal distribution will be less than or equal to x. function normCumProb(x,mean,stdDev,zScoreDist) { // First, convert x to a z-score. var zScore = (x - mean) / stdDev; // Next, find the number of entries in zScoreDist that are less than or equal to the z-score of x. Divide this number by the length of the distribution array to get the percent of all z-scores that are less than or equal to the z-score of x. This is the probability you want to calculate. var valueCount = 0; for (var z=0; z
Mar 6, 2013, 02:18 AM   #2
SrWebDeveloper
macrumors 68000

Join Date: Dec 2007
Location: Alexandria, VA, USA

Quote:
 Originally Posted by moonman239 EDIT: I should note that I need help! The latter function works, but when I test it using X=2.4, mean=2 and stdDev = 2.5, I consistently get an 80% probability. My TI-84 says the answer is approximately 56%.
You made no mention here of passing the value of zScoreDist to your function which is an array, apparently. Explain?

Why did you post the first function?

Your question seems to be about the latter function which you say works, but returns unexpected results. And what is a TI-84 ?! Are you a student being tasked to write some JS code for a course?

Please be specific as to what you need help with in terms of JS syntax or functions to help you accomplish your goal. We help you with that stuff, it's up to you to resolve the math.
__________________
Jim Goldbloom
Sr. Web Developer, owner GoldTechPro, LLC
http://www.GoldTechPro.com
0
Mar 7, 2013, 09:09 AM   #3
960design
macrumors 65816

Join Date: Apr 2012
Location: Destin, FL
Quote:
 Originally Posted by SrWebDeveloper And what is a TI-84 ?!
A pretty awesome calculator. I prefer the TI-89 Titanium. Blondes Vs Brunettes, age old story.

You need to step through your variables using a dev tool.

What I noticed immediately is that your top function returns an average that is not used anywhere. Exactly what SrWeb was stating.

Your second function starts looking something like this:
normCumProb(2.4, 2, 2.5, ???). We are missing something.
__________________
TI-99/4A, tape cassette, 12" B&W Zenith

Last edited by 960design; Mar 7, 2013 at 09:36 AM.
0
 Mar 7, 2013, 01:09 PM #4 SrWebDeveloper macrumors 68000     Join Date: Dec 2007 Location: Alexandria, VA, USA Ah, of course. These days I use the Mac calculators from the app store, plenty of really cool free ones. Anyway, if you (OP) does not have a dev tool to step through simply add print or echo statements after *each* math statement to print the result and compare with your TI-84 each step. Simple as that. That will tell you if you goofed up syntax or in the math department. The usual culprits are forgetting about divisible by zero, not casting properly, looping through arrays without checking values for each iteration, unexpected negative values, not rounding off, etc. which means learn how PHP math functions work. http://php.net/manual/en/ref.math.php __________________ Jim Goldbloom Sr. Web Developer, owner GoldTechPro, LLC http://www.GoldTechPro.com 0