Example usage for org.apache.commons.math3.distribution RealDistribution sample

List of usage examples for org.apache.commons.math3.distribution RealDistribution sample

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution RealDistribution sample.

Prototype

double[] sample(int sampleSize);

Source Link

Document

Generate a random sample from the distribution.

Usage

From source file:com.anhth12.util.WeightInitUtil.java

public static INDArray initWeight(int[] shape, WeightInit initScheme, ActivationFunction act,
        RealDistribution dist) {
    INDArray ret;// w  w w .  ja va  2s  . c o  m
    switch (initScheme) {
    case VI:
        //Rand .* 2 .*r - r
        ret = Nd4j.rand(shape);
        int len = 0;
        for (int i = 0; i < shape.length; i++) {
            len += shape[i];
        }
        double r = Math.sqrt(6) / Math.sqrt(len + 1);
        ret.muli(2).muli(r).subi(r);
        return ret;
    case ZERO:
        return Nd4j.create(shape);
    case SIZE:
        return uniformBasedOnInAndOut(shape, shape[0], shape[1]);
    case DISTRIBUTION:
        ret = Nd4j.rand(shape);
        for (int i = 0; i < ret.slices(); i++) {
            ret.putSlice(i, Nd4j.create(dist.sample(ret.columns())));
        }
    case NORMALIZED:
        ret = Nd4j.rand(shape);
        return ret.subi(0.5).divi(shape[0]);
    case UNIFORM:
        double a = 1 / shape[0];
        return Nd4j.rand(shape, -a, a, new MersenneTwister(123));
    default:
        throw new AssertionError(initScheme.name());
    }
}

From source file:cz.cuni.mff.d3s.spl.interpretation.DistributionLearningInterpretation.java

private RealDistribution substractDistributions(RealDistribution left, RealDistribution right) {
    double[] leftSamples = left.sample(diffDistributionSampleCount);
    double[] rightSamples = left.sample(diffDistributionSampleCount);

    for (int i = 0; i < leftSamples.length; i++) {
        leftSamples[i] -= rightSamples[i];
    }/*  w ww  . j ava 2  s  . c  om*/

    return DistributionUtils.makeEmpirical(leftSamples);
}

From source file:org.apache.solr.client.solrj.io.eval.SampleEvaluator.java

@Override
public Object doWork(Object... objects) throws IOException {
    if (objects.length < 1) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }//from  w ww. j  av  a2 s  . c  om

    Object first = objects[0];

    if (!(first instanceof RealDistribution) && !(first instanceof IntegerDistribution)
            && !(first instanceof MarkovChainEvaluator.MarkovChain)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a Markov Chain, Real or Integer Distribution",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    Object second = null;
    if (objects.length > 1) {
        second = objects[1];
    }

    if (first instanceof MarkovChainEvaluator.MarkovChain) {
        MarkovChainEvaluator.MarkovChain markovChain = (MarkovChainEvaluator.MarkovChain) first;
        if (second != null) {
            return Arrays.stream(markovChain.sample(((Number) second).intValue())).mapToObj(item -> item)
                    .collect(Collectors.toList());
        } else {
            return markovChain.sample();
        }
    } else if (first instanceof RealDistribution) {
        RealDistribution realDistribution = (RealDistribution) first;
        if (second != null) {
            return Arrays.stream(realDistribution.sample(((Number) second).intValue())).mapToObj(item -> item)
                    .collect(Collectors.toList());
        } else {
            return realDistribution.sample();
        }
    } else {
        IntegerDistribution integerDistribution = (IntegerDistribution) first;
        if (second != null) {
            return Arrays.stream(integerDistribution.sample(((Number) second).intValue()))
                    .mapToObj(item -> item).collect(Collectors.toList());
        } else {
            return integerDistribution.sample();
        }
    }
}

From source file:org.workflowsim.utils.DistributionGenerator.java

/**
 *
 * @param dist/* ww  w .jav  a  2  s. c  o m*/
 * @param scale
 * @param shape
 */
public DistributionGenerator(DistributionFamily dist, double scale, double shape) {
    this.dist = dist;
    this.scale = scale;
    this.shape = shape;
    this.scale_prior = scale;
    this.shape_prior = shape;
    RealDistribution distribution = getDistribution(scale, shape);
    samples = distribution.sample(SAMPLE_SIZE);
    updateCumulativeSamples();
    cursor = 0;
}

From source file:org.workflowsim.utils.DistributionGenerator.java

/**
 * Vary the distribution parameters but not the prior knowledge
 *
 * @param scale the first param/*  ww w . j  ava2 s .c o m*/
 * @param shape the second param
 */
public void varyDistribution(double scale, double shape) {
    this.scale = scale;
    this.shape = shape;
    RealDistribution distribution = getDistribution(scale, shape);
    samples = distribution.sample(SAMPLE_SIZE);
    updateCumulativeSamples();
    //cursor = 0;
}