/** * Computes the conditional probability between a collection of objects and a new object. * @param prior The collection of objects that's conditioned on. If conditional probability is thought of as "A given B", this is the B. * @param posterior The new object; finding its probability given the prior. This is the "A" in the conditional probability definition. * @param priorDistribution The distribution which the prior is over. * @param posteriorDistribution The distribution which the posterior is over. If the prior contains n elements, this is a distribution over collections of size n+1 * @return The double representing the conditional probability. */ public double computeConditionalProbability(Collection<DataType> prior, DataType posterior, ComputableDistribution<Collection<DataType>> priorDistribution, ComputableDistribution<Collection<DataType>> posteriorDistribution) { Collection<DataType> realPosterior = new ArrayList<DataType>(prior); realPosterior.add(posterior); double numerator = posteriorDistribution.getProbabilityFunction().evaluate(realPosterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super ObservationType> output) { Vector p = this.getInitialProbability(); int state = -1; for( int n = 0; n < sampleCount; n++ ) { double value = random.nextDouble(); state = -1; while( value > 0.0 ) { state++; value -= p.getElement(state); } ObservationType sample = CollectionUtil.getElement( this.getEmissionFunctions(), state ).sample(random); output.add( sample ); p = this.getTransitionProbability().getColumn(state); } }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super ObservationType> output) { Vector p = this.getInitialProbability(); int state = -1; for( int n = 0; n < sampleCount; n++ ) { double value = random.nextDouble(); state = -1; while( value > 0.0 ) { state++; value -= p.getElement(state); } ObservationType sample = CollectionUtil.getElement( this.getEmissionFunctions(), state ).sample(random); output.add( sample ); p = this.getTransitionProbability().getColumn(state); } }
/** * Computes the conditional probability between two objects. This is used when the data type contains the historical data. * @param prior The object that's conditioning the posterior. If conditional probability is thought of as "A given B", this is the B. * @param posterior The object whose probability is based off the prior. This is the "A" in the conditional probability definition. * @param priorDistribution A distribution for the objects. * @param posteriorDistribution A distribution for all pairs of objects. * @return The double representing the conditional probability. */ public double computeConditionalProbabilityWhenDataTypeHasHistoricalData(DataType prior, DataType posterior, ComputableDistribution<DataType> priorDistribution, ComputableDistribution<DataType> posteriorDistribution) { double numerator = posteriorDistribution.getProbabilityFunction().evaluate( posterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; } }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super ObservationType> output) { Vector p = this.getInitialProbability(); int state = -1; for( int n = 0; n < sampleCount; n++ ) { double value = random.nextDouble(); state = -1; while( value > 0.0 ) { state++; value -= p.getElement(state); } ObservationType sample = CollectionUtil.getElement( this.getEmissionFunctions(), state ).sample(random); output.add( sample ); p = this.getTransitionProbability().getColumn(state); } }
/** * Computes the conditional probability between two objects. This is used when the data type contains the historical data. * @param prior The object that's conditioning the posterior. If conditional probability is thought of as "A given B", this is the B. * @param posterior The object whose probability is based off the prior. This is the "A" in the conditional probability definition. * @param priorDistribution A distribution for the objects. * @param posteriorDistribution A distribution for all pairs of objects. * @return The double representing the conditional probability. */ public double computeConditionalProbabilityWhenDataTypeHasHistoricalData(DataType prior, DataType posterior, ComputableDistribution<DataType> priorDistribution, ComputableDistribution<DataType> posteriorDistribution) { double numerator = posteriorDistribution.getProbabilityFunction().evaluate( posterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; } }
/** * Computes the conditional probability between a collection of objects and a new object. * @param prior The collection of objects that's conditioned on. If conditional probability is thought of as "A given B", this is the B. * @param posterior The new object; finding its probability given the prior. This is the "A" in the conditional probability definition. * @param priorDistribution The distribution which the prior is over. * @param posteriorDistribution The distribution which the posterior is over. If the prior contains n elements, this is a distribution over collections of size n+1 * @return The double representing the conditional probability. */ public double computeConditionalProbability(Collection<DataType> prior, DataType posterior, ComputableDistribution<Collection<DataType>> priorDistribution, ComputableDistribution<Collection<DataType>> posteriorDistribution) { Collection<DataType> realPosterior = new ArrayList<DataType>(prior); realPosterior.add(posterior); double numerator = posteriorDistribution.getProbabilityFunction().evaluate(realPosterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; }
/** * Computes the conditional probability between two objects. This is used when the data type contains the historical data. * @param prior The object that's conditioning the posterior. If conditional probability is thought of as "A given B", this is the B. * @param posterior The object whose probability is based off the prior. This is the "A" in the conditional probability definition. * @param priorDistribution A distribution for the objects. * @param posteriorDistribution A distribution for all pairs of objects. * @return The double representing the conditional probability. */ public double computeConditionalProbabilityWhenDataTypeHasHistoricalData(DataType prior, DataType posterior, ComputableDistribution<DataType> priorDistribution, ComputableDistribution<DataType> posteriorDistribution) { double numerator = posteriorDistribution.getProbabilityFunction().evaluate( posterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; } }
/** * Computes the conditional probability between a collection of objects and a new object. * @param prior The collection of objects that's conditioned on. If conditional probability is thought of as "A given B", this is the B. * @param posterior The new object; finding its probability given the prior. This is the "A" in the conditional probability definition. * @param priorDistribution The distribution which the prior is over. * @param posteriorDistribution The distribution which the posterior is over. If the prior contains n elements, this is a distribution over collections of size n+1 * @return The double representing the conditional probability. */ public double computeConditionalProbability(Collection<DataType> prior, DataType posterior, ComputableDistribution<Collection<DataType>> priorDistribution, ComputableDistribution<Collection<DataType>> posteriorDistribution) { Collection<DataType> realPosterior = new ArrayList<DataType>(prior); realPosterior.add(posterior); double numerator = posteriorDistribution.getProbabilityFunction().evaluate(realPosterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; }
public ProbabilityFunction<ObservationType> call() { final int N = this.gammas.size(); for( int n = 0; n < N; n++ ) { this.weightedValues.get(n).setWeight( this.gammas.get(n).getElement( this.index ) ); } return this.distributionLearner.learn(this.weightedValues).getProbabilityFunction(); }
/** * Computes the conditional probability between two objects. * @param prior The object that's conditioning the posterior. If conditional probability is thought of as "A given B", this is the B. * @param posterior The object whose probability is based off the prior. This is the "A" in the conditional probability definition. * @param priorDistribution A distribution for the objects. * @param posteriorDistribution A distribution for all pairs of objects. * @return The double representing the conditional probability. */ public double computeConditionalProbability(DataType prior, DataType posterior, ComputableDistribution<DataType> priorDistribution, ComputableDistribution<Collection<DataType>> posteriorDistribution) { Collection<DataType> realPosterior = new ArrayList<DataType>(); realPosterior.add(prior); realPosterior.add(posterior); double numerator = posteriorDistribution.getProbabilityFunction().evaluate( realPosterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; }
/** * Computes the conditional probability between two objects. * @param prior The object that's conditioning the posterior. If conditional probability is thought of as "A given B", this is the B. * @param posterior The object whose probability is based off the prior. This is the "A" in the conditional probability definition. * @param priorDistribution A distribution for the objects. * @param posteriorDistribution A distribution for all pairs of objects. * @return The double representing the conditional probability. */ public double computeConditionalProbability(DataType prior, DataType posterior, ComputableDistribution<DataType> priorDistribution, ComputableDistribution<Collection<DataType>> posteriorDistribution) { Collection<DataType> realPosterior = new ArrayList<DataType>(); realPosterior.add(prior); realPosterior.add(posterior); double numerator = posteriorDistribution.getProbabilityFunction().evaluate( realPosterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; }
/** * Computes the conditional probability between two objects. * @param prior The object that's conditioning the posterior. If conditional probability is thought of as "A given B", this is the B. * @param posterior The object whose probability is based off the prior. This is the "A" in the conditional probability definition. * @param priorDistribution A distribution for the objects. * @param posteriorDistribution A distribution for all pairs of objects. * @return The double representing the conditional probability. */ public double computeConditionalProbability(DataType prior, DataType posterior, ComputableDistribution<DataType> priorDistribution, ComputableDistribution<Collection<DataType>> posteriorDistribution) { Collection<DataType> realPosterior = new ArrayList<DataType>(); realPosterior.add(prior); realPosterior.add(posterior); double numerator = posteriorDistribution.getProbabilityFunction().evaluate( realPosterior); double denominator = priorDistribution.getProbabilityFunction().evaluate(prior); if (denominator == 0) { throw new IllegalArgumentException("Prior does not appear in prior distribution."); } return numerator/denominator; }
/** * Computes the log likelihood of the i.i.d. data using the given * probability function. * @param <ObservationType> * Type of observations to consider * @param distribution * Computable distribution from which to get the probability function * (a pdf or pmf) used to compute the likelihood. * @param observations * Observations to compute the log likelihood of. * @return * Log likelihood of the i.i.d. data using the given probability function. */ public static <ObservationType> double logLikelihood( final ComputableDistribution<? super ObservationType> distribution, final Iterable<? extends ObservationType> observations ) { ProbabilityFunction<? super ObservationType> f = distribution.getProbabilityFunction(); double logSum = 0.0; for( ObservationType observation : observations ) { logSum += f.logEvaluate(observation); } return logSum; }
/** * Computes the log likelihood of the i.i.d. data using the given * probability function. * @param <ObservationType> * Type of observations to consider * @param distribution * Computable distribution from which to get the probability function * (a pdf or pmf) used to compute the likelihood. * @param observations * Observations to compute the log likelihood of. * @return * Log likelihood of the i.i.d. data using the given probability function. */ public static <ObservationType> double logLikelihood( final ComputableDistribution<? super ObservationType> distribution, final Iterable<? extends ObservationType> observations ) { ProbabilityFunction<? super ObservationType> f = distribution.getProbabilityFunction(); double logSum = 0.0; for( ObservationType observation : observations ) { logSum += f.logEvaluate(observation); } return logSum; }
/** * Creates a Hidden Markov Model with the same PMF/PDF for each state, * but sampling the columns of the transition matrix and the initial * probability distributions from a diffuse Dirichlet. * @param <ObservationType> * Type of observations to generate. * @param numStates * Number of states to create * @param distribution * Distribution from which we will assign to each state. * @param random * Random number generator * @return * HMM with the specified states. */ public static <ObservationType> HiddenMarkovModel<ObservationType> createRandom( int numStates, ComputableDistribution<ObservationType> distribution, Random random ) { Collection<ProbabilityFunction<ObservationType>> distributions = Collections.nCopies(numStates, distribution.getProbabilityFunction()); return createRandom(distributions, random); }
/** * Creates a Hidden Markov Model with the same PMF/PDF for each state, * but sampling the columns of the transition matrix and the initial * probability distributions from a diffuse Dirichlet. * @param <ObservationType> * Type of observations to generate. * @param numStates * Number of states to create * @param distribution * Distribution from which we will assign to each state. * @param random * Random number generator * @return * HMM with the specified states. */ public static <ObservationType> HiddenMarkovModel<ObservationType> createRandom( int numStates, ComputableDistribution<ObservationType> distribution, Random random ) { Collection<ProbabilityFunction<ObservationType>> distributions = Collections.nCopies(numStates, distribution.getProbabilityFunction()); return createRandom(distributions, random); }
/** * Creates a Hidden Markov Model with the same PMF/PDF for each state, * but sampling the columns of the transition matrix and the initial * probability distributions from a diffuse Dirichlet. * @param <ObservationType> * Type of observations to generate. * @param numStates * Number of states to create * @param distribution * Distribution from which we will assign to each state. * @param random * Random number generator * @return * HMM with the specified states. */ public static <ObservationType> HiddenMarkovModel<ObservationType> createRandom( int numStates, ComputableDistribution<ObservationType> distribution, Random random ) { Collection<ProbabilityFunction<ObservationType>> distributions = Collections.nCopies(numStates, distribution.getProbabilityFunction()); return createRandom(distributions, random); }
public ProbabilityFunction<ObservationType> call() { final int N = this.gammas.size(); for( int n = 0; n < N; n++ ) { this.weightedValues.get(n).setWeight( this.gammas.get(n).getElement( this.index ) ); } return this.distributionLearner.learn(this.weightedValues).getProbabilityFunction(); }
/** * Computes the log likelihood of the i.i.d. data using the given * probability function. * @param <ObservationType> * Type of observations to consider * @param distribution * Computable distribution from which to get the probability function * (a pdf or pmf) used to compute the likelihood. * @param observations * Observations to compute the log likelihood of. * @return * Log likelihood of the i.i.d. data using the given probability function. */ public static <ObservationType> double logLikelihood( final ComputableDistribution<? super ObservationType> distribution, final Iterable<? extends ObservationType> observations ) { ProbabilityFunction<? super ObservationType> f = distribution.getProbabilityFunction(); double logSum = 0.0; for( ObservationType observation : observations ) { logSum += f.logEvaluate(observation); } return logSum; }