FinancialFormula Forecasting in MSChart C#

MSChart control is very powerful tool for data display and analyzing. One very powerful feature is FinancialFormula Forecasting in MSChart C#. We can, for example,  model data with Linear Regression by using FinancialFormula Forecasting. Here is an example:

chart1.Series.Clear();
chart1.Series.Add(“Over Time”);
chart1.Series.Add(“Trendline“);
chart1.Series.Add(“Polynomial”);
chart1.Series[“Over Time”].ChartType = SeriesChartType.Point;
chart1.Series[“Trendline“].ChartType = SeriesChartType.Line;
chart1.Series[“Polynomial”].ChartType = SeriesChartType.Line;

double[] X = {1.0,2.0,5.0,7.0, 10.0, 11.0, 15.0, 20.0 };
double[] Y = {-0.5, 3.1, 4.5, 8.8, 9.2, 10.5, 17.1, 22.5 };

for (int i = 0; i < X.Length; i++)
{

chart1.Series[“Over Time”].Points.AddXY(X[i], Y[i]);

}

chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, “Linear,2,false,false”, chart1.Series[“Over Time”], chart1.Series[“Trendline“]);

In given example, it is obtain linear regression by using FinancialFormula Forecasting.

Linear-Regression-FinancialFormula-Forecasting

Power:

chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, “Power, 0, false, false”, chart1.Series[“Over Time”], chart1.Series[“Trendline”]);

Polynomial:

chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, “Polynomial, 0, false, false”, chart1.Series[“Over Time”], chart1.Series[“Polynomial”]);

Exponential:

chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, “Exponential, 0, false, false”, chart1.Series[“Over Time”], chart1.Series[“Polynomial”]);

Logarithmic:

chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, “IsLogarithmic, 0, false, false”, chart1.Series[“Over Time”], chart1.Series[“Polynomial”]);

For some of above mentioned regressions set of data in the input series must be all positive, Exponential for example.

If we want to use regression, and not linear, but with higher order polynomial, we can give parameters in numbers:

2 – Linear
3 – Square
4 – Third order, etc.

For given set of the input data, non-linear regression with the polynomial of square gives following look:

Non-Linear-Regression-FinancialFormula-Forecasting

Corresponding code is (3 – for square polynomial):

chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, “3,2,false,false”, chart1.Series[“Over Time”], chart1.Series[“Trendline”]);

Take care to have enough points to display for particular regression, at least:

3 for linear
4 for square
5 for order of three, etc.

External links:

FinancialFormula Forecasting in MSChart C#, on Microsoft
FinancialFormula Forecasting in MSChart C#, on Stackoverflow

 

Posted in c-sharp-code-examples Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*