FittingFunction.cs 9.8 KB
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App
{
    class FittingFunction
    {
        /** 
         * 多项式拟合函数,输出系数是y=a0+a1*x+a2*x*x+.........,按a0,a1,a2输出 
         *  
         * @param y 
         * @param x 
         * @param order 
         * @return 
         */
        public static double[] polyfit(double[] y, double[] x, int order)
        {
            double[,] guass = get_Array(y, x, order);


            double[] ratio = cal_Guass(guass, order + 1);


            return ratio;
        }


        /** 
         * 一次拟合函数,y=a0+a1*x,输出次序是a0,a1 
         *  
         * @param y 
         * @param x 
         * @return 
         */
        public static double[] linear(double[] y, double[] x)
        {
            double[] ratio = polyfit(y, x, 1);
            return ratio;
        }


        /** 
         * 对数拟合函数,.y= c*(ln x)+b,输出为b,c 
         *  
         * @param y 
         * @param x 
         * @return 
         */
        public static double[] Logest(double[] y, double[] x)
        {
            double[] lnX = new double[x.Length];


            for (int i = 0; i < x.Length; i++)
            {
                if (x[i] == 0 || x[i] < 0)
                {
                    //System.out.println("正对非正数取对数!");  
                    return null;
                }
                lnX[i] = Math.Log(x[i]);
            }


            return linear(y, lnX);
        }


        /** 
         * 幂函数拟合模型, y=c*x^b,输出为c,b 
         *  
         * @param y 
         * @param x 
         * @return 
         */
        public static double[] PowEST(double[] y, double[] x)
        {
            double[] lnX = new double[x.Length];
            double[] lnY = new double[y.Length];
            double[] dlinestRet;


            for (int i = 0; i < x.Length; i++)
            {
                lnX[i] = Math.Log(x[i]);
                lnY[i] = Math.Log(y[i]);
            }


            dlinestRet = linear(lnY, lnX);


            dlinestRet[0] = Math.Exp(dlinestRet[0]);


            return dlinestRet;
        }


        /** 
         * 指数函数拟合函数模型,公式为 y=c*m^x;输出为 c,m 
         *  
         * @param y 
         * @param x 
         * @return 
         */
        public static double[] indexEST(double[] y, double[] x)
        {
            double[] lnY = new double[y.Length];
            double[] ratio;
            for (int i = 0; i < y.Length; i++)
            {
                lnY[i] = Math.Log(y[i]);
            }


            ratio = linear(lnY, x);


            for (int i = 1; i < ratio.Length; i++)
            {
                ratio[i] = Math.Exp(ratio[i]);
            }
            return ratio;
        }


        /** 
         * 最小二乘法部分, 计算增广矩阵 
         *  
         * @param guass 
         * @param count 
         * @return 
         */
        private static double[] cal_Guass(double[,] guass, int count)
        {
            double temp;
            double[] x_value;


            for (int j = 0; j < count; j++)
            {
                int k = j;
                double min = guass[j, j];


                for (int i = j; i < count; i++)
                {
                    if (Math.Abs(guass[i, j]) < min)
                    {
                        min = guass[i, j];
                        k = i;
                    }
                }


                if (k != j)
                {
                    for (int x = j; x <= count; x++)
                    {
                        temp = guass[k, x];
                        guass[k, x] = guass[j, x];
                        guass[j, x] = temp;
                    }
                }


                for (int m = j + 1; m < count; m++)
                {
                    double div = guass[m, j] / guass[j, j];
                    for (int n = j; n <= count; n++)
                    {
                        guass[m, n] = guass[m, n] - guass[j, n] * div;
                    }
                }
            }
            x_value = get_Value(guass, count);


            return x_value;
        }


        /** 
         * 回带计算X值 
         *  
         * @param guass 
         * @param count 
         * @return 
         */
        private static double[] get_Value(double[,] guass, int count)
        {
            double[] x = new double[count];
            double[,] X_Array = new double[count, count];


            for (int i = 0; i < count; i++)
                for (int j = 0; j < count; j++)
                    X_Array[i, j] = guass[i, j];


            if (2 < count - 1)// 表示有多解  
            {
                return null;
            }
            // 回带计算x值  
            x[count - 1] = guass[count - 1, count] / guass[count - 1, count - 1];
            for (int i = count - 2; i >= 0; i--)
            {
                double temp = 0;
                for (int j = i; j < count; j++)
                {
                    temp += x[j] * guass[i, j];
                }
                x[i] = (guass[i, count] - temp) / guass[i, i];
            }


            return x;
        }


        /** 
         * 得到数据的法矩阵,输出为发矩阵的增广矩阵 
         *  
         * @param y 
         * @param x 
         * @param n 
         * @return 
         */
        private static double[,] get_Array(double[] y, double[] x, int n)
        {
            double[,] result = new double[n + 1, n + 2];


            if (y.Length != x.Length)
            {
                //System.out.println("两个输入数组长度不一!");  
            }


            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j <= n; j++)
                {
                    result[i, j] = cal_sum(x, i + j);
                }
                result[i, n + 1] = cal_multi(y, x, i);
            }


            return result;
        }


        /** 
         * 累加的计算 
         *  
         * @param input 
         * @param order 
         * @return 
         */
        private static double cal_sum(double[] input, int order)
        {
            double result = 0;
            int Length = input.Length;


            for (int i = 0; i < Length; i++)
            {
                result += Math.Pow(input[i], order);
            }


            return result;
        }


        /** 
         * 计算∑(x^j)*y 
         *  
         * @param y 
         * @param x 
         * @param order 
         * @return 
         */
        private static double cal_multi(double[] y, double[] x, int order)
        {
            double result = 0;


            int Length = x.Length;


            for (int i = 0; i < Length; i++)
            {
                result += Math.Pow(x[i], order) * y[i];
            }


            return result;
        }


        public static double calRSquare_linear(double[] x, double[] y, double a, double b)
        {


            int num = y.Length;
            double[] yLine = new double[num];
            for (int i = 0; i < num; i++)
            {
                yLine[i] = x[i] * b + a;
            }


            return calRSquare(x, y, yLine, a, b);
        }


        public static double calRSquare_Logest(double[] x, double[] y, double a, double b)
        {


            int num = y.Length;
            double[] yLine = new double[num];
            for (int i = 0; i < num; i++)
            {
                yLine[i] = Math.Log(x[i]) * b + a;
            }


            return calRSquare(x, y, yLine, a, b);
        }


        public static double calRSquare_PowEST(double[] x, double[] y, double c, double b)
        {


            int num = y.Length;


            double[] yLine = new double[num];


            for (int i = 0; i < num; i++)
            {
                // y=c*x^b,输出为c,b  
                yLine[i] = c * Math.Pow(x[i], b);
            }


            return calRSquare(x, y, yLine, c, b);
        }


        public static double calRSquare_indexEST(double[] x, double[] y, double c, double m)
        {


            int num = y.Length;
            double[] yLine = new double[num];
            for (int i = 0; i < num; i++)
            {
                // y=c*m^x  
                yLine[i] = c * Math.Pow(m, x[i]);
            }


            return calRSquare(x, y, yLine, c, m);
        }


        /** 
         * 计算R?值:R?=1-SSE/SST。SSE为实际值减去预测值的平方和;SST为实际值减去平均值的平方和 
         *  
         * @param x 
         *            实际坐标x的值数组 
         * @param y 
         *            实际坐标y的值数组 
         * @param yLine 
         *            预测线坐标y的值数组 
         * @param a 
         *            前一个参数 
         * @param b 
         *            后一个参数 
         * @return 
         */
        private static double calRSquare(double[] x, double[] y, double[] yLine,
                double a, double b)
        {


            // 1、获取线性函数对应的数组值  
            int num = y.Length;


            // 2、y平均值  
            double sum = 0;
            foreach (double yi in y)
            {
                sum = sum + yi;
            }
            double yAverage = sum / num;


            // 3、计算实际值减去预算值的平方和  
            double sse = 0;
            for (int i = 0; i < num; i++)
            {
                sse = sse + (y[i] - yLine[i]) * (y[i] - yLine[i]);
            }


            // 4、计算实际值减去平均值的平方和  
            double sst = 0;
            for (int i = 0; i < num; i++)
            {
                sst = sst + (y[i] - yAverage) * (y[i] - yAverage);
            }


            return 1 - sse / sst;
        }
    }
}