• 大小: 6KB
    文件类型: .cs
    金币: 2
    下载: 1 次
    发布日期: 2021-07-22
  • 语言: C#
  • 标签: C#  预测  

资源简介

实现了包括GM(1,1),线性回归预测,移动平均法等预测方法

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace 铁路运量预测及改扩建辅助程序
{
    class Forecast
    {
        //data有二列,第一列表示x值,第二列表示y值
        
        //一元线性回归分析预测返回相关系数
        public static double LinearRegression(double[] dataout double Aout double B)
        {
            double[] derivedData = new double[data.GetLength(0) + 1 data.GetLength(1) + 3];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                derivedData[i 0] = data[i 0];   //X
                derivedData[i 1] = data[i 1];   //Y
                derivedData[i 2] = data[i 0] * data[i 1];  //XY
                derivedData[i 3] = data[i 0] * data[i 0];  //XX
                derivedData[i 4] = data[i 1] * data[i 1];  //YY
                derivedData[derivedData.GetLength(0) - 1 0] += derivedData[i 0]; //X的累加
                derivedData[derivedData.GetLength(0) - 1 1] += derivedData[i 1]; //Y的累加
                derivedData[derivedData.GetLength(0) - 1 2] += derivedData[i 2]; //XY的累加
                derivedData[derivedData.GetLength(0) - 1 3] += derivedData[i 3]; //XX的累加
                derivedData[derivedData.GetLength(0) - 1 4] += derivedData[i 4]; //YY的累加
            }

            double xba = derivedData[derivedData.GetLength(0) - 1 0] / data.GetLength(0);
            double yba = derivedData[derivedData.GetLength(0) - 1 1] / data.GetLength(0);
            double Lxx = derivedData[derivedData.GetLength(0) - 1 3] - Math.Pow(derivedData[derivedData.GetLength(0) - 1 0] 2) / data.GetLength(0);
            double Lyy = derivedData[derivedData.GetLength(0) - 1 4] - Math.Pow(derivedData[derivedData.GetLength(0) - 1 1] 2) / data.GetLength(0);
            double Lxy = derivedData[derivedData.GetLength(0) - 1 2] - derivedData[derivedData.GetLength(0) - 1 0] * derivedData[derivedData.GetLength(0) - 1 1] / data.GetLength(0);
            double b = Lxy / Lxx;
            double a = yba - b * xba;
            A = a;
            B = b;
            return Lxy / Math.Sqrt(Lxx * Lyy);
        }
        public static double LinearRegression(double[] data double x out double r)
        {
            double a;
            double b;
            r = LinearRegression(data out a out b);
            return a + b * x;
        }
        //幂回归分析预测
        public static double PowerRegression(double[] data out double A out double B)
        {
            double[] derivedData = (double[])data.Clone();
            for (int i = 0; i < derivedData.GetLength(0); i++)
            {
                for (int j = 0; j < derivedData.GetLength(1); j++)
                {
                    derivedData[i j] = Math.Log(derivedData[i j]);
                }
            }
            double a;
            double b;
            double r;
            r = LinearRegression(derivedData out a out b);
            A = Math.Exp(a);
            B = b;
 

评论

共有 条评论