Example #1
0
        double P()
        {
            if (fb == null || fb.Count == 0)
            {
                throw new Exception("You have to call DoWork() first");
            }

            return(StateSpace.Sum(state => fb[state][T - 1]));
        }
Example #2
0
        public override double P()
        {
            if (fb == null || fb.Count == 0)
            {
                throw new Exception("You have to call DoWork() first");
            }

            return
                (StateSpace.Sum(
                     state =>
                     InitialProbabilitiesOfStates[state] * EmissionMatrix[state, SequenceOfObservations[0]] *
                     fb[state][0]));
        }
Example #3
0
        private void ValidateParameters()
        {
            if (EmissionMatrix == null || TransitionMatrix == null || SequenceOfObservations == null ||
                StateSpace == null || ObservationSpace == null)
            {
                throw new ArgumentException("Parameters cannot be null");
            }

            if (ObservationSpace.Length != EmissionMatrix.GetLength(1) || ObservationSpace.Length == 0)
            {
                throw new ArgumentException("N should be greater than 0 and consistent");
            }

#if _USE_ARRAYS_INSTEAD_OF_MATRIX_HASHTABLE
            if (StateSpace.Length != TransitionMatrix.GetLength(0) || TransitionMatrix.GetLength(0) != TransitionMatrix.GetLength(1) || TransitionMatrix.GetLength(1) != EmissionMatrix.GetLength(0) || EmissionMatrix.GetLength(0) != InitialProbabilitiesOfStates.Length || StateSpace.Length == 0)
            {
                throw new ArgumentException("K should be greater than 0 and consistent");
            }
#else
            if (StateSpace.Length != TransitionMatrix.GetLength(0) ||
                TransitionMatrix.GetLength(0) != TransitionMatrix.GetLength(1) ||
                TransitionMatrix.GetLength(1) != EmissionMatrix.GetLength(0) ||
                EmissionMatrix.GetLength(0) != InitialProbabilitiesOfStates.Count || StateSpace.Length == 0)
            {
                throw new ArgumentException("K should be greater than 0 and consistent");
            }
#endif
            if (SequenceOfObservations.Length == 0)
            {
                throw new ArgumentException("T should be greater than 0 and consistent");
            }

            if (
                StateSpace.Select(state => ObservationSpace.Sum(observation => EmissionMatrix[state, observation]))
                .Any(sum => sum <= 0.99 || sum >= 1.01))
            {
                throw new ArgumentException("EmissionMatrix has not normalized probabilities"); //Exception("Emi");
            }

            if (
                StateSpace.Select(state1 => StateSpace.Sum(state2 => TransitionMatrix[state1, state2]))
                .Any(sum => sum <= 0.99 || sum >= 1.01))
            {
                throw new ArgumentException("TransitionMatrix has not normalized probabilities"); //Exception("Emi");
            }
        }
        public override void DoWork()
        {
            base.DoWork();

            AlphaPass();
            BetaPass();

            _probabilitySuffix = StateSpace.Sum(
                state =>
                InitialProbabilitiesOfStates[state] * EmissionMatrix[state, SequenceOfObservations[0]] *
                beta[state][0]);
            _probabilityPrefix = StateSpace.Sum(state => alpha[state][T - 1]);

            var TOLERANCE = 1e-3;

            if (Math.Abs((_probabilitySuffix - _probabilityPrefix) / _probabilityPrefix) > TOLERANCE)
            {
                throw new Exception("Probabilities are not equal");
            }

            // Probability = (_probabilitySuffix + _probabilityPrefix)/2.0;

            LogProbability = 0.0;


            for (int i = 0; i < T; i++)
            {
                LogProbability -= Math.Log(c[i]);
            }
            Probability = Math.Exp(LogProbability);

            Output = new TState[T];

            for (var i = 0; i < T; i++)
            {
                var mostProbableState = StateSpace.Aggregate(
                    (s1, s2) =>
                    alpha[s1][i] * beta[s1][i] >
                    alpha[s2][i] * beta[s2][i]
                            ? s1
                            : s2);
                Output[i] = mostProbableState;
            }
        }
Example #5
0
 public override void DoWork()
 {
     base.DoWork();
     foreach (var state in StateSpace)
     {
         fb[state][T - 1] = 1;
         //InitialProbabilitiesOfStates[state] * EmissionMatrix[state, SequenceOfObservations[T - 1]];
     }
     for (var i = T - 2; i >= 0; i--)
     {
         foreach (var kState in StateSpace)
         {
             fb[kState][i] =
                 StateSpace.Sum(
                     lState =>
                     TransitionMatrix[kState, lState] * EmissionMatrix[lState, SequenceOfObservations[i + 1]] *
                     fb[lState][i + 1]);
         }
     }
 }