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

lphy.base.distribution.GeneralLinearModel 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.base.function.GeneralLinearFunction;
import lphy.core.model.RandomVariable;
import lphy.core.model.Value;
import lphy.core.model.ValueUtils;
import lphy.core.model.annotation.GeneratorCategory;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.random.RandomGenerator;

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

import static lphy.base.distribution.DistributionConstants.sdParamName;

/**
 * General Linear Model.
 * @author Alexei Drummond
 */
public class GeneralLinearModel extends ParametricDistribution {

    public static final String stdevParamName = "stdev";

    private Value beta;

    private Value x;

    private Value sd;

    public GeneralLinearModel(@ParameterInfo(name = GeneralLinearFunction.betaParamName, narrativeName = "beta", description = "the coefficients of the general linear model.") Value beta,
                              @ParameterInfo(name = GeneralLinearFunction.xParamName, narrativeName = "x", description = "the explanatory variables of the general linear model.") Value x,
                              @ParameterInfo(name = sdParamName, narrativeName = "stdev", description = "the standard deviation of the general linear model.") Value sd) {
        super();
        this.beta = beta;
        this.x = x;
        this.sd = sd;
    }

    @Override
    protected void constructDistribution(RandomGenerator random) {
    }

    @GeneratorInfo(name = "GLM", verbClause = "have", narrativeName = "General linear model",
            category = GeneratorCategory.ALL,
            description = "The general linear model.")
    public RandomVariable sample() {

        double mean = 0.0;

        double[] b = ValueUtils.doubleArrayValue(beta);
        double[] xv = ValueUtils.doubleArrayValue(x);

        for (int i = 0; i < b.length; i++) {
            mean += b[i] * xv[i];
        }

        NormalDistribution normalDistribution = new NormalDistribution(mean, ValueUtils.doubleValue(sd));

        return new RandomVariable<>("y", normalDistribution.sample(), this);
    }

    public double density(Double[] d) {
        // TODO
        throw new UnsupportedOperationException("TODO");
    }

    @Override
    public Map getParams() {
        return new TreeMap<>() {{
            put(GeneralLinearFunction.betaParamName, beta);
            put(GeneralLinearFunction.xParamName, x);
            put(sdParamName, sd);
        }};
    }

    public void setParam(String paramName, Value value) {
        if (paramName.equals(GeneralLinearFunction.betaParamName)) beta = value;
        else if (paramName.equals(GeneralLinearFunction.xParamName)) x = value;
        else if (paramName.equals(sdParamName)) sd = value;
        else super.setParam(paramName, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy