// (c)2009 Exstrom Laboratories LLC

function initPage(){calculateKellyFrac();simulateBets();}
function toggleShow(elementName){var elem=document.getElementById(elementName);var isShown=(elem.style.visibility=="visible")?true:false;elem.style.visibility=isShown?"hidden":"visible";elem.style.display=isShown?"none":"inline";}
function file2textarea()
{}
function getParams()
{var rStr=document.multireturn.rtextbox.value;var pStr=document.multireturn.ptextbox.value;var r=text2num(rStr);var p=text2num(pStr);var n=r.length;if(n!=p.length){alert("Please enter the same number of returns and probabilities.");return 0;}
if(n==1){alert("Please enter more than one return and probability.");return 0;}
return[r,p];}
function calculateKellyFrac()
{var params=getParams();var rv=params[0];var pv=params[1];var n=rv.length;var kf=100*kfsingle(rv,pv);var cash=100-kf;var output1=document.getElementById("output1");output1.innerHTML=kf.toFixed(4)+"%";var output2=document.getElementById("output2");output2.innerHTML=cash.toFixed(4)+"%";kf/=100;document.multireturn.fraction.value=kf.toFixed(4);drawProbPlot(rv,pv);}
function simulateBets()
{var fraction=parseFloat(document.multireturn.fraction.value);var nbets=parseFloat(document.multireturn.nbets.value);var params=getParams();var rv=params[0];var pv=params[1];var n=rv.length;var pvsum=0;for(var i=0;i<pv.length;++i)
pvsum+=pv[i];if(pvsum<1.0){alert("Simulation will only work when probabilities add up to 1.");return 0;}
var vec=sim_fbet(nbets,rv,pv,fraction);var outstr="";for(var i=0;i<vec.length;++i)
outstr+=vec[i].valueOf().toFixed(4)+"\n";document.multireturn.results.value=outstr;drawSimPlot(vec);}
function drawSimPlot(data2plot)
{var d1=new Array(data2plot.length);for(var i=0;i<data2plot.length;++i)
d1[i]=[i+1,data2plot[i]];new Proto.Chart($('PlotContainer2'),[{data:d1,label:"Simulation Dataset",lines:{show:true,fill:false},points:{show:true}}]);var container=document.getElementById("PlotContainer2");var plotlabel=document.createElement("div");plotlabel.style.position="absolute";plotlabel.style.left=(Math.floor(container.offsetWidth/4)+50)+"px";plotlabel.style.top="-25px";var plotlabeltext=document.createElement("i");plotlabeltext.innerHTML="<strong>"+"Total Assets"+"</strong>";plotlabel.appendChild(plotlabeltext);container.appendChild(plotlabel);}
function drawProbPlot(retv,probv)
{var d1=new Array(retv.length);var largest=Math.max.apply(null,retv);var smallest=Math.min.apply(null,retv);for(var i=0;i<retv.length;++i)
d1[i]=[retv[i],probv[i]];new Proto.Chart($('PlotContainer1'),[{data:d1,label:"Probabilities"}],{bars:{show:true,autoScale:false,barWidth:0.25},xaxis:{min:smallest-1,max:largest+1,tickSize:1,tickDecimals:0.1}});var container=document.getElementById("PlotContainer1");var plotlabel=document.createElement("div");plotlabel.style.position="absolute";plotlabel.style.left=(Math.floor(container.offsetWidth/4)+15)+"px";plotlabel.style.top="-25px";var plotlabeltext=document.createElement("i");plotlabeltext.innerHTML="<strong>"+"Probability Distribution"+"</strong>";plotlabel.appendChild(plotlabeltext);container.appendChild(plotlabel);}