All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lphy.base.distribution.ExpMulti Maven / Gradle / Ivy

Go to download

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.GenerativeDistribution;
import lphy.core.model.RandomVariable;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import lphy.core.simulator.RandomUtils;
import org.apache.commons.math3.distribution.ExponentialDistribution;
import org.apache.commons.math3.random.RandomGenerator;

import java.util.Map;
import java.util.TreeMap;

@Deprecated()
public class ExpMulti implements GenerativeDistribution {

    private Value mean;
    private Value n;


    private RandomGenerator random;

    public ExpMulti(@ParameterInfo(name = DistributionConstants.meanParamName, description = "the mean of an exponential distribution.") Value mean,
                    @ParameterInfo(name = DistributionConstants.nParamName, description = "the number of iid exponential draws.") Value n) {
        this.mean = mean;
        this.n = n;

        this.random = RandomUtils.getRandom();
    }

    @GeneratorInfo(name = "Exp", description = "The exponential probability distribution.")
    public RandomVariable sample() {

        Double[] x = new Double[n.value()];

        for (int i = 0; i < x.length; i++) {
            x[i] = -Math.log(random.nextDouble()) * getMean();
        }
        return new RandomVariable<>("x", x, this);
    }

    @Override
    public double logDensity(Double[] x) {
        ExponentialDistribution exp = new ExponentialDistribution(random, mean.value());
        double logP = exp.logDensity(x[0]);
        for (int i = 1; i < x.length; i++) {
            logP += exp.logDensity(x[i]);
        }
        return logP;
    }

    @Override
    public Map getParams() {
        return new TreeMap<>() {{
            put(DistributionConstants.meanParamName, mean);
            put(DistributionConstants.nParamName, n);
        }};
    }

    @Override
    public void setParam(String paramName, Value value) {
        if (DistributionConstants.meanParamName.equals(paramName)) {
            mean = value;
        } else if (DistributionConstants.nParamName.equals(paramName)) {
            n = value;
        } else {
            throw new RuntimeException("Unrecognised parameter name: " + paramName);
        }
    }

    public double getMean() {
        if (mean != null) return mean.value();
        return 1.0;
    }

    public void setMean(double mean) {
        this.mean.setValue(mean);
    }

    public String toString() {
        return getName();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy