public static double POISSON(int k, double lambda, bool state) { if (state == false) { return(Poisson.PMF(lambda, k)); } return(Poisson.CDF(lambda, k)); }
private static List <List <double> > GetStateDurationProbability(List <int> sojournMeans, int maxStateLength) { var stateDurationProbability = new List <List <double> >(sojournMeans.Count); var stateCounter = 0; foreach (var sojournMean in sojournMeans) { stateDurationProbability.Add(new List <double>(maxStateLength)); for (var stateDuration = 0; stateDuration < maxStateLength; stateDuration++) { stateDurationProbability[stateCounter].Add(Math.Log(Poisson.PMF(sojournMean, stateDuration))); } stateCounter++; } return(stateDurationProbability); }
/// <summary> /// BestHsmmPathViterbi helper method to calculate sojourn survival function /// </summary> /// <param name="maxStateLength"></param> /// <param name="means"></param> /// <returns></returns> public double[][] CalculateSojourn(int maxStateLength, List <int> means) { double[][] sojourn = CanvasCommon.Utilities.MatrixCreate(maxStateLength + 1, maxStateLength + 2); // Store D for (int j = 0; j < nStates; j++) { for (int u = 1; u < maxStateLength + 1; u++) { double x = 0; for (int v = u; v < maxStateLength + 2; v++) { x += Math.Log(Poisson.PMF(means[j], v)); } sojourn[j][u] = x; } sojourn[j][maxStateLength] = 0; } return(sojourn); }
private Dictionary <double, double> GetOverlayData(double step, Chart cherte) { var min = cherte.Series[0].Points.Min(x => x.XValue); var max = cherte.Series[0].Points.Max(x => x.XValue); var overlayData = new Dictionary <double, double>(); Func <int, double, double> g = (tb, x) => { switch (tabControl.SelectedIndex) { case 0: return(1); case 1: return(1 / (max - min)); case 2: return(Triangular.PDF(Convert.ToDouble(tb_tre_max.Text), Convert.ToDouble(tb_tre_min.Text), Convert.ToDouble(tb_tre_moda.Text), x)); case 3: return(Erlang.PDF(Convert.ToInt32(nUD_erl_kol_norm.Value), 1.0 / Convert.ToDouble(tb_erl_moda.Text), x)); case 4: return(Poisson.PMF(Convert.ToDouble(tb_puas_lambd.Text), (int)x)); case 5: return(Normal.PDF(Convert.ToDouble(tb_norm_m.Text), Convert.ToDouble(tb_norm_d.Text), x)); case 6: return(Exponential.PDF(1.0 / Convert.ToDouble(tb_exp_m.Text), x)); default: return(double.NaN); } }; for (var x = min; x <= max; x += step) { overlayData[x] = g(tabControl.SelectedIndex, x); } return(overlayData); }
/// <summary> /// HSMM Viterbi implementtion based on: /// Guedon, Y. (2003), Estimating hidden semi-Markov chains from discrete sequences, Journal of /// Computational and Graphical Statistics, Volume 12, Number 3, page 604-639 - 2003 /// </summary> /// <param name="x"></param> /// <param name="haploidMeans"></param> /// <returns></returns> public List <int> BestHsmmPathViterbi(List <List <double> > x, List <double> haploidMeans) { // Initialization var length = x.Count; var alpha = CanvasCommon.Utilities.MatrixCreate(nStates, length + 1); var bestStateDuration = new int[nStates][]; var bestStateIndex = new int[nStates][]; for (int i = 0; i < nStates; ++i) { bestStateIndex[i] = new int[length]; bestStateDuration[i] = new int[length]; } for (int j = 0; j < nStates; j++) { alpha[j][0] = this._stateProbabilities[j]; } var maxStateLength = 90; var sojournMeans = new List <int> { 10, 10, 80, 50, 50 }; var stateDurationProbability = GetStateDurationProbability(sojournMeans, maxStateLength); var sojournLastState = CalculateSojourn(maxStateLength, sojournMeans); double emissionSequence = 0; double tempEmissionSequence = 0; var bestState = 0; var firstState = true; var firstI = true; var transition = Enumerable.Repeat(1.0, nStates).ToArray(); // Induction for (int t = 1; t < length - 1; t++) { for (int j = 0; j < nStates; j++) { emissionSequence = 0; firstState = true; for (int stateDuration = 1; stateDuration < Math.Min(maxStateLength, t); stateDuration += 2) { firstI = true; for (int i = 0; i < nStates; i++) { if (i == j) { continue; } if (Math.Log(_transition[i][j]) + alpha[i][t - stateDuration] > tempEmissionSequence || firstI) { tempEmissionSequence = Math.Log(_transition[i][j]) + alpha[i][t - stateDuration]; bestState = i; firstI = false; } } if (firstState || emissionSequence + stateDurationProbability[j][stateDuration] + tempEmissionSequence > alpha[j][t]) { alpha[j][t] = emissionSequence + stateDurationProbability[j][stateDuration] + tempEmissionSequence; bestStateDuration[j][t] = stateDuration; bestStateIndex[j][t] = bestState; firstState = false; } emissionSequence += _emission.EstimateViterbiLikelihood(x[t - stateDuration], j, haploidMeans, transition); } if (t + 1 <= maxStateLength) { if (firstState || emissionSequence + Math.Log(Poisson.PMF(sojournMeans[j], t + 1) * _stateProbabilities[j]) > alpha[j][t]) { alpha[j][t] = emissionSequence + Math.Log(Poisson.PMF(sojournMeans[j], t + 1) * _stateProbabilities[j]); bestStateDuration[j][t] = -1; bestStateIndex[j][t] = -1; } } alpha[j][t] += _emission.EstimateViterbiLikelihood(x[t], j, haploidMeans, transition); } } for (int j = 0; j < nStates; j++) { emissionSequence = 0; firstState = true; for (int stateDuration = 1; stateDuration < maxStateLength - 1; stateDuration++) { firstI = true; for (int i = 0; i < nStates; i++) { if (i == j) { continue; } if (Math.Log(_transition[i][j]) + alpha[i][length - 1 - stateDuration] > tempEmissionSequence || firstI) { tempEmissionSequence = Math.Log(_transition[i][j]) + alpha[i][length - 1 - stateDuration]; bestState = i; firstI = false; } } if (emissionSequence + Math.Log(sojournLastState[j][Math.Min(stateDuration, maxStateLength)]) + tempEmissionSequence > alpha[j][length - 1] || firstState) { alpha[j][length - 1] = emissionSequence + Math.Log(sojournLastState[j][Math.Min(stateDuration, maxStateLength)]) + tempEmissionSequence; bestStateDuration[j][length - 1] = stateDuration; bestStateIndex[j][length - 1] = bestState; firstState = false; } emissionSequence += _emission.EstimateViterbiLikelihood(x[length - 1 - stateDuration], j, haploidMeans, transition); } if (emissionSequence + Math.Log(sojournLastState[j][Math.Min(length - 1, maxStateLength)] * _stateProbabilities[j]) > alpha[j][length - 1] || firstState) { alpha[j][length - 1] = emissionSequence + Math.Log(sojournLastState[j][Math.Min(length, maxStateLength)] * _stateProbabilities[j]); bestStateDuration[j][length - 1] = -1; bestStateIndex[j][length - 1] = -1; } alpha[j][length - 1] += _emission.EstimateViterbiLikelihood(x[length - 1], j, haploidMeans, transition); } // backtracking List <int> finalStates = Enumerable.Repeat(2, length).ToList(); int T = length - 1; while (bestStateIndex[bestState][T] >= 0) { for (int i = T; i >= T - bestStateDuration[bestState][T] + 1; i--) { finalStates[i] = bestState; } var alternativeBestState = bestState; bestState = bestStateIndex[bestState][T]; T -= bestStateDuration[alternativeBestState][T]; } finalStates.Reverse(); OutlierMask(finalStates); SmallSegmentsMask(finalStates); OversegmentationMask(finalStates); return(finalStates); }
public float ComputeProbabilityDensity(int value_domain) { return((float)Poisson.PMF(d_lambda, value_domain)); }