最小二乘法在简单线性情况下的例子,c#
来源:互联网 发布:mac不能缓冲优酷视频 编辑:程序博客网 时间:2024/05/21 18:46
google了一下,这回高数算补回来了。以下是大致思路
假如给定的实验数据点为(Xi,Yi),其中i=0,1,...n,那么 直线与数据点的偏差平方和为
要使得
取到极小值,则要求:
,
这两个式子是取得极小值的必要条件,具体运算的过程如下:
对该式求解得到:
<!--[if !vml]--><!--[endif]-->
public class LeastSquare
{
/// <summary>
/// To Draw Linear Least Square Fitting Curve
/// </summary>
/// <param name="g">The device on which to draw the curve</param>
/// <param name="lp">A list of point used to do the approximation</param>
public static void LeastSquare2(Graphics g, List<PointF> lp)
{
// Clear the client window with the white color
g.Clear(Color.White);
// Move the drawing origin to the point(200,200)
g.TranslateTransform(200, 200);
// Use FillEllipse method to draw each point as an ellipse
foreach (PointF p in lp)
{
g.FillEllipse(Brushes.Red, new RectangleF(p.X - 5.0f, p.Y - 5.0f, 10.0f, 10.0f));
}
int i;
float a, b, sumX, sumY2, sumY, sumXY;
sumX = 0.0f;
sumY2 = 0.0f;
sumY = 0.0f;
sumXY = 0.0f;
// To calculate as per the description of the Mathematical Formular
for (i = 0; i < lp.Count; i++)
{
sumY += lp[i].Y;
sumY2 += lp[i].Y * lp[i].Y;
sumX += lp[i].X;
sumXY += lp[i].X * lp[i].Y;
}
// Deduct the coefficients required to do the approximation using the mathematical formular
a = (lp.Count * sumXY - sumX * sumY) / (lp.Count * sumY2 - sumY * sumY);
b = (sumY2 * sumX - sumY * sumXY) / (lp.Count * sumY2 - sumY * sumY);
Pen newPen = new Pen(Color.Blue, 3.0f);
g.DrawLine(newPen, new PointF(0, -b / a), new PointF(360, (360 - b) / a));
}
}
{
/// <summary>
/// To Draw Linear Least Square Fitting Curve
/// </summary>
/// <param name="g">The device on which to draw the curve</param>
/// <param name="lp">A list of point used to do the approximation</param>
public static void LeastSquare2(Graphics g, List<PointF> lp)
{
// Clear the client window with the white color
g.Clear(Color.White);
// Move the drawing origin to the point(200,200)
g.TranslateTransform(200, 200);
// Use FillEllipse method to draw each point as an ellipse
foreach (PointF p in lp)
{
g.FillEllipse(Brushes.Red, new RectangleF(p.X - 5.0f, p.Y - 5.0f, 10.0f, 10.0f));
}
int i;
float a, b, sumX, sumY2, sumY, sumXY;
sumX = 0.0f;
sumY2 = 0.0f;
sumY = 0.0f;
sumXY = 0.0f;
// To calculate as per the description of the Mathematical Formular
for (i = 0; i < lp.Count; i++)
{
sumY += lp[i].Y;
sumY2 += lp[i].Y * lp[i].Y;
sumX += lp[i].X;
sumXY += lp[i].X * lp[i].Y;
}
// Deduct the coefficients required to do the approximation using the mathematical formular
a = (lp.Count * sumXY - sumX * sumY) / (lp.Count * sumY2 - sumY * sumY);
b = (sumY2 * sumX - sumY * sumXY) / (lp.Count * sumY2 - sumY * sumY);
Pen newPen = new Pen(Color.Blue, 3.0f);
g.DrawLine(newPen, new PointF(0, -b / a), new PointF(360, (360 - b) / a));
}
}
调用部分:
// Declare a list of points
List<PointF> lp = new List<PointF>();
// PointF array
PointF[] pf = new PointF[]{
new PointF(0.0f,68.0f),
new PointF(10.0f,73.1f),new PointF(20.0f,66.4f),
new PointF(30.0f,70.6f),new PointF(40.0f,64.6f),
new PointF(50.0f,68.8f),new PointF(60.0f,61.0f),
new PointF(70.0f,65.8f),new PointF(80.0f,60.4f),
new PointF(90.0f,61.0f)
};
// Using AddRange method of the list to add the pointf array to the end of the list
lp.AddRange(pf);
// Call the static metod LeastSquare2 of LeastSquare Class to proceed
LeastSquare.LeastSquare2(this.CreateGraphics(), lp);
List<PointF> lp = new List<PointF>();
// PointF array
PointF[] pf = new PointF[]{
new PointF(0.0f,68.0f),
new PointF(10.0f,73.1f),new PointF(20.0f,66.4f),
new PointF(30.0f,70.6f),new PointF(40.0f,64.6f),
new PointF(50.0f,68.8f),new PointF(60.0f,61.0f),
new PointF(70.0f,65.8f),new PointF(80.0f,60.4f),
new PointF(90.0f,61.0f)
};
// Using AddRange method of the list to add the pointf array to the end of the list
lp.AddRange(pf);
// Call the static metod LeastSquare2 of LeastSquare Class to proceed
LeastSquare.LeastSquare2(this.CreateGraphics(), lp);
- 最小二乘法在简单线性情况下的例子,c#
- 最小二乘法的线性拟合
- 最小二乘法的多元线性回归
- 深度学习-最小二乘法,简单线性回归
- 最小二乘法C#实现,简单代码
- C# 简单的例子
- 线性最小二乘法
- 线性最小二乘法
- 自定义线性表的简单例子
- 在 C# 平台中, 编写 Windows 服务的简单例子
- 统计学 简单的线性回归(Linear Regression) - 最小二乘法 (least square method)
- 在MFC下使用OpenGL的一个简单的例子
- 在MFC下使用OpenGL的一个简单的例子
- 基于最小二乘法的线性回归分析
- 关于在vp下dof模型驱动的简单例子
- 在.Net中使用AnyChart的简单例子(下)
- 关于在vp下dof模型驱动的简单例子
- 关于在vp下dof模型驱动的简单例子
- sql生成脚本里SET ANSI_NULLS ON什么意思
- 打造站长最喜爱开发平台 华夏互联新战略浮现
- ubuntu9 如何识别u盘
- Linux上对内存泄露的测试
- Asp.net URL重定向后css路径问题
- 最小二乘法在简单线性情况下的例子,c#
- 信息化分析:集团企业信息化规划和实施研究
- Weblogic JDBC 之灵异事件
- 利用jmap和heapAnalyzer对java进程进行内存使用率分析
- C语言 SDK编程之数据库编程--ODBC数据源 API的使用
- JavaScript 日历
- top命令
- GetTableFields --参数:传入要剔除的自增字段; '-1'-自动剔除自增字段; ''输出全部字段
- VC6升级到VC9,线程中自定义消息的升级方法