<commit_before>package org.continuity.api.entities.artifact.markovbehavior;

import java.util.Arrays;
import java.util.stream.DoubleStream;

import javax.ws.rs.NotSupportedException;

/**
 * Represents a normal distribution of {@code double}.
 *
 * @author <NAME>
 *
 */
public class NormalDistribution {

	public static final NormalDistribution ZERO = new ZeroDistribution();

	private double mean;

	private double variance;

	/**
	 * Creates a new distribution.
	 *
	 * @param mean
	 *            The mean.
	 * @param variance
	 *            The variance.
	 */
	public NormalDistribution(double mean, double variance) {
		this.mean = mean;
		this.variance = variance;
	}

	/**
	 * Creates a new zero distribution, i.e., mean and variance are 0.
	 */
	public NormalDistribution() {
		this(0.0, 0.0);
	}

	/**
	 * Calculates the normal distribution from the given sample.
	 *
	 * @param sample
	 *            The sample.
	 * @return The resulting normal distribution.
	 */
	public static NormalDistribution fromSample(double... sample) {
		double mean = DoubleStream.of(sample).sum() / sample.length;
		double variance = DoubleStream.of(sample).map(x -> Math.pow(x - mean, 2)).sum() / (sample.length - 1);

		return new NormalDistribution(mean, variance);
	}

	/**
	 * Adds several normal distributions, i.e., calculates the convolution. The result will be
	 * another normal distribution.
	 *
	 * @param first
	 *            The first distribution to be added.
	 * @param second
	 *            The second distribution to be added.
	 * @param distributions
	 *            Further distributions to be added.
	 * @return The resulting normal distribution.
	 */
	public static NormalDistribution add(NormalDistribution first, NormalDistribution second, NormalDistribution... distributions) {
		double mean = first.getMean() + second.getMean() + Arrays.stream(distributions).mapToDouble(NormalDistribution::getMean).sum();
		double variance = first.getVariance() + second.getVariance() + Arrays.stream(distributions).mapToDouble(NormalDistribution::getVariance).sum();<commit_msg>CONT-181: Workload model modularization produces NaN.<commit_after>package org.continuity.api.entities.artifact.markovbehavior;

import java.util.Arrays;
import java.util.stream.DoubleStream;

import javax.ws.rs.NotSupportedException;

/**
 * Represents a normal distribution of {@code double}.
 *
 * @author <NAME>
 *
 */
public class NormalDistribution {

	public static final NormalDistribution ZERO = new ZeroDistribution();

	private double mean;

	private double variance;

	/**
	 * Creates a new distribution.
	 *
	 * @param mean
	 *            The mean.
	 * @param variance
	 *            The variance.
	 */
	public NormalDistribution(double mean, double variance) {
		this.mean = mean;
		this.variance = variance;
	}

	/**
	 * Creates a new zero distribution, i.e., mean and variance are 0.
	 */
	public NormalDistribution() {
		this(0.0, 0.0);
	}

	/**
	 * Calculates the normal distribution from the given sample.
	 *
	 * @param sample
	 *            The sample.
	 * @return The resulting normal distribution.
	 */
	public static NormalDistribution fromSample(double... sample) {
		double mean;
		double variance;

		if (sample.length > 0) {
			mean = DoubleStream.of(sample).sum() / sample.length;
		} else {
			mean = 0;
		}

		if (sample.length > 1) {
			variance = DoubleStream.of(sample).map(x -> Math.pow(x - mean, 2)).sum() / (sample.length - 1);
		} else {
			variance = 0;
		}

		return new NormalDistribution(mean, variance);
	}

	/**
	 * Adds several normal distributions, i.e., calculates the convolution. The result will be
	 * another normal distribution.
	 *
	 * @param first
	 *            The first distribution to be added.
	 * @param second
	 *            The second distribution to be added.
	 * @param distributions
	 *            Further distributions to be added.
	 * @return The resulting normal distribution.
	 */
	public static NormalDistribution add(NormalDistribution first, NormalDistribution second, NormalDistribution... distributions) {
		double mean = first.getMean() + second.getMean() + Arrays.stream(distributions).mapToDouble(NormalDistribution::getMean).sum();
		double variance = first.getVariance() + second.getVariance() + Arrays.stream(distributions).mapToDouble(NormalDistribution::getVariance).sum();
{'fraction_non_alphanumeric': 0.11149584487534626, 'fraction_numerical': 0.004847645429362881, 'mean_word_length': 3.359154929577465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836345', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1152, 'n_words': 356}