Normalize an array of ln values to probabilities. - Java java.lang

Java examples for java.lang:Math Array Function

Description

Normalize an array of ln values to probabilities.

Demo Code

/*/*from w w w. j a  v a  2s.  c o  m*/
 * Copyright (c) 2014. Real Time Genomics Limited.
 *
 * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement
 * for Academic Non-commercial Research Purposes only.
 *
 * If you did not receive a license accompanying this file, a copy must first be obtained by email
 * from support@realtimegenomics.com.  On downloading, using and/or continuing to use this source
 * code you accept the terms of that license agreement and any amendments to those terms that may
 * be made from time to time by Real Time Genomics Limited.
 */
//package com.java2s;

public class Main {
    /**
     * Normalize an array of ln values to probabilities.
     * @param logs array of log values to be normalized.
     * @return normalized probabilities.
     */
    public static double[] lnToNormaliedProb(final double[] logs) {
        final double[] prob = new double[logs.length];
        double max = logs[0];
        for (int i = 1; i < logs.length; i++) {
            if (max < logs[i]) {
                max = logs[i];
            }
        }
        double sum = 0.0;
        for (int i = 0; i < logs.length; i++) {
            final double v = Math.exp(logs[i] - max);
            prob[i] = v;
            sum += v;
        }
        for (int i = 0; i < logs.length; i++) {
            prob[i] = prob[i] / sum;
        }
        return prob;
    }
}

Related Tutorials