C# chart.DataManipulator.FinancialFormula()公式的使用 线性回归预测方法

最近翻阅资料,找到
chart.DataManipulator.FinancialFormula()公式的使用,打开另一扇未曾了解的窗,供大家分享一下。


一 DataManipulator类


运行时,执行数据操作。此类是通过chart中DataManipulator属性对外公开的。

在C#中的继承关系如下:

System.Object

System.Web.UI.DataVisualization.Charting.DataFormula

System.Web.UI.DataVisualization.Charting.DataManipulator

《命名空间:System.Web.UI.DataVisualization.Charting》

《程序集:System.Web.DataVisualization(在
System.Web.DataVisualization.dll 中)》

在DataManipulator属性中囊括了很多数学计算方法(大多都是针对图表的数据序列展开的)以下是这样一个列子:

double result =
Chart1.DataManipulator.Statistics.Mean("Series1");平均值函数

double result =
Chart1.DataManipulator.Statistics.Median("Series1");中值函数

StatisticFormula
类计算统计公式。

二、DataFormula.FinancialFormula
方法

使用指定的参数从公式模块调用方法。

重载此成员。有关此成员的完整信息(包括语法、用法和示例),请单击重载列表中的相应名称。

在这里特别讲到的是预测函数功能的实现情况。

预测公式尝试根据历史数据找出拟合度最佳的回归函数,然后根据最拟合的函数预测最可能的未来数据值。

Chart.DataManipulator.FinancialFormula(
   
FinancialFormula.Forecasting,
   
"RegressionType,Period,ApproxError,ForecastError",
   
"Historical",
   
"Forecast,UpperError,LowerError")

三 预测函数的语法分析说明

此公式采用四个可选参数。

RegressionType


回归类型。使用一个数字来指示特定次数的多元回归,或者使用以下值之一指定不同的回归类型:Linear、Exponential、Logarithmic、Power。默认值为 2,与指定
Linear 等效。


Period

预测时段。公式会预测此指定的未来天数内的数据变化。默认值为序列长度的一半。


ApproxError

是否输出近似误差。如果设置为 false,则输出误差序列不包含相应历史数据的数据。默认值为 true。


ForecastError

是否输出预测误差。如果设置为 false,并且 ApproxError 设置为 true,则输出误差序列将包含所有预测数据点的近似误差。默认值为 true。

输入值:

此公式采用一个输入 Y
值。

Historical:用于预测的历史数据。

输出值:

此公式输出三个 Y
值。

Forecast:预测测值。

UpperError:上限误差。


LowerError:下限误差。

?





1

2

下面的示例以 Series1 (Series1:Y) 作为输入,在 Series2 上输出预测值 (Series2:Y),在 Series3 上输出误差范围(Series3:Y、Series3:Y2)。该示例采用二次多元回归,预测期间为 40 天。

Chart1.DataManipulator.FinancialFormula (FinancialFormula.Forecasting, "2,40,true,true", "Series1:Y", "Series2:Y,Series3:Y,Series3:Y2");

在编程的过程中,注意原始历史数据的输入,要求X轴的间隔是一定的,否则影响数据的回归分析

四:以下是自己编写的线性回归预测方法的一个公用类

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

using
System.Drawing;

using
System.Windows.Forms.DataVisualization.Charting;

using
System;

namespace
WindowsFormsApplication8

