Are you curious about our Budgets Forecast Formula? Wait no more, this is how we calculated!
We have:
x: the last day of the budget period, unix timestamp
xValues: the days have passed of the budget period, unix timestamp
yValues: the money spent on the passed days of the budget period
This is the code we use:
double[] xValues = {20, 28, 31, 38, 40};
double[] yValues = {6, 7, 9, 15, 21};
double X = 0f;
double forecast = 0f;
double xAvg = 0f;
double yAvg = 0f;
double b = 0f;
double a = 0f;
double tempTop = 0f;
double tempBottom = 0f;
// x axis
for (int i = 0; i < xValues.Length; i++) {
xAvg += xValues[i];
}
xAvg /= xValues.Length;
// y axis
for (int i = 0; i < yValues.Length; i++) {
yAvg += yValues[i];
}
yAvg /= yValues.Length;
for (int i = 0; i < yValues.Length; i++) {
tempTop += (xValues[i] - xAvg) * (yValues[i] - yAvg);
tempBottom += Math.Pow(((xValues[i] - xAvg)), 2f);
}
b = tempTop / tempBottom;
a = yAvg - b * xAvg;
X = 30f;
forecast = a + b * X;
Read on: