Bar Info
Menu
Debugging
Menu
Derivatives
Menu
Matrix Handling
Menu
Price Rounding
Menu
Regression Lines
Menu
Statistics
Menu
User Tracking
Menu
Example Strategies
Menu
Quadratic Regression
Description:
Calculate the best fit quadratic regression line given a set of price values.
Syntax:
QuadraticRegression(List y);
Arguments:
y: A provided list of inputs along the y axis.
Notes:
This method assumes the x axis to increase in increments of 1.
It is known that there are more accurate methods for find the best fit quadratic regression line. We are currently developing this and we hope to update soon.
///
/// Calculates the best fit quadratic regression of a set of data.
///
/// The y values of the input data.
/// A list of the regression values from start to finish.
private List QuaraticRegression(List y)
{
int n = y.Count;
// set all needed lists
List x = new List();
List x2 = new List();
List x3 = new List();
List x4 = new List();
List xy = new List();
List x2y = new List();
// centers x values based off of number of y inputs
int zero = Convert.ToInt32(y.Count / 2);
if (n % 2 == 0)
{
for (int i = 0; i < zero; i++)
x.Add(i * -1);
for (int i = 1; i <= zero; i++)
x.Add(i);
x.Sort();
}
else
{
for (int i = 0; i <= zero; i++)
x.Add(i * -1);
for (int i = 1; i <= zero; i++)
x.Add(i);
x.Sort();
}
// calculate each needed list
for (int i = 0; i < n; i++)
{
double xVal = x[i];
x2.Add(Math.Pow(xVal, 2));
x3.Add(Math.Pow(xVal, 3));
x4.Add(Math.Pow(xVal, 4));
x2y.Add(x2[i] * y[i]);
xy.Add(xVal * y[i]);
}
// build needed matricies for using Least Squares Method quadratic regression
double[,] num = {
{ x2y.Sum() },
{ xy.Sum() },
{ y.Sum() }
};
double[,] den = {
{ x4.Sum() , x3.Sum() , x2.Sum() },
{ x3.Sum() , x2.Sum() , x.Sum() },
{ x2.Sum() , x.Sum() , x.Count }
};
// matrix of minors
double[,] minors = {
{ den[1,1] * den[2,2] - den[1,2] * den[2,1] , den[1,0] * den[2,2] - den[1,2] * den[2,0] , den[1,0] * den[2,1] - den[1,1] * den[2,0] },
{ den[0,1] * den[2,2] - den[0,2] * den[2,1] , den[0,0] * den[2,2] - den[0,2] * den[2,0] , den[0,0] * den[2,1] - den[0,1] * den[2,0] },
{ den[0,1] * den[1,2] - den[0,2] * den[1,1] , den[0,0] * den[1,2] - den[0,2] * den[1,0] , den[0,0] * den[1,1] - den[0,1] * den[1,0] }
};
// matrix of coefitients
double[,] cofs = {
{ minors[0,0] , minors[0,1] * -1 , minors[0,2] },
{ minors[1,0] * -1 , minors[1,1] , minors[1,2] * -1 },
{ minors[2,0] , minors[2,1] * -1 , minors[2,2] }
};
// adjusted matrix
double[,] adj = {
{ cofs[0,0] , cofs[1,0] , cofs[2,0] },
{ cofs[0,1] , cofs[1,1] , cofs[2,1] },
{ cofs[0,2] , cofs[1,2] , cofs[2,2] }
};
// determinate
double d = den[0, 0] * minors[0, 0] + den[0, 1] * minors[0, 1] + den[0, 2] * minors[0, 2];
// inverted matrix
double[,] inv = {
{ adj[0,0] / d , adj[0,1] / d , adj[0,2] / d },
{ adj[1,0] / d , adj[1,1] / d , adj[1,2] / d },
{ adj[2,0] / d , adj[2,1] / d , adj[2,2] / d }
};
// calculate constants
double a = inv[0, 0] * num[0, 0] + inv[0, 1] * num[1, 0] + inv[0, 2] * num[2, 0];
double b = inv[1, 0] * num[0, 0] + inv[1, 1] * num[1, 0] + inv[1, 2] * num[2, 0];
double c = inv[2, 0] * num[0, 0] + inv[2, 1] * num[1, 0] + inv[2, 2] * num[2, 0];
// set list of function values
List fx = new List();
for (int i = 0; i < n; i++)
fx.Add(a * Math.Pow(x[i], 2) + (b * x[i]) + c);
/* find reliability of graph using R^2 method
double sse = 0;
double sst = 0;
for (int i = 0; i < n; i++)
{
sse += Math.Pow(y[i] - (a * Math.Pow(x[i], 2) + b * x[i] + c), 2);
sst += Math.Pow(y[i] - y.Average(), 2);
}
double r2 = 1 - sse / sst;
*/
return fx;
}