{

    class
RegressionModelClass

    {

        #region  字段

        double[] sourceData_X = new
double[4];  // 样本数据 X 轴坐标值

        double[] sourceData_Y = new
double[4];  // 样本数据 Y 轴坐标值

        double[] predictData_Y = new
double[8]; // 预测的未来数据的 Y 轴坐标值

        int
n = 4;                              // 样本数据的个数

        // Chart

        System.Windows.Forms.DataVisualization.Charting.Chart chart_temp = new
System.Windows.Forms.DataVisualization.Charting.Chart();

        #endregion

        /// <summary>

        /// 构造函数

        /// </summary>

        public
RegressionModelClass(double[] data_x, double[] data_y)

        {

            for
(int
i = 0; i < n;i++)

            {

                sourceData_X[i] = data_x[i];

                sourceData_Y[i] = data_y[i];

            }

            InitialChart(chart_temp, sourceData_X, sourceData_Y);

        }

        // 初始化 Chart 控件

        private
void InitialChart(System.Windows.Forms.DataVisualization.Charting.Chart chart, double[] data_x, double[] data_y)

        {

            #region  1. Title 设置

            Title title = new
Title();  //* 实例化

            title.Text = "信息预测";

            //** 关联

            chart.Titles.Add(title);  //* 当使用这种重载方式时,可以将属性传递

            #endregion

            #region 2. ChartArea 设置

            ChartArea chartarea1 = new
ChartArea();  //* 实例化

            chartarea1.Name = "chartarea1";     //* ChartArea 的唯一名称

            // 关联

            chart.ChartAreas.Add(chartarea1);  //重要//使用这种重载方法

            #endregion

            #region 3. 坐标轴设置

            #region  3.1 X轴

            Axis axis_X = new
Axis();

            axis_X.IntervalType = DateTimeIntervalType.Days; 

            axis_X.Title = "时 间"//* 轴的标题

            // ** 关联

            chart.ChartAreas[0].AxisX = axis_X;

            chart.ChartAreas[0].AxisX.Enabled = AxisEnabled.True;

            #endregion

            #region   3.2.1  深度 -- Y 轴

            Axis axisY_depth = new
Axis();

            axisY_depth.Title = "深度";

            axisY_depth.LineColor = Color.Black;

            axisY_depth.ArrowStyle = AxisArrowStyle.None;

            axisY_depth.TextOrientation = TextOrientation.Stacked;

            axisY_depth.TitleFont = new
Font("微软雅黑", 14F, FontStyle.Bold);

            axisY_depth.TitleForeColor = Color.Black;

            axisY_depth.TitleAlignment = StringAlignment.Far;

            axisY_depth.IsLabelAutoFit = false;

            axisY_depth.IntervalType = DateTimeIntervalType.Number;

            axisY_depth.IsStartedFromZero = false;

            axisY_depth.Minimum = 0;

            axisY_depth.Maximum = 10;

            axisY_depth.IntervalAutoMode = IntervalAutoMode.FixedCount;

            axisY_depth.InterlacedColor = Color.Red;

            // ** 关联

            chart.ChartAreas[0].AxisY = axisY_depth;

            chart.ChartAreas[0].AxisY.Enabled = AxisEnabled.True;

            #endregion

            #endregion

            #region 4. Series 设置

            Series series = new
Series();

            series.Name = "样本数据曲线";

            series.ChartType = SeriesChartType.Line;

            series.XAxisType = AxisType.Primary;

            series.YAxisType = AxisType.Primary;

            // important

            series.XValueType = ChartValueType.DateTime;

            series.YValueType = ChartValueType.Double;

            series.Enabled = true;

            //关联

            series.ChartArea = chart.ChartAreas[0].Name;

            chart.Series.Clear();

            chart.Series.Add(series);  // 注意要使用这个重载方法,不应该使用 Add(string)重载方法

            #endregion

            #region 5. Points 设置

            // 清除所有数据点

            chart.Series[0].Points.Clear();

            // 添加数据点

            int
m = data_x.Length;

            for
(int
i = 0; i < m; i++)

            {

                chart.Series[0].Points.AddXY(data_x[i], data_y[i]);

            }

            #endregion

        }

        /// <summary>

        /// 得到基于回归分析预测的数据

        /// </summary>

        ///

        public
double[] GetPredictData()

        {

            Series trendSeries = new
Series();

            trendSeries.Name = "trend";

            trendSeries.ChartType = SeriesChartType.Line;

            

            // 关联

            trendSeries.ChartArea = chart_temp.ChartAreas[0].Name;

            chart_temp.Series.Add(trendSeries);

            string
typeRegression = "2";

            // The number of days for Forecasting (备注:该数字对应的单位与X轴的数据间隔单位有关,并不一定是“天”)

            string
forecasting = "4";

            string
error = "false";

            string
forecastingError = "false";

            string
parameters = typeRegression + ‘,‘
+ forecasting + ‘,‘
+ error + ‘,‘
+ forecastingError;

            chart_temp.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, parameters, chart_temp.Series[0], chart_temp.Series["trend"]);

            for
(int
i = 0; i < 8; i++)  // 共4个预测值

            {

                predictData_Y[i] = Math.Round(chart_temp.Series["trend"].Points[i].YValues[0], 5);   // chart.Series["trend"]共8个数据点

            }

            return
predictData_Y;

        }

    }

}

C# chart.DataManipulator.FinancialFormula()公式的使用
线性回归预测方法

时间: 2024-07-29 13:19:52

C# chart.DataManipulator.FinancialFormula()公式的使用 线性回归预测方法的相关文章

PowerPoint2007 执行VBA代码&#39;.Shapes(ShapeIdx).Chart&#39; 报438错误 不知道该对象或方法

因为之前木有这个问题的 重装了下Office就出现这个问题了 百度木有百到 然后看到一位仁兄这样说: 我想是不是微软不给力 需要打补丁 于是安装了下面补丁 果然好了: PowerPoint2007 执行VBA代码'.Shapes(ShapeIdx).Chart' 报438错误 不知道该对象或方法

