These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Need javascript for calculating standard deviation

Snktimoniuz
Registered: Jun 16 2009
Posts: 8

I'm using Acrobat 9 Pro to create forms which contain simple calculations. The average function, available through text field calculations, works fine. I am looking for a way to calculate the standard deviation of the same set of data (40 data points). I know nothing about javascript and wanted to know if there is a repository of simple calculations that I can pirate to suit my needs. Or maybe there's a helpful programmer in the forums.

Standard deviation is the square root of the sum of the mean subtracted from each individual value divided by n-1. SD=Sqrt( sum (xi-xmean)/n-1)

Thanks

My Product Information:
Acrobat Pro 9.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2399
Let's assume you have the mean stored in a variable called 'mean' and the values are in an array called 'values'. Then the way to calculate SD is:

sum=0;for (i=0; i<n; i++) {sum+=values[i]-mean;}SD = Math.sqrt(sum/(n-1));

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Snktimoniuz
Registered: Jun 16 2009
Posts: 8
Thanks for the reply. Unfortunately I do not know how to store the values in an array so that would be helpful to know.

Also, I realize I made a mistake in my original description.

Each of the (individual value minus the mean) is squared before being summed.

SD = SQRT [ sum (xi-xmean)^2/n-1]
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
First we need to define some useful document level functions:
function Count(aValues) {// count non-blank valuesvar count = 0;for(i = 0; i < aValues.length; i++) {// count non-blank elementsif(aValues[i].toString() != '') {count++;} // not blank} // end loopreturn count;} function Sum(aValues) {// sum non-blank valuesvar sum = 0;for(i = 0; i < aValues.length; i++) {// sum only non-blanksif(aValues[i].toString() != '') {// add element to sumsum += Number(aValues[i]);} // not blank} // end loopreturn sum;} function Avg(aValues) {// average non-blank valuesvar avg = 0;// count the non-bank valuesvar nCnt = Count(aValues);// compute average if count is not zeroif(nCnt != 0) {// sum the non-blank numbersnSum = Sum(aValues);// compute averageavg = nSum / nCnt;} // end non-zero count processingreturn avg;} function StdDevDist(aValues) {// Standard Deviation Distribution for non-blank items// compute the averagevar nStd = 0;var nCnt = Count(aValues);var nAvg = Avg(aValues);var nSumDiffSq = 0;for(i = 0; i < aValues.length; i++) {// skip blank entriesif(aValues[i].toString() != '') {nSumDiffSq += Math.pow(aValues[i] - nAvg, 2);} // not blank} // end loop// need to have more than 1 non-blank itemif(nCnt > 1) {nStd = Math.sqrt(nSumDiffSq / (nCnt -1));}return nStd;}// end StdDevDist function StdDev(aValues) {// Standard Deviation for non-blank items// compute the averagevar nStd = 0;var nCnt = Count(aValues);var nAvg = Avg(aValues);var nSumDiffSq = 0;for(i = 0; i < aValues.length; i++) {// skip blank entriesif(aValues[i].toString() != '') {nSumDiffSq += Math.pow(aValues[i] - nAvg, 2);} // not blank} // end loop// need to have more than 1 non-blank itemif(nCnt > 1) {nStd = Math.sqrt(nSumDiffSq / nCnt );}return nStd;}// end StdDev

For the field to receive the [url=http://mathworld.wolfram.com/StandardDeviation.php]standard deviation distribution[/url] you can use the following 'Custom calculation script':
// create an array of field names to usevar aFieldNames = new Array('score0','score1', 'score2', 'score3', 'score4','score5', 'score6','score7'); // create an array of values for named fieldsfor(i = 0; i < aFields.length; i++) {aScores[i] = this.getField(aFieldNames[i]).valueAsString;// additional code to force balnks to zeroif(aScores[i] == ''){aScores[i] = 0;} end force blank tozero} // end loop // call the function to compute the Standard Deviation Distributionevent.value = StdDevDist(aScores);

George Kaiser

Snktimoniuz
Registered: Jun 16 2009
Posts: 8
Wow thank you!

Looks like I'll be having some coffee and cross referencing code with my JavaScript for Acrobat API Reference.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can learn about document level Acrobat JavaScript placement in Thom Parker's article [url=http://www.acrobatusers.com/tutorials/2007/07/js_document_scripts]Entering Document Scripts[/url]. And in the "Custom Calculation Script" for the field, you only need to change the test 'score#' to the field names, and by letting JavaScript determine the number of elements in the 'aFieldNames' array there is no need to change any of the computational code as it automatically adjust for the size of the array of names. This is similar to the code behind "Value is the ____ of:" calculation option.

To understand all of the JavaScirpt, you will also need to look at reference material at the [url=https://developer.mozilla.org/En/JavaScript]Mozilla Developer Center for JavaScirpt[/url] for the 'Math' object's 'sqrt' and 'pow' and user defined functions.

The script to build an array of values, also changes empty, null, vlaues to zero just like the Acrobat builtin furnction. But if you do not want to treat a null field as a zero result, you only need to remove the lines or comment out the lines for this test, and the computions will only be perfromed on non-blank items. Note zero is a non-blank or non-null entry. So if one does not want the assumption made by Adobe for the provided routines, these scripts could provide a basis for a work around.

George Kaiser