public static <E> Distribution<E> laplaceSmoothedDistribution(Counter<E> counter, int numberOfKeys, double lambda) {
    Distribution<E> norm = new Distribution<E>();
    norm.counter = new ClassicCounter<E>();
    double total = counter.totalCount();
    double newTotal = total + (lambda * numberOfKeys);
    double reservedMass = ((double) numberOfKeys - counter.size()) * lambda / newTotal;
    if (verbose) {
      System.err.println(((double) numberOfKeys - counter.size()) + " * " + lambda + " / (" + total + " + ( " + lambda + " * " + (double) numberOfKeys + ") )");
    }
    norm.numberOfKeys = numberOfKeys;
    norm.reservedMass = reservedMass;
    if (verbose) {
      System.err.println("reserved mass=" + reservedMass);
    }
    for (E key : counter.keySet()) {
      double count = counter.getCount(key);
      norm.counter.setCount(key, (count + lambda) / newTotal);
    }
    if (verbose) {
      System.err.println("unseenKeys=" + (norm.numberOfKeys - norm.counter.size()) + " seenKeys=" + norm.counter.size() + " reservedMass=" + norm.reservedMass);
      System.err.println("0 count prob: " + lambda / newTotal);
      System.err.println("1 count prob: " + (1.0 + lambda) / newTotal);
      System.err.println("2 count prob: " + (2.0 + lambda) / newTotal);
      System.err.println("3 count prob: " + (3.0 + lambda) / newTotal);
    }
    return norm;
  }
public static < E > Distribution < E > laplaceSmoothedDistribution ( Counter < E > counter , int numberOfKeys , double lambda ) { Distribution < E > norm = new Distribution < E > ( ) ; norm . counter = new ClassicCounter < E > ( ) ; double total = counter . totalCount ( ) ; double newTotal = total + ( lambda * numberOfKeys ) ; double reservedMass = ( ( double ) numberOfKeys - counter . size ( ) ) * lambda / newTotal ; if ( verbose ) { System . err . println ( ( ( double ) numberOfKeys - counter . size ( ) ) + " * " + lambda + " / (" + total + " + ( " + lambda + " * " + ( double ) numberOfKeys + ") )" ) ; } norm . numberOfKeys = numberOfKeys ; norm . reservedMass = reservedMass ; if ( verbose ) { System . err . println ( "reserved mass=" + reservedMass ) ; } for ( E key : counter . keySet ( ) ) { double count = counter . getCount ( key ) ; norm . counter . setCount ( key , ( count + lambda ) / newTotal ) ; } if ( verbose ) { System . err . println ( "unseenKeys=" + ( norm . numberOfKeys - norm . counter . size ( ) ) + " seenKeys=" + norm . counter . size ( ) + " reservedMass=" + norm . reservedMass ) ; System . err . println ( "0 count prob: " + lambda / newTotal ) ; System . err . println ( "1 count prob: " + ( 1.0 + lambda ) / newTotal ) ; System . err . println ( "2 count prob: " + ( 2.0 + lambda ) / newTotal ) ; System . err . println ( "3 count prob: " + ( 3.0 + lambda ) / newTotal ) ; } return norm ; }
Creates a smoothed Distribution using Lidstone's law, ie adds lambda (typically
between 0 and 1) to every item, including unseen ones, and divides by the total count.

@return a new Lidstone smoothed Distribution