线性回归预测波士顿房价

预测波士顿的房价,上次已经通过房间数目预测了房价,这次用多元线性回归预测. 根据之前推导的多元线性回归的参数 接下来是多元线性回归的代码实现 def LinearRegression_(x,y): np.array(x) np.array(y) a = (np.linalg.inv(x.T.dot(x))).dot(x.T).dot(y) 上次大致了解了得个feature的name.下面是‘ZN’和‘RM’的散点图(由于我比较懒所以只实现这两个) 我们可以看出每个特征的数据范围相差较大,为了加快

降维算法中的线性判别方法LDA

线性判别分析(Linear?Discriminant?Analysis,?LDA),有时也称Fisher线性判别(Fisher?Linear?Discriminant?,FLD),?这种算法是Ronald?Fisher?于?1936年发明的,是模式识别的经典算法.在1996年由Belhumeur引入模式识别和人工智能领域的.基本思想是将高维的模式样本投影到最佳鉴别矢量空间,以达到抽取分类信息和压缩特征空间维数的效果,投影后保证模式样本在新的子空间有最大的类间距离和最小的类内距离,即模式在该空间中

SPSS-如何进行多元线性回归预测

http://jingyan.baidu.com/article/4e5b3e1955c89391901e24d0.html 在数据分析行业内,最困难的一项工作就是对未来的某项变化进行预测,以下给各位分享如何利用多元线性回归模型对因变量进行预测: 步骤: 建立预测模型:这里模型为:本例中收集了某地区过去16年的蛾量.卵量.降水量.雨日以及幼虫密度的历史数据,这里蛾量.卵量.降水量和雨日可以统计得到,因此需要这4个自变量来预测因变量幼虫密度,这里建立模型Y=a+x1*b1+x2*b2+x3*b3+

[What-Why-How] 线性回归预测

What 现有多个变量X1, X2, X3, ....会对结果数据Y产生影响,现在要求出这些变量Xn对于最终结果的影响权重.找到一个线(两个变量),面(三个变量)来拟合这些权重的数值.通过训练数据得到这些参数,然后使用这些参数(模型)对新数据进行预测 例如,拟合一个平面: 其中 θ0表示预置的权重参数. 误差 真实值和预测值之间肯定是要存在差异的 误差是独立并且具有相同分布,并且服从均值为0方差为θ2的高斯分布(正态分布) 似然函数:,什么样的参数跟我们的数据组合后恰好时真实值.  样本数据 -

tensorflow线性回归预测鲍鱼数据

代码如下: import tensorflow as tf import csv import numpy as np import matplotlib.pyplot as plt # 设置学习率 learning_rate = 0.01 # 设置训练次数 train_steps = 1000 #数据地址:http://archive.ics.uci.edu/ml/datasets/Abalone with open('./data/abalone.data') as file: reader

根据a(n)/a(n-1)的无理数极限逆推二阶线性递推数列公式

首先看这样一道题目: a(n)=6*a(n-1)-a(n-2),a1=1,a2=5,求b(n)=a(n+1)/a(n)的极限  数列通项两边除以a(n-1) 得: a(n)/a(n-1)=6-a(n-1)/a(n-2) 根据单调有界定理可以证明极限存在 单调性可以用数学归纳法证明,不再赘述 设极限为x 则x=6-1/x x^2-6*x+1=0 解一元二次方程得 x=3+2√2 我举这个例子,是因为,这个例子和2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络E题的数列很像,只不过在后面减了个

R语言中的线性判别分析

在R语言中,线性判别分析(Liner Discriminant Analysis,简称LDA),依靠软件包MASS中有线性判别函数lqa()来实现.该函数有三种调用格式: 1)当对象为数据框data.frame时 lda(x,grouping,prior = propotions,tol = 1.0e-4,method,CV = FALSE,nu,...) 2) 当对象为公式Formula时 lda(formula,data,...,subnet,na.action) 3) 当对象为矩阵Matr

模式识别(Pattern Recognition)学习笔记(七)——线性分类器及线性判别函数

1.为什么要设计分类器? 回顾下前面学习的统计决策,也就是贝叶斯决策,它可以简单被划分为两步,首先根据样本进行PDF估计,然后根据估计出的PDF来求分类面,因此又经常被叫做两步贝叶斯决策.如果我们能够很好地估计出PDF模型,也总可以利用贝叶斯来实现两类甚至多类的最优分类,但是很多实际情形中,想要精准的估计出PDF模型,并非易事,尤其当样本存在高维特征空间,以及样本数量并不足够多的情况,本质上来说,模式识别的真正目的并非估计PDF模型,而是在特征空间中想方设法找到各类的分界线或分界面.因此,如果可