Java Code Examples for org.apache.commons.math3.util.MathArrays#normalizeArray()

The following examples show how to use org.apache.commons.math3.util.MathArrays#normalizeArray() . 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: JGenProg2017_0070_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 2
Source File: Cardumen_00179_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 3
Source File: JGenProg2017_0035_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 4
Source File: Arja_00108_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 5
Source File: JGenProg2017_0035_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 6
Source File: Arja_0085_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 7
Source File: Cardumen_00272_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 8
Source File: Arja_00144_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 9
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 10
Source File: Arja_00170_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 11
Source File: Cardumen_00229_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 12
Source File: Arja_0045_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 13
Source File: Arja_0045_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 14
Source File: 1_DiscreteDistribution.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 15
Source File: 1_DiscreteDistribution.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a discrete distribution using the given random number generator
 * and probability mass function definition.
 *
 * @param rng random number generator.
 * @param samples definition of probability mass function in the format of
 * list of pairs.
 * @throws NotPositiveException if probability of at least one value is
 * negative.
 * @throws MathArithmeticException if the probabilities sum to zero.
 * @throws MathIllegalArgumentException if probability of at least one value
 * is infinite.
 */
public DiscreteDistribution(final RandomGenerator rng, final List<Pair<T, Double>> samples)
    throws NotPositiveException, MathArithmeticException, MathIllegalArgumentException {
    random = rng;

    singletons = new ArrayList<T>(samples.size());
    final double[] probs = new double[samples.size()];

    for (int i = 0; i < samples.size(); i++) {
        final Pair<T, Double> sample = samples.get(i);
        singletons.add(sample.getKey());
        if (sample.getValue() < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        probs[i] = sample.getValue();
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);
}
 
Example 16
Source File: FuzzyKMeansClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the membership matrix with random values.
 */
private void initializeMembershipMatrix() {
    for (int i = 0; i < points.size(); i++) {
        for (int j = 0; j < k; j++) {
            membershipMatrix[i][j] = random.nextDouble();
        }
        membershipMatrix[i] = MathArrays.normalizeArray(membershipMatrix[i], 1.0);
    }
}
 
Example 17
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);

    cumulativeProbabilities = new double[probabilities.length];
    double sum = 0;
    for (int i = 0; i < probabilities.length; i++) {
        sum += probabilities[i];
        cumulativeProbabilities[i] = sum;
    }
}
 
Example 18
Source File: EnumeratedDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an enumerated distribution using the given random number generator
 * and probability mass function enumeration.
 *
 * @param rng random number generator.
 * @param pmf probability mass function enumerated as a list of <T, probability>
 * pairs.
 * @throws NotPositiveException if any of the probabilities are negative.
 * @throws NotFiniteNumberException if any of the probabilities are infinite.
 * @throws NotANumberException if any of the probabilities are NaN.
 * @throws MathArithmeticException all of the probabilities are 0.
 */
public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
    throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException {
    random = rng;

    singletons = new ArrayList<T>(pmf.size());
    final double[] probs = new double[pmf.size()];

    for (int i = 0; i < pmf.size(); i++) {
        final Pair<T, Double> sample = pmf.get(i);
        singletons.add(sample.getKey());
        final double p = sample.getValue();
        if (p < 0) {
            throw new NotPositiveException(sample.getValue());
        }
        if (Double.isInfinite(p)) {
            throw new NotFiniteNumberException(p);
        }
        if (Double.isNaN(p)) {
            throw new NotANumberException();
        }
        probs[i] = p;
    }

    probabilities = MathArrays.normalizeArray(probs, 1.0);

    cumulativeProbabilities = new double[probabilities.length];
    double sum = 0;
    for (int i = 0; i < probabilities.length; i++) {
        sum += probabilities[i];
        cumulativeProbabilities[i] = sum;
    }
}
 
Example 19
Source File: FuzzyKMeansClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the membership matrix with random values.
 */
private void initializeMembershipMatrix() {
    for (int i = 0; i < points.size(); i++) {
        for (int j = 0; j < k; j++) {
            membershipMatrix[i][j] = random.nextDouble();
        }
        membershipMatrix[i] = MathArrays.normalizeArray(membershipMatrix[i], 1.0);
    }
}
 
Example 20
Source File: NormalizeSumEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Object doWork(Object... values) throws IOException{

  Object value = values[0];

  double sumTo = 1.0;

  if(values.length == 2) {
    Number n = (Number)values[1];
    sumTo = n.doubleValue();
  }

  if(null == value){
    return null;
  } else if(value instanceof Matrix) {
    Matrix matrix = (Matrix) value;

    double[][] data = matrix.getData();
    double[][] unitData = new double[data.length][];
    for(int i=0; i<data.length; i++) {
      double[] row = data[i];
      double[] unitRow = MathArrays.normalizeArray(row, sumTo);
      unitData[i] = unitRow;
    }

    Matrix m = new Matrix(unitData);
    m.setRowLabels(matrix.getRowLabels());
    m.setColumnLabels(matrix.getColumnLabels());
    return m;
  } else if(value instanceof List) {
    @SuppressWarnings({"unchecked"})
    List<Number> vals = (List<Number>)value;
    double[] doubles = new double[vals.size()];
    for(int i=0; i<doubles.length; i++) {
      doubles[i] = vals.get(i).doubleValue();
    }

    List<Number> unitList = new ArrayList<>(doubles.length);
    double[] unitArray = MathArrays.normalizeArray(doubles, sumTo);
    for(double d : unitArray) {
      unitList.add(d);
    }

    return unitList;
  } else {
    throw new IOException("The unit function expects either a numeric array or matrix as a parameter");
  }
}