Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.math.BigDecimal;

public class Main {
    private static final int ENTROPY_SCALE = 3;

    /**
     * H = -[0.5 * lg(0.5) + 0.4*lg(0.4) + 0.1*lg(0.1)]
     */
    public static BigDecimal calculateEntropy(double... probabilities) {
        BigDecimal res = BigDecimal.ZERO;

        for (double singleProbability : probabilities) {

            BigDecimal singleEntropy = BigDecimal.valueOf(singleProbability)
                    .multiply(BigDecimal.valueOf(lg2(singleProbability)));

            res = res.add(singleEntropy);
        }

        return res.negate().setScale(ENTROPY_SCALE, BigDecimal.ROUND_HALF_UP);
    }

    private static double lg2(double value) {
        return Math.log10(value) / Math.log10(2.0);
    }
}