Guest User

Untitled

a guest
Jun 22nd, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace newRegression
  6. {
  7.     public class LinearRegression
  8.     {
  9.         public double[,] V;
  10.         public double[] C;
  11.  
  12.         public bool Regress(double[] Y, double[,] X, double[] W)
  13.         {
  14.             int M = Y.Length;             // M = Number of data points
  15.             int N = X.Length / M;         // N = Number of linear terms
  16.             int NDF = M - N;              // Degrees of freedom
  17.  
  18.             if (NDF < 1)
  19.             {
  20.                 return false;
  21.             }
  22.             V = new double[N, N];
  23.             C = new double[N];
  24.             double[] B = new double[N];
  25.  
  26.  
  27.             for (int i = 0; i < N; i++)
  28.             for (int j = 0; j < N; j++)
  29.                 V[i, j] = 0;
  30.  
  31.  
  32.             for (int i = 0; i < N; i++)
  33.             {
  34.                 for (int j = 0; j < N; j++)
  35.                 {
  36.                     V[i, j] = 0;
  37.                     for (int k = 0; k < M; k++)
  38.                         V[i, j] = V[i, j] + W[k] * X[i, k] * X[j, k];
  39.                 }
  40.                 B[i] = 0;
  41.                 for (int k = 0; k < M; k++)
  42.                     B[i] = B[i] + W[k] * X[i, k] * Y[k];
  43.             }
  44.  
  45.             if (!SymmetricMatrixInvert(V))
  46.             {
  47.                 return false;
  48.             }
  49.  
  50.             for (int i = 0; i < N; i++)
  51.             {
  52.                 C[i] = 0;
  53.                 for (int j = 0; j < N; j++)
  54.                     C[i] = C[i] + V[i, j] * B[j];
  55.             }
  56.            
  57.             return true;
  58.         }
  59.  
  60.  
  61.         public bool SymmetricMatrixInvert(double[,] V)
  62.         {
  63.             int N = (int)Math.Sqrt(V.Length);
  64.             double[] t = new double[N];
  65.             double[] Q = new double[N];
  66.             double[] R = new double[N];
  67.             double AB;
  68.             int K, L, M;
  69.  
  70.             // Invert a symetric matrix in V
  71.             for (M = 0; M < N; M++)
  72.                 R[M] = 1;
  73.             K = 0;
  74.             for (M = 0; M < N; M++)
  75.             {
  76.                 double Big = 0;
  77.                 for (L = 0; L < N; L++)
  78.                 {
  79.                     AB = Math.Abs(V[L, L]);
  80.                     if ((AB > Big) && (R[L] != 0))
  81.                     {
  82.                         Big = AB;
  83.                         K = L;
  84.                     }
  85.                 }
  86.                 if (Big == 0)
  87.                 {
  88.                     return false;
  89.                 }
  90.                 R[K] = 0;
  91.                 Q[K] = 1 / V[K, K];
  92.                 t[K] = 1;
  93.                 V[K, K] = 0;
  94.                 if (K != 0)
  95.                 {
  96.                     for (L = 0; L < K; L++)
  97.                     {
  98.                         t[L] = V[L, K];
  99.                         if (R[L] == 0)
  100.                             Q[L] = V[L, K] * Q[K];
  101.                         else
  102.                             Q[L] = -V[L, K] * Q[K];
  103.                         V[L, K] = 0;
  104.                     }
  105.                 }
  106.                 if ((K + 1) < N)
  107.                 {
  108.                     for (L = K + 1; L < N; L++)
  109.                     {
  110.                         if (R[L] != 0)
  111.                             t[L] = V[K, L];
  112.                         else
  113.                             t[L] = -V[K, L];
  114.                         Q[L] = -V[K, L] * Q[K];
  115.                         V[K, L] = 0;
  116.                     }
  117.                 }
  118.                 for (L = 0; L < N; L++)
  119.                     for (K = L; K < N; K++)
  120.                         V[L, K] = V[L, K] + t[L] * Q[K];
  121.             }
  122.             M = N;
  123.             L = N - 1;
  124.             for (K = 1; K < N; K++)
  125.             {
  126.                 M = M - 1;
  127.                 L = L - 1;
  128.                 for (int J = 0; J <= L; J++)
  129.                     V[M, J] = V[J, M];
  130.             }
  131.             return true;
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment