Statistical Functions
script fdensity {
input x=1.31;
input d1=1;
input d2=1;
def fden = 1/sqrt(x)*power((1+x/d2),(-(1+d2)/2));
plot fd = fden;
}
script pdf {
input x=1.31;
input length = 10;
def mean=average(x,length);
def std=stdev(mean);
def standardvariable = -4.0;
def fx = mean+(standardvariable*std );
def fden =exp(-1/2*power(fx,2)/sqrt(2*double.pi));
plot fd = fden;
}
script pnorm {
input z =1;
def x = SQRT(1/2)*absvalue(z);
def t = 1.0/(1+0.5*x);
def t2 = t*t;
def t3 = t2*t;
def t4 = t2*t2;
def t5 = t2*t3;
def t6 = t3*t3;
def t7 = t3*t4;
def t8 = t4*t4;
def t9 = t4*t5;
def tau = -x*x - 1.26551223 + 1.00002368*t + 0.37409196*t2 + 0.09678418*t3 - 0.18628806*t4 + 0.27886807*t5 - 1.13520398*t6 + 1.48851587*t7 - 0.82215223*t8 + 0.17087277*t9;
def p = 0.5*t*exp(tau);
def p2 = if z < 0 then 1-p else p;
plot pnorm_ = p2;
}
script erf {
input z = 0;
def t = 1.0 / (1.0 + 0.5 * AbsValue(z));
# use Horner's method
def ans = 1 - t * Exp( -z * z - 1.26551223 +
t * ( 1.00002368 +
t * ( 0.37409196 +
t * ( 0.09678418 +
t * (-0.18628806 +
t * ( 0.27886807 +
t * (-1.13520398 +
t * ( 1.48851587 +
t * (-0.82215223 +
t * ( 0.17087277))))))))));
plot return = if z >= 0.0 then ans else -ans;
}
script fdt {
input f = 1.31;
input d1 = 1;
input d2 = 1;
def s;
def t;
def z;
## Computes using inverse for small F-values
if f < 1 {
s = d2;
t = d1;
z = 1 / f;
} else {
s = d1;
t = d2;
z = f;
}
def j = 2 / (9 * s);
def k = 2 / (9 * t);
## Uses approximation formulas
def y0 = AbsValue((1 - k) * Power(z, (1 / 3)) - 1 + j) / Sqrt(k * Power(z, (2 / 3)) + j);
def y;
if t < 4 {
y = y0 * (1 + 0.08 * Power(y0, 4) / Power(t, 3));
} else {
y = y0;
}
def a1 = 0.196854;
def a2 = 0.115194;
def a3 = 0.000344;
def a4 = 0.019527;
def x0 = 0.5 / Power((1 + y * (a1 + y * (a2 + y * (a3 + y * a4)))), 4);
def x1 = Floor(x0 * 10000 + 0.5) / 10000;
def x = if f < 1 then 1 - x1 else x1; # Tail End
def str1 = (1 - x);# Percentile
plot fd = x;
}
script Tdist {
input tvalue = 1.31;
input d = 1;
def x = 1;
def y = 1;
def t = Power(tvalue , 2);
def s;
def r;
def z;
if t < 1 {
s = d;
r = y;
z = 1 / t;
} else {
s = y;
r = d;
z = t;
}
def j = 2 / (9 * s);
def k = 2 / (9 * t);
def l0 = AbsValue((1 - k) * Power(z, (1 / 3)) - 1 + j) / Sqrt(k * Power(z, (2 / 3)) + j);
def l;
if t < 4 {
l = l0 * (1 + 0.08 * Power(l0, 4) / Power(r, 3));
} else {
l = l0;
}
def a1 = 0.196854;
def a2 = 0.115194;
def a3 = 0.000344;
def a4 = 0.019527;
def x0 = 0.25 / Power(1 + l * (a1 + l * (a2 + l * (a3 + l * a4))), 4);
def x1 = Floor(x0 * 10000 + 0.5) / 10000;
def x2 = if t < 1 then 1 - x1 else x1;
def left = 1 - x1;
plot td = x2;
}
script Tdist {
input tvalue = 1.31;
input d = 1;
def x = 1;
def y = 1;
def t = power(tvalue ,2);
def s;
def r;
def z;
if t< 1 {
s = d;
r = y;
z = 1/t;
} else {
s = y;
r = d;
z = t;
}
def j = 2 / (9 * s);
def k = 2 / (9 * t);
def l0 = AbsValue((1 - k) * Power(z, (1 / 3)) - 1 + j) / Sqrt(k * Power(z, (2 / 3)) + j);
def l;
if t < 4 {
l= l0 * (1 + 0.08 * Power(l0, 4) / Power(r, 3));
} else {
l = l0;
}
def a1 = 0.196854;
def a2 = 0.115194;
def a3 = 0.000344;
def a4 = 0.019527;
def x0 = 0.25 / Power(1 + l*(a1 + l*(a2 + l*(a3 + l*a4))), 4);
def x1 = Floor(x0 * 10000 + 0.5) / 10000;
def x2 = if t < 1 then 1 - x1 else x1;
def left = 1 - x1;
plot td = x2;
}
script binomR { # binomial
input n = 20;
input y = close;
def x = CompoundValue(1, x[1] + 1, 1);
def Exy = SumLastN(x * y, n);
def Ey = SumLastN(y, n);
def Ex = SumLastN(x, n);
def Ex2 = SumLastN(Sqr(x), n);
def a = (n * Exy - Ex * Ey) / (n * Ex2 - Sqr(Ex));
def b = (Ey - a * Ex) / n;
def c1 = (Exy - a * Ex2 - b * Ey) / n;
plot QuadraticAverage = a * x * x + b * x + c1;
}
script kernel {
input p = 4; # window size
input data = hl2;
# Define different xh values for multiple kernels
input xh1 = 0.001; # play with it. Step is 0.001. "0" value returns line equal to SMA
input xh2 = 0.01; # You can add more xh values if needed
# Define the kernel function for each data series
def kernel1 = fold i1 = 0 to p with n1 = 0 do n1 + (Exp(-xh1 * Sqr(p - i1)) * GetValue(data, p - i1, p));
def kernel2 = fold i2 = 0 to p with n2 = 0 do n2 + (Exp(-xh2 * Sqr(p - i2)) * GetValue(data, p - i2, p));
# Add more kernel functions for additional xh values
# Compute the normalization terms for each kernel
def norm1 = fold j1 = 0 to p with m1 = 0 do m1 + Exp(-xh1 * Sqr(p - j1));
def norm2 = fold j2 = 0 to p with m2 = 0 do m2 + Exp(-xh2 * Sqr(p - j2));
# Add more normalization terms for additional xh values
# Compute the R-squared values for each kernel
def rsquared1 = Power(Correlation(data[p], kernel1), 2);
def rsquared2 = Power(Correlation(data[p], kernel2), 2);
# Add more R-squared values for additional xh values
# Calculate the adaptive weights based on R-squared values
def total_rsquared = rsquared1 + rsquared2; ## Add more rsquared values if needed
def weight1 = rsquared1 / total_rsquared;
def weight2 = rsquared2 / total_rsquared;
# You can add more weights for additional xh values
# Compute the multiple kernel regression
def ts = (kernel1 * weight1 + kernel2 * weight2) / (norm1 * weight1 + norm2 * weight2);
# Add more terms for additional xh values
# Plot the result
plot multiple_kernel_regression = ts;
}
script fractR {
# Define fractional power for y
input n = 20;
input y = close;
def power = 2;
# Calculations
def x = BarNumber();
def FracY = FractionalPolynomial(y, power);
def Ex = SumLastN(x, n);
def Ey = SumLastN(FracY, n);
def Ex2 = SumLastN(x * x, n);
def Exy = SumLastN(x * FracY, n);
# Regression calculations
def Exx = Ex2 - Sqr(Ex) / n;
def a = Exy / Exx;
def b = Ey / n - a * Ex / n;
def regressionLine = a * x + b;
# Plot
plot regression = regressionLine;
}
## Bivariate
script BivariateCoeff {
input n = 14;
input Y = close;
# Define independent variables
def X1 = volume; # Volume
def X2 = high - low; # Daily range
# Calculate means
def meanY = Average(Y, n);
def meanX1 = Average(X1, n);
def meanX2 = Average(X2, n);
# Calculate covariances and variances
def cov1 = Average(Y * X1, n) - meanY * meanX1;
def varX1 = Average(X1 * X1, n) - meanX1 * meanX1;
def cov2 = Average(Y * X2, n) - meanY * meanX2;
def varX2 = Average(X2 * X2, n) - meanX2 * meanX2;
# Calculate bivariate coefficients (betas)
def beta1 = cov1 / varX1;
def beta2 = cov2 / varX2;
# Calculate y-intercept (beta0)
def beta0 = meanY - beta1 * meanX1 - beta2 * meanX2;
# Calculate and plot predicted Y
plot Y_pred = beta0 + beta1 * X1 + beta2 * X2;
}
script func { # simple fuction
input x = 0;
plot f = x - 2.035;
}
script Bisec {
input a = 1.0001;
input b = 2.31;
def e = 0.00001;
def mn = a;
def mx = b;
def mid1 = (mn + mx) / 2;
def ret = fold i = 0 to 100 with mid=(mn + mx) / 2 while mx - mn > e do
if func(mn) * func(mid1) < 0 then mx == mid else mn == mid;
plot return = ret;
}
script Tinv {
input probability = 1;
input degreesOfFreedom = 1;
input tolerance = 0.00001;
input maxIterations = 50;
def tLow = -1000.0;
def tHigh = 1000.0;
def tMid = (tLow + tHigh) / 2.0;
def pMid = CumulativeProbabilityT(tMid, degreesOfFreedom);
def iterations = fold i = 0 to maxIterations with pMida =double.nan
while AbsValue(CumulativeProbabilityT((if pMid < probability then tMid else tLow + if pMid > probability then tMid else tHigh)/2.0, degreesOfFreedom) - probability) > tolerance do
CumulativeProbabilityT( ((if pMid < probability then tMid else tLow)+ (if pMid < probability then tHigh else tMid)) / 2.0, degreesOfFreedom)+1;
plot return = if iterations < maxIterations then tMid else Double.NaN;
}
script GrubbsTest {
input data = close; # Data points for which Grubbs' Test is calculated
input significanceLevel = 0.05; # Significance level for outlier detection
Input N = 10;
# def n = BarNumber(); # Number of data points
def sum = CompoundValue(1, if !isNaN(close) then sum[1] + data else sum[1], 0);
# def mean = sum / n; # Mean of the dataset
# def variance = CompoundValue(1, if !isNaN(close) then variance[1] + ( data - mean) * ( data - mean) else variance[1], 0);
# def AVvariance = variance / n;
# def stdev_ = Sqrt(AVvariance) ; # Standard deviation of the dataset
def mean = Average(data, n); # Mean of the dataset
def stdev_ = StDev(data); # Standard deviation of the dataset
# Calculate the Grubbs' Test statistic
def G = AbsValue((highestall(data) - mean) / stdev_);
# Calculate the critical value for outlier detection
def iscriticalValue = Sqrt((n - 1) / (Sqr(n) * Tinv(probability = 1 - (significanceLevel / (2 * n)), degreesOfFreedom = n - 2) + 1));
plot GrubbsTestStat = G;
plot CriticalValue = iscriticalValue;
}

