lphy.base.distribution.Geometric Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lphy-base Show documentation
Show all versions of lphy-base Show documentation
The standard library of LPhy, which contains the required generative distributions and basic functions.
The newest version!
package lphy.base.distribution;
import lphy.core.model.GenerativeDistribution1D;
import lphy.core.model.RandomVariable;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorCategory;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import org.apache.commons.math3.distribution.GeometricDistribution;
import org.apache.commons.math3.random.RandomGenerator;
import java.util.Collections;
import java.util.Map;
/**
* The discrete probability distribution of the number of failures
* before the first success given a fixed probability of success p.
* @see GeometricDistribution
* @author Alexei Drummond
* @author Walter Xie
*/
public class Geometric extends ParametricDistribution implements GenerativeDistribution1D {
private Value p;
GeometricDistribution geom;
public Geometric(@ParameterInfo(name= DistributionConstants.pParamName, description="the probability of success.") Value p) {
super();
this.p = p;
constructDistribution(random);
}
@Override
protected void constructDistribution(RandomGenerator random) {
geom = new GeometricDistribution(random, p.value());
}
@GeneratorInfo(name="Geometric", category = GeneratorCategory.PRIOR,
description="The probability distribution of the number of failures before the first success given a fixed probability of success p, supported on the set { 0, 1, 2, 3, ... }.")
public RandomVariable sample() {
return new RandomVariable<>(null, geom.sample(), this);
}
public double density(Integer i) {
return geom.probability(i);
}
@Override
public Map getParams() {
return Collections.singletonMap(DistributionConstants.pParamName, p);
}
public void setSuccessProbability(double p) {
this.p.setValue(p);
}
private static final Integer[] domainBounds = {0, Integer.MAX_VALUE};
public Integer[] getDomainBounds() {
return domainBounds;
}
}