Advertisement
cesarsouza

Untitled

Aug 6th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1.   /// <summary>
  2.         ///   Predicts the next observation occurring after a given observation sequence.
  3.         /// </summary>
  4.         public double Predict(double[] observations)
  5.         {
  6.             double probability;
  7.             double[][] obs = convert(observations);
  8.             return Predict(obs, 1, out probability)[0][0];
  9.         }
  10.  
  11.         /// <summary>
  12.         ///   Predicts the next observation occurring after a given observation sequence.
  13.         /// </summary>
  14.         public double[] Predict(double[][] observations)
  15.         {
  16.             double probability;
  17.             return Predict(observations, 1, out probability)[0];
  18.         }
  19.  
  20.         /// <summary>
  21.         ///   Predicts the next observation occurring after a given observation sequence.
  22.         /// </summary>
  23.         public double[] Predict(double[] observations, int next)
  24.         {
  25.             double probability;
  26.             double[][] obs = convert(observations);
  27.             return Predict(obs, next, out probability)[0];
  28.         }
  29.  
  30.         /// <summary>
  31.         ///   Predicts the next observation occurring after a given observation sequence.
  32.         /// </summary>
  33.         public double[][] Predict(double[][] observations, int next)
  34.         {
  35.             double probability;
  36.             return Predict(observations, next, out probability);
  37.         }
  38.  
  39.         /// <summary>
  40.         ///   Predicts the next observation occurring after a given observation sequence.
  41.         /// </summary>
  42.         public double[] Predict(double[] observations, int next, out double probability)
  43.         {
  44.             double[][] obs = convert(observations);
  45.             double[][] prediction = Predict(obs, next, out probability);
  46.             return Accord.Math.Matrix.Combine(prediction);
  47.         }
  48.  
  49.         /// <summary>
  50.         ///   Predicts the next observation occurring after a given observation sequence.
  51.         /// </summary>
  52.         public double[][] Predict(double[][] observations, int next, out double probability)
  53.         {
  54.             int states = States;
  55.             int T = next;
  56.  
  57.             double[,] A = Transitions;
  58.             double logLikelihood;
  59.  
  60.             double[][] prediction = new double[next][];
  61.             double[][] means = new double[states][];
  62.  
  63.             if (Dimension > 1)
  64.             {
  65.                 for (int i = 0; i < states; i++)
  66.                     means[i] = (B[i] as IMultivariateDistribution).Mean;
  67.             }
  68.             else
  69.             {
  70.                 for (int i = 0; i < states; i++)
  71.                     means[i] = new double[] { (B[i] as IUnivariateDistribution).Mean };
  72.             }
  73.  
  74.  
  75.             // Compute forward probabilities for the given observation sequence.
  76.             double[,] fw0 = ForwardBackwardAlgorithm.Forward(this, observations, out logLikelihood);
  77.  
  78.             // Create a matrix to store the future probabilities for the prediction
  79.             // sequence and copy the latest forward probabilities on its first row.
  80.             double[,] fwd = new double[T + 1, States];
  81.  
  82.  
  83.             // 1. Initialization
  84.             for (int i = 0; i < States; i++)
  85.                 fwd[0, i] = fw0[observations.Length - 1, i];
  86.  
  87.  
  88.             // 2. Induction
  89.             for (int t = 0; t < T; t++)
  90.             {
  91.                 double scale = 0;
  92.                 for (int i = 0; i < states; i++)
  93.                 {
  94.                     double sum = 0.0;
  95.                     for (int j = 0; j < states; j++)
  96.                         sum += fwd[t, j] * A[j, i];
  97.                     fwd[t + 1, i] = sum * B[i].ProbabilityFunction(means[i]);
  98.  
  99.                     scale += fwd[t + 1, i];
  100.                 }
  101.  
  102.                 scale /= Math.Exp(logLikelihood);
  103.  
  104.                 if (scale != 0) // Scaling
  105.                 {
  106.                     for (int i = 0; i < states; i++)
  107.                         fwd[t + 1, i] /= scale;
  108.                 }
  109.  
  110.  
  111.                 // Select most probable value
  112.                 double maxWeight = fwd[t + 1, 0];
  113.                 double sumWeight = maxWeight;
  114.                 prediction[t] = means[0];
  115.                 for (int i = 1; i < states; i++)
  116.                 {
  117.                     if (fwd[t + 1, i] > maxWeight)
  118.                     {
  119.                         maxWeight = fwd[t + 1, i];
  120.                         prediction[t] = means[i];
  121.                     }
  122.                 }
  123.  
  124.                 // Recompute log-likelihood
  125.                 logLikelihood = Math.Log(maxWeight);
  126.             }
  127.  
  128.             // Returns the sequence probability as an out parameter
  129.             probability = Math.Exp(logLikelihood);
  130.  
  131.             return prediction;
  132.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement