The Laws of Cryptography:
Inverse of Capacity Formula

by Neal R. Wagner

Copyright © 2001 by Neal R. Wagner. All rights reserved.

NOTE: This site is obsolete. See book draft (in PDF):

Here is a Java program that prints a table of channel capacities and corresponding channel probabilities. (The function calculating the inverse of the channel capacity function is given in boldface.)

// CapInv2.java: print table of inverse capacities
//   p: the channel probability for a binary symmetric channel
import java.text.DecimalFormat;
public class CapInv2 {
   static final int TABLE_SIZE = 20;
   static DecimalFormat eightDigits = new DecimalFormat("0.00000000");
   // main function to do calculation
   public static void main (String[] args) {
      double p; // channel probability
      double c; // channel capacity
      System.out.println("<table border>");
      System.out.println("<tr align=center><td><b>Channel<br>Capacity</b></td>");
      System.out.println("<td><b>Probability<br>p</b></td>");
      System.out.println("<td><b>Probability<br>1 - p</b></td></tr>");
      System.out.println("<tr><td></td><td></td></tr>");
      for (int i = 0; i <= TABLE_SIZE; i++) {
         c = (double)i/TABLE_SIZE;
         System.out.print("<tr><td>" + c);
         if ((int)(10*c) == 10*c) System.out.print("0");
         System.out.print("</td><td>" + 
            eightDigits.format(capacityInverse(c)) + "</td>");
         System.out.println("</td><td>" +
            eightDigits.format(1 - capacityInverse(c)) + "</td></tr>");
      }
      System.out.println("</table>");
   } // end of main

   // capacity: the capacity of the binary symmetric channel
   private static double capacity(double p) {
      if (p == 0 || p == 1) return 1;
      return 1 + p*log2(p) + (1 - p)*log2(1 - p);
   }

   // capacityInverse: the inverse of the capacity function
   // uses simple bisection method
   private static double capacityInverse(double c) {
      if (c < 0 || c > 1) return -1;
      double lo = 0, hi = 0.5, mid, cLo, cHi, cMid;
      do {
         mid = (lo + hi)/2;
         cLo = capacity(lo); cHi = capacity(hi); cMid = capacity(mid);
         if (c > cMid) hi = mid;
         else lo = mid;
      } while (hi - lo > 1.0E-15);
      return mid; 
   }

   // log2:  Logarithm base 2
   public static double log2(double d) {
      return Math.log(d)/Math.log(2.0);
   }
} 

Here is the table printed by the above program:

Channel
Capacity
Probability
p
Probability
1 - p
0.000.500000000.50000000
0.050.369127750.63087225
0.100.316019350.68398065
0.150.276040890.72395911
0.200.243003850.75699615
0.250.214501740.78549826
0.300.189297710.81070229
0.350.166657010.83334299
0.400.146102400.85389760
0.450.127304810.87269519
0.500.110027860.88997214
0.550.094097240.90590276
0.600.079382600.92061740
0.650.065786710.93421329
0.700.053239040.94676096
0.750.041692690.95830731
0.800.031124460.96887554
0.850.021539630.97846037
0.900.012986860.98701314
0.950.005607170.99439283
1.000.000000001.00000000


Revision date: 2002-01-04. (Please use ISO 8601, the International Standard.)