//Weibull distribution online. Including the exponential and Rayleigh 
//distribution.
//copyright: Daan G Uitenbroek PhD

function ClearOutp()   
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";   
document.form1.outp.value ="cleared"+writeln;  
}  
 
function array(len)  
{  
  this.length=len;  
   for (var i=1;i<=len;i++)  
      this[i]=0;  
  return this  
} 
  
function odd(number)  //determines if a number is odd  
{  
var isodd=0*1;  
if (Math.round(number/2+0.00001)==number/2)  isodd=1*1;  
return isodd  
}  
 
function intg(number)  //determines if a number is an integer 
{ 
var isint=0*1 
if (Math.round(number)==number) isint=1*1 
return isint 
} 

function factorial(start,startloop,fvalue) //factorial without recursion
{
factorialvalue=start
for (var index=startloop;index<=fvalue;index++)
 factorialvalue*=index

return factorialvalue 
}
 
function factorial2(start,andex,fvalue)  //returns the factorial value of a number 
{ 
factorialvalue=start 
if (andex<=fvalue)
factorial(factorialvalue*andex,andex*1+1*1,fvalue)  
 
return factorialvalue 
} // recursive procedure (doesn't work well in Netscape)

function logfactorial(start,startloop,fvalue) //logfactorial without recursion
{
factorialvalue=start
for (var index=startloop;index<=fvalue;index++)
 factorialvalue=factorialvalue*1+Math.log(index)*1

return factorialvalue 
}
 
function logfactorial2(start,andex,fvalue)  //returns the log factorial value of a number 
{ 
factorialvalue=start 
if (andex<=fvalue)  
  
logfactorial(factorialvalue*1+Math.log(andex)*1,andex*1+1*1,fvalue)  
 
return factorialvalue 
} 
 
function truncate(real,places)  // truncates 'real' numbers n 'places' behind the decimal dot 
{ 
var multi=Math.pow(10,places); 
return (Math.round(real*multi)/multi) 
} 
 
function remain(num)  //returns the remainder of a real number 
{ 
var remainder=num-Math.round(num-0.5) 
 
return remainder 
} 

function gamma(x)  //algorithm 221 combined with exact factorial 
{
if (x>6) var gamma=factorial(1,1,Math.round(x-1)) 
else  
if ((x>3) & (intg(x)==1)) var gamma=factorial(1,1,x-1) 
else 
{ 
a=new array(11);  
a[0]=1.0 
a[1]=.4227843370 
a[2]=.4118402518 
a[3]=.0815782188 
a[4]=.0742379076 
a[5]=-.0002109075 
a[6]=.0109736958 
a[7]=-.0024667480 
a[8]=.0015397681 
a[9]=-.0003442342 
a[10]=.0000677106 
 
var t=(x<=1) ? x : (x<=2) ? x-1 : x-2 
var p=a[10] 
for (var index=9;index>-1;index-=1) var p=t*p+a[index]*1 
var gamma=(x<=1) ? p/(x*(x*1+1*1)) : (x<=2) ? p/x : p 
} 
return gamma 
} 

function pointweibull(x,alfa,beta)
{
var pointprobability=alfa*Math.pow(beta,(alfa*-1))*Math.pow(x,(alfa-1))* Math.exp(-1*Math.pow((x/beta),alfa))
return pointprobability
}

function cumweibull(x,alfa,beta)
{
var cumulativeprobability=1-Math.exp(-1*Math.pow((x/beta),alfa))
return cumulativeprobability
}

function meanweibull(alfa,beta)
{
var mean=beta*gamma(1*1+(1/alfa)*1)
return mean
}

function varianceweibull(alfa,beta)
{
var variance=beta*beta*(gamma(1+2/alfa)-(gamma(1*1+(1/alfa)*1)*gamma(1+(1/alfa)*1)))
return variance
}

function qweibull(percentile,alfa,beta)
{
var x=beta*Math.pow((-1*Math.log(1-percentile)),(1/alfa))
return x
}

function modeweibull(alfa,beta)
{
var mode=0;
if (alfa>1) mode=beta*Math.pow((1-(1/alfa)),(1/alfa))
return mode
}

