熵
package demo;
import java.util.*;
public class Welcome {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter the probability: ");
String[] array=input.nextLine().split(" ");
double[] probability=new double[array.length];
if(array[0].contains("/")) {
for(int i=0;i<array.length;i++)
probability[i]=convertFractionToDecimal(array[i]);
}
else {
for(int i=0;i<array.length;i++)
probability[i]=Double.parseDouble(array[i]);
}
double entropy=0;
for(int i=0;i<probability.length;i++)
entropy+=probability[i]*Math.log(probability[i])/Math.log(2);
System.out.printf("The entropy is %f bit/symbol\n",-entropy);
}
public static double convertFractionToDecimal(String fraction) {
String[] array=fraction.split("/");
return Double.parseDouble(array[0])/Double.parseDouble(array[1]);
}
}
运行示例1:
Enter the probability: 1/10 2/10 4/10 1/10 2/10
The entropy is 2.121928 bit/symbol
运行示例2:
Enter the probability: 0.1 0.2 0.4 0.1 0.2
The entropy is 2.121928 bit/symbol
自信息量
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter the probability to calculate the amount of self information: ");
double probability=input.nextDouble();
double selfInformation=-Math.log(probability)/Math.log(2);
System.out.printf("The self information is %f bit\nlog2(%f) = %f",selfInformation,probability,-selfInformation);
}
}
运行示例:
Enter the probability to calculate the amount of self information: 0.5
The self information is 1.000000 bit
log2(0.500000) = -1.000000