org.dmg.pmml.GaussianDistribution Java Examples

The following examples show how to use org.dmg.pmml.GaussianDistribution. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DistributionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public double probability(GaussianDistribution gaussianDistribution, Number x){
	Number mean = gaussianDistribution.getMean();
	if(mean == null){
		throw new MissingAttributeException(gaussianDistribution, PMMLAttributes.GAUSSIANDISTRIBUTION_MEAN);
	}

	Number variance = gaussianDistribution.getVariance();
	if(variance == null){
		throw new MissingAttributeException(gaussianDistribution, PMMLAttributes.GAUSSIANDISTRIBUTION_VARIANCE);
	} // End if

	if(variance.doubleValue() <= 0d){
		throw new InvalidAttributeException(gaussianDistribution, PMMLAttributes.GAUSSIANDISTRIBUTION_VARIANCE, variance);
	}

	NormalDistribution distribution = new NormalDistribution(mean.doubleValue(), Math.sqrt(variance.doubleValue()));

	return distribution.density(x.doubleValue());
}
 
Example #2
Source File: NaiveBayesModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private void calculateContinuousProbabilities(ProbabilityMap<Object, ?> probabilities, TargetValueStats targetValueStats, Number threshold, FieldValue value){
	Number x = value.asNumber();

	for(TargetValueStat targetValueStat : targetValueStats){
		Object targetCategory = targetValueStat.getValue();
		if(targetCategory == null){
			throw new MissingAttributeException(targetValueStat, PMMLAttributes.TARGETVALUESTAT_VALUE);
		}

		ContinuousDistribution distribution = targetValueStat.getContinuousDistribution();
		if(distribution == null){
			throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(targetValueStat.getClass()) + "/<ContinuousDistribution>"), targetValueStat);
		} // End if

		Number probability;

		// "For Naive Bayes models, continuous distribution types are restricted to Gaussian and Poisson distributions"
		if((distribution instanceof GaussianDistribution) || (distribution instanceof PoissonDistribution)){
			probability = DistributionUtil.probability(distribution, x);
		} else

		{
			throw new MisplacedElementException(distribution);
		}

		// The calculated probability cannot fall below the default probability
		if(NumberUtil.compare(probability, threshold) < 0){
			probability = threshold;
		}

		probabilities.multiply(targetCategory, probability);
	}
}
 
Example #3
Source File: DistributionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Calculates the value of the specified probability function at the specified point.
 * </p>
 */
static
public double probability(ContinuousDistribution distribution, Number x){

	if(distribution instanceof GaussianDistribution){
		return probability((GaussianDistribution)distribution, x);
	} else

	if(distribution instanceof PoissonDistribution){
		return probability((PoissonDistribution)distribution, x);
	}

	throw new UnsupportedElementException(distribution);
}
 
Example #4
Source File: DistributionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void estimateDensity(){
	ContinuousDistribution distribution = new GaussianDistribution(5d, Math.pow(1.5d, 2d));

	assertEquals(0.00759732d, DistributionUtil.probability(distribution, 1d), 1e-8);
	assertEquals(0.10934005d, DistributionUtil.probability(distribution, 3d), 1e-8);
	assertEquals(0.26596152d, DistributionUtil.probability(distribution, 5d), 1e-8);
}
 
Example #5
Source File: GaussianNB.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private TargetValueStats encodeTargetValueStats(List<?> values, List<? extends Number> means, List<? extends Number> variances){
	TargetValueStats targetValueStats = new TargetValueStats();

	ClassDictUtil.checkSize(values, means, variances);

	for(int i = 0; i < values.size(); i++){
		GaussianDistribution gaussianDistribution = new GaussianDistribution(means.get(i), variances.get(i));

		TargetValueStat targetValueStat = new TargetValueStat(values.get(i), gaussianDistribution);

		targetValueStats.addTargetValueStats(targetValueStat);
	}

	return targetValueStats;
}