function main(x,alfa,beta,perc) 
{ 
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  

if (x>=1) 
document.form1.outp.value+=
     "The cumulative probability equals: "
     +cumweibull(x,alfa,beta)+writeln 
     +"The density at x equals: "+
     pointweibull(x,alfa,beta)+writeln 
document.form1.outp.value+=
     "The mean of the distribution equals: "+meanweibull(alfa,beta)+writeln 
     +"The variance equals: "+varianceweibull(alfa,beta) +writeln 
     +"The mode equals: "+modeweibull(alfa,beta)+writeln+writeln 

     if (perc>0)
     {if (perc>1) perc=perc/100
         document.form1.outp.value+="The "+(perc*100)
                         +"th percentile is at x= " 
                         +qweibull(perc,alfa,beta)+writeln+writeln

     }
}

//START OF ALTERNATIVE TO WEIBULL ACCORDING TO Mendenhall & Sincich, 1995

function altinvertweibull(percentile,alfa,beta)  
{ 
var minu=0.0*1  
var maxu=2000.0*1  
 
var x=1;    
for (var index=1;maxu-minu > 0.000000001;index+=1)  
  {  
   if (altcumweibull(x,alfa,beta)>percentile)   
                 maxu=x  
   else  
                  minu=x  
   x=(maxu*1 + minu*1)*0.5  
   }  
return x 
}  

function altpointweibull(x,alfa,beta)
{
var pointprobability=alfa/beta*Math.pow(x,(alfa-1))*Math.exp(-1*Math.pow(x,alfa)/beta)
return pointprobability
}

function altcumweibull(x,alfa,beta)
{
var cumulativeprobability=1-Math.exp(-1*Math.pow(x,alfa)/beta)        
return cumulativeprobability
}

function altmeanweibull(alfa,beta)
{
var mean= Math.pow(beta,1/alfa)*gamma(1*1+(1/alfa)*1)
return mean
}

function altvarianceweibull(alfa,beta)
{
var variance=Math.pow(beta,2/alfa)*(gamma(1+2/alfa)-(gamma(1*1+(1/alfa)*1)*gamma(1+(1/alfa)*1)))
return variance
}

function alter(x,alfa,beta,perc) 
{ 
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
 
if (x>=1) 
document.form1.outp.value+=
     "The alternative cumulative p equals: "
     +altcumweibull(x,alfa,beta)+writeln 
     +"The alternative density at x equals: "
     +altpointweibull(x,alfa,beta)+writeln 
document.form1.outp.value+=
     "The mean of the alternative equals: "+altmeanweibull(alfa,beta)+writeln 
     +"The alternative variance equals: "
     +altvarianceweibull(alfa,beta)+writeln+writeln 

    if (perc>0)
     {if (perc>1) perc=perc/100
      document.form1.outp.value+="The alternative "+(perc*100)+"th percentile is at x="
                               +altinvertweibull(perc,alfa,beta)+writeln+writeln
     }
}

//START OF LOG-WEIBULL-FISHER-TIPPETT-EXTREME-VALUE

function logcumweibull(x,alfa,beta)
{
var pointprobability= Math.exp(-1*Math.exp((alfa-x)/beta))
return pointprobability
}

function logpointweibull(x,alfa,beta)
{
var cumulativeprobability=Math.exp(((alfa-x)/beta)-Math.exp((alfa-x)/beta))        
cumulativeprobability=cumulativeprobability/beta
return cumulativeprobability
}

function logmeanweibull(alfa,beta)
{
var mean=beta*0.577215664901532860606512090082402431042+alfa*1  
    //Euler-Mascheroni Constant
return mean
}

function logvarianceweibull(alfa,beta)
{
var variance=Math.PI*Math.PI*beta*beta/6
return variance
}

function logqweibull(percentile,alfa,beta)
{
var x=alfa-(beta*Math.log(Math.log(1/percentile)))
return x
}

function log(x,alfa,beta,perc) 
{ 
writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
 
if (x>=1) 
document.form1.outp.value+=
     "The log-Weibull cumulative p equals: "
     +logcumweibull(x,alfa,beta)+writeln 
     +"The log-Weibull density at x equals: "
     +logpointweibull(x,alfa,beta)+writeln 
document.form1.outp.value+=
     "The mean of the log-Weibull equals: "+logmeanweibull(alfa,beta)+writeln 
     +"The log-Weibull variance equals: "
     +logvarianceweibull(alfa,beta)+writeln
     +"The log-Weibull mode is equal to alpha"+writeln 

    if (perc>0)
     {if (perc>1) perc=perc/100
       var x=logqweibull(perc,alfa,beta)
       document.form1.outp.value+="The log-Weibull "+(perc*100)
           +"th percentile is at x= "
           +x+writeln
       if (x<0) 
       document.form1.outp.value+="Note that x is negative"+writeln
       document.form1.outp.value+= writeln
     }
}

