Forecast 数据预测
/// <summary>
/// 数据预测
/// </summary>
/// <param name="xValues">数据序列</param>
/// <param name="yValues">数据值</param>
/// <param name="forecastPoint">第N个预测序列</param>
/// <returns>返回第N个序列预测值</returns>
public double Forecast(double[] xValues, double[] yValues, double forecastPoint)
{
//数组平均值
var xAverage = xValues.Average();
var yAverage = yValues.Average();
//预测值上下区间
var bounds = yValues
.Select((y, i) => new { Value = y, Index = i })
.Aggregate(new { Top = 0.0, Bottom = 0.0 }, (acc, cur) =>
new
{
//上限值
Top = acc.Top + (xValues[cur.Index] - xAverage) * (yValues[cur.Index] - yAverage),
//下限值
Bottom = acc.Bottom + Math.Pow(xValues[cur.Index] - xAverage, 2.0)
});
var level = bounds.Top / bounds.Bottom;
//区中预测值
return (yAverage - level * xAverage) + level * forecastPoint;
}
测试调用:
//测试数据
double[] xList = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
double[] yList = new double[] { 836.43, 831.68, 836.03, 835.94, 836.61, 835.8, 835.2, 834.35, 835.93, 835.41, 835.75, 838.15, 835.19, 836.64, 746.17, 669.6, 666.57, 679.41, 679.99, 684.96 };
//根据已知20个值预测未来10个值
for (int i = 1; i <= 10; i++)
{
//调用主方法
double foreCastValue = Forecast(xList, yList, 20 + i);
//打印输出
Response.Write(foreCastValue.ToString("F4") + "\r\n");
}
MathNet.Numerics 学习分享:
原文地址: https://blog.csdn.net/zyyujq/article/details/123215130
MathNet.Numerics主要类功能简述