Got a tip for us? Let us know
Mar 5, 2013, 05:12 PM  #1 
JavaScript normal distribution
Code:
// The function below generates a list of approximately normally distributed zscores. The zscores 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 zscore. var zScore = (x  mean) / stdDev; // Next, find the number of entries in zScoreDist that are less than or equal to the zscore of x. Divide this number by the length of the distribution array to get the percent of all zscores that are less than or equal to the zscore of x. This is the probability you want to calculate. var valueCount = 0; for (var z=0; z<zScoreDist.length; z++) { if (zScoreDist[z] <= zScore) { valueCount = valueCount + 1; }; }; var normProbability = valueCount / zScoreDist.length; return normProbability; }
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so. 

0 
Mar 6, 2013, 03:18 AM  #2  
Quote:
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 TI84 ?! 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, 10:09 AM  #3 
A pretty awesome calculator. I prefer the TI89 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.
__________________
TI99/4A, tape cassette, 12" B&W Zenith Last edited by 960design; Mar 7, 2013 at 10:36 AM. 

0 
Mar 7, 2013, 02:09 PM  #4 
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 TI84 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 
«
Previous Thread

Next Thread
»
Thread Tools  Search this Thread 
Display Modes  
Similar Threads  
thread  Thread Starter  Forum  Replies  Last Post 
Is this legal? (10.4 distribution question)  toroidal boat  Buying Tips and Advice  13  Feb 23, 2014 12:50 AM 
OS X inspired Linux distribution  Cactii  Windows, Linux & Others on the Mac  11  Dec 28, 2012 10:19 AM 
Distribution Center job?  marcusj0015  Community Discussion  2  Jun 15, 2012 05:43 AM 
is that normal?batteryis it normal?battery  talaada101  iPhone Tips, Help and Troubleshooting  0  Jun 4, 2012 09:51 PM 

All times are GMT 5. The time now is 04:52 PM.