use of org.apache.commons.math3.distribution.NormalDistribution in project SIMVA-SoS by SESoS.
the class MCIResponseWorld method generateExpectedPatientsMap.
private void generateExpectedPatientsMap() {
NormalDistribution xND = new NormalDistribution(MAP_SIZE.getLeft() / 2, MAP_SIZE.getLeft() / 4);
NormalDistribution yND = new NormalDistribution(MAP_SIZE.getRight() / 2, MAP_SIZE.getRight() / 4);
for (int x = 0; x < MAP_SIZE.getLeft(); x++) for (int y = 0; y < MAP_SIZE.getRight(); y++) {
double xProb = xND.cumulativeProbability(x) - xND.cumulativeProbability(x - 1);
double nX = xProb * MAP_SIZE.getLeft() * MAP_SIZE.getRight();
double yProb = xND.cumulativeProbability(y) - xND.cumulativeProbability(y - 1);
int nXY = (int) Math.round(yProb * nX);
this.expectedPatientsMap.setValue(x, y, nXY);
}
}
use of org.apache.commons.math3.distribution.NormalDistribution in project incubator-systemml by apache.
the class ParameterizedBuiltin method computeFromDistribution.
/**
* Helper function to compute distribution-specific cdf (both lowertail and uppertail) and inverse cdf.
*
* @param dcode probablility distribution code
* @param params map of parameters
* @param inverse true if inverse
* @return cdf or inverse cdf
*/
private static double computeFromDistribution(ProbabilityDistributionCode dcode, HashMap<String, String> params, boolean inverse) {
// given value is "quantile" when inverse=false, and it is "probability" when inverse=true
double val = Double.parseDouble(params.get("target"));
boolean lowertail = true;
if (params.get("lower.tail") != null) {
lowertail = Boolean.parseBoolean(params.get("lower.tail"));
}
AbstractRealDistribution distFunction = null;
switch(dcode) {
case NORMAL:
// default values for mean and sd
double mean = 0.0, sd = 1.0;
String mean_s = params.get("mean"), sd_s = params.get("sd");
if (mean_s != null)
mean = Double.parseDouble(mean_s);
if (sd_s != null)
sd = Double.parseDouble(sd_s);
if (sd <= 0)
throw new DMLRuntimeException("Standard deviation for Normal distribution must be positive (" + sd + ")");
distFunction = new NormalDistribution(mean, sd);
break;
case EXP:
// default value for 1/mean or rate
double exp_rate = 1.0;
if (params.get("rate") != null)
exp_rate = Double.parseDouble(params.get("rate"));
if (exp_rate <= 0) {
throw new DMLRuntimeException("Rate for Exponential distribution must be positive (" + exp_rate + ")");
}
// For exponential distribution: mean = 1/rate
distFunction = new ExponentialDistribution(1.0 / exp_rate);
break;
case CHISQ:
if (params.get("df") == null) {
throw new DMLRuntimeException("" + "Degrees of freedom must be specified for chi-squared distribution " + "(e.g., q=qchisq(0.5, df=20); p=pchisq(target=q, df=1.2))");
}
int df = UtilFunctions.parseToInt(params.get("df"));
if (df <= 0) {
throw new DMLRuntimeException("Degrees of Freedom for chi-squared distribution must be positive (" + df + ")");
}
distFunction = new ChiSquaredDistribution(df);
break;
case F:
if (params.get("df1") == null || params.get("df2") == null) {
throw new DMLRuntimeException("" + "Degrees of freedom must be specified for F distribution " + "(e.g., q = qf(target=0.5, df1=20, df2=30); p=pf(target=q, df1=20, df2=30))");
}
int df1 = UtilFunctions.parseToInt(params.get("df1"));
int df2 = UtilFunctions.parseToInt(params.get("df2"));
if (df1 <= 0 || df2 <= 0) {
throw new DMLRuntimeException("Degrees of Freedom for F distribution must be positive (" + df1 + "," + df2 + ")");
}
distFunction = new FDistribution(df1, df2);
break;
case T:
if (params.get("df") == null) {
throw new DMLRuntimeException("" + "Degrees of freedom is needed to compute probabilities from t distribution " + "(e.g., q = qt(target=0.5, df=10); p = pt(target=q, df=10))");
}
int t_df = UtilFunctions.parseToInt(params.get("df"));
if (t_df <= 0) {
throw new DMLRuntimeException("Degrees of Freedom for t distribution must be positive (" + t_df + ")");
}
distFunction = new TDistribution(t_df);
break;
default:
throw new DMLRuntimeException("Invalid distribution code: " + dcode);
}
double ret = Double.NaN;
if (inverse) {
// inverse cdf
ret = distFunction.inverseCumulativeProbability(val);
} else if (lowertail) {
// cdf (lowertail)
ret = distFunction.cumulativeProbability(val);
} else {
// cdf (upper tail)
// TODO: more accurate distribution-specific computation of upper tail probabilities
ret = 1.0 - distFunction.cumulativeProbability(val);
}
return ret;
}
use of org.apache.commons.math3.distribution.NormalDistribution in project pyramid by cheng-li.
the class RegressionSynthesizer method univarStepFeatureNoise.
public RegDataSet univarStepFeatureNoise() {
NormalDistribution featureNoise = new NormalDistribution(0, 0.1);
RegDataSet dataSet = RegDataSetBuilder.getBuilder().numDataPoints(numDataPoints).numFeatures(1).dense(true).missingValue(false).build();
for (int i = 0; i < numDataPoints; i++) {
double featureValue = Sampling.doubleUniform(0, 1);
double label;
if (featureValue > 0.5) {
label = 0.7;
} else {
label = 0.2;
}
label += noise.sample();
featureValue += featureNoise.sample();
dataSet.setFeatureValue(i, 0, featureValue);
dataSet.setLabel(i, label);
}
return dataSet;
}
use of org.apache.commons.math3.distribution.NormalDistribution in project pyramid by cheng-li.
the class RegressionSynthesizer method gaussianMixture.
public RegDataSet gaussianMixture() {
NormalDistribution leftGaussian = new NormalDistribution(0.2, 0.01);
NormalDistribution rightGaussian = new NormalDistribution(0.7, 0.1);
RegDataSet dataSet = RegDataSetBuilder.getBuilder().numDataPoints(numDataPoints).numFeatures(1).dense(true).missingValue(false).build();
for (int i = 0; i < numDataPoints; i++) {
double featureValue = Sampling.doubleUniform(0, 1);
double label;
if (featureValue > 0.5) {
label = leftGaussian.sample();
} else {
label = rightGaussian.sample();
}
dataSet.setFeatureValue(i, 0, featureValue);
dataSet.setLabel(i, label);
}
return dataSet;
}
use of org.apache.commons.math3.distribution.NormalDistribution in project druid by druid-io.
the class BenchmarkColumnValueGenerator method initDistribution.
private void initDistribution() {
BenchmarkColumnSchema.ValueDistribution distributionType = schema.getDistributionType();
ValueType type = schema.getType();
List<Object> enumeratedValues = schema.getEnumeratedValues();
List<Double> enumeratedProbabilities = schema.getEnumeratedProbabilities();
List<Pair<Object, Double>> probabilities = new ArrayList<>();
switch(distributionType) {
case SEQUENTIAL:
// not random, just cycle through numbers from start to end, or cycle through enumerated values if provided
distribution = new SequentialDistribution(schema.getStartInt(), schema.getEndInt(), schema.getEnumeratedValues());
break;
case UNIFORM:
distribution = new UniformRealDistribution(schema.getStartDouble(), schema.getEndDouble());
break;
case DISCRETE_UNIFORM:
if (enumeratedValues == null) {
enumeratedValues = new ArrayList<>();
for (int i = schema.getStartInt(); i < schema.getEndInt(); i++) {
Object val = convertType(i, type);
enumeratedValues.add(val);
}
}
// give them all equal probability, the library will normalize probabilities to sum to 1.0
for (int i = 0; i < enumeratedValues.size(); i++) {
probabilities.add(new Pair<>(enumeratedValues.get(i), 0.1));
}
distribution = new EnumeratedTreeDistribution<>(probabilities);
break;
case NORMAL:
distribution = new NormalDistribution(schema.getMean(), schema.getStandardDeviation());
break;
case ROUNDED_NORMAL:
NormalDistribution normalDist = new NormalDistribution(schema.getMean(), schema.getStandardDeviation());
distribution = new RealRoundingDistribution(normalDist);
break;
case ZIPF:
int cardinality;
if (enumeratedValues == null) {
Integer startInt = schema.getStartInt();
cardinality = schema.getEndInt() - startInt;
ZipfDistribution zipf = new ZipfDistribution(cardinality, schema.getZipfExponent());
for (int i = 0; i < cardinality; i++) {
probabilities.add(new Pair<>((Object) (i + startInt), zipf.probability(i)));
}
} else {
cardinality = enumeratedValues.size();
ZipfDistribution zipf = new ZipfDistribution(enumeratedValues.size(), schema.getZipfExponent());
for (int i = 0; i < cardinality; i++) {
probabilities.add(new Pair<>(enumeratedValues.get(i), zipf.probability(i)));
}
}
distribution = new EnumeratedTreeDistribution<>(probabilities);
break;
case ENUMERATED:
for (int i = 0; i < enumeratedValues.size(); i++) {
probabilities.add(new Pair<>(enumeratedValues.get(i), enumeratedProbabilities.get(i)));
}
distribution = new EnumeratedTreeDistribution<>(probabilities);
break;
default:
throw new UnsupportedOperationException("Unknown distribution type: " + distributionType);
}
if (distribution instanceof AbstractIntegerDistribution) {
((AbstractIntegerDistribution) distribution).reseedRandomGenerator(seed);
} else if (distribution instanceof AbstractRealDistribution) {
((AbstractRealDistribution) distribution).reseedRandomGenerator(seed);
} else if (distribution instanceof EnumeratedDistribution) {
((EnumeratedDistribution) distribution).reseedRandomGenerator(seed);
}
}
Aggregations