/*	20070412a-volatility.js		3-1-2009		JavaScript
	Copyright (C)2008-09 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License (GPL)
	Version 3 as published by the Free Software Foundation.
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

JavaScript functions specific to the stock price volatility calculator.

*/
//----------------------------------------------------------------------------------------------
// The calculator expects the historical data to have the oldest value at the top.
// This reverses the order of the input values. 
function ReverseInputs()
{
var t = document.getElementById("StockPricesIn").value.replace(/[^0-9.]+/gi," ").split(/\s+/).reverse();
var u = "";
var i;
for(i = 0 ; i < t.length ; i++)
	u += (t[i] + "\n");
document.getElementById("StockPricesIn").value = u;
}
//----------------------------------------------------------------------------------------------
function CalcVolatility()	
{
// Strip out all chars that are illegal in a number, which also removes security hazards.
// Then split at whitespace, treating multiple whitespace as one.
// Now raw[] contains a list of all the input numbers. 
var raw = document.getElementById("StockPricesIn").value.replace(/[^0-9.]+/gi," ").split(/\s+/);

var src = new Array();			// raw with blank or illegal values omitted
var ruleof16 = new Array();		// transformed to abs(px - prevpx)/(prev px)
var lognormal = new Array();	// transformed to natural log of px/(prev px)
var prev = Number.NaN;			// remains NaN until the first valid data point is extracted
var i, x;
for(i = 0 ; i < raw.length ; i++)
{
	x = parseFloat(raw[i]);
	if(!isNaN(x))
	{
		src.push(x);
		if(!isNaN(prev))	// price change calculation is only valid after the first data point
		{
			ruleof16.push(Math.abs(x - prev) / prev);
			lognormal.push(Math.log(x / prev));	
		}
		prev = x;
	}
}

var stats;	// stats["IsOk"] should always be true (even if the values are wrong) because all non-numeric chars have been stripped.
stats = DescriptiveStatistics(src);	
document.getElementById("ShortcutOutput").innerHTML = (stats["Range"] / (stats["Minimum"] + stats["Maximum"])); 
document.getElementById("StdDevPxOutput").innerHTML = (stats["StdDevPop"] / stats["ArithMean"]);

stats = DescriptiveStatistics(ruleof16);
document.getElementById("RuleOf16Output").innerHTML = (stats["ArithMean"] * 16); //.toFixed(4)//.toString(10); 

stats = DescriptiveStatistics(lognormal);
document.getElementById("LognormalOutput").innerHTML = (stats["StdDevEst"] * Math.sqrt(TradingDaysIn(365.25)));
}
//----------------------------------------------------------------------------------------------
