Go Back   MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 5, 2013, 05: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<zScoreDist.length; z++)
{
if (zScoreDist[z] <= zScore)
{
valueCount = valueCount + 1;
};
};
var normProbability = valueCount / zScoreDist.length;
return normProbability;
}
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%.
__________________
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.
moonman239 is offline   0 Reply With Quote
Old Mar 6, 2013, 03:18 AM   #2
SrWebDeveloper
macrumors 68000
 
SrWebDeveloper's Avatar
 
Join Date: Dec 2007
Location: Alexandria, VA, USA
 
Quote:
Originally Posted by moonman239 View Post
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
SrWebDeveloper is offline   0 Reply With Quote
Old Mar 7, 2013, 10:09 AM   #3
960design
macrumors 6502a
 
Join Date: Apr 2012
Location: Destin, FL
Quote:
Originally Posted by SrWebDeveloper View Post
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 10:36 AM.
960design is offline   0 Reply With Quote
Old Mar 7, 2013, 02:09 PM   #4
SrWebDeveloper
macrumors 68000
 
SrWebDeveloper's Avatar
 
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
SrWebDeveloper is offline   0 Reply With Quote

Reply
MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

All times are GMT -5. The time now is 02:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC