Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.mahout.math.random;
 
 
Generates samples from a generalized Chinese restaurant process (or Pittman-Yor process). The number of values drawn exactly once will asymptotically be equal to the discount parameter as the total number of draws T increases without bound. The number of unique values sampled will increase as O(alpha * log T) if discount = 0 or O(alpha * T^discount) for discount > 0.
 
 public final class ChineseRestaurant implements Sampler<Integer> {
 
   private final double alpha;
   private double weight = 0;
   private double discount = 0;
   private final DoubleArrayList weights = new DoubleArrayList();
   private final Random rand = RandomUtils.getRandom();

  
Constructs a Dirichlet process sampler. This is done by setting discount = 0.

Parameters:
alpha The strength parameter for the Dirichlet process.
 
   public ChineseRestaurant(double alpha) {
     this(alpha, 0);
   }

  
Constructs a Pitman-Yor sampler.

Parameters:
alpha The strength parameter that drives the number of unique values as a function of draws.
discount The discount parameter that drives the percentage of values that occur once in a large sample.
 
   public ChineseRestaurant(double alphadouble discount) {
     Preconditions.checkArgument(alpha > 0);
     Preconditions.checkArgument(discount >= 0 && discount <= 1);
     this. = alpha;
     this. = discount;
   }
 
   @Override
   public Integer sample() {
     double u = .nextDouble() * ( + );
     for (int j = 0; j < .size(); j++) {
       // select existing options with probability (w_j - d) / (alpha + w)
       if (u < .get(j) - ) {
         .set(j.get(j) + 1);
         ++;
         return j;
       } else {
         u -= .get(j) - ;
       }
     }
 
     // if no existing item selected, pick new item with probability (alpha - d*t) / (alpha + w)
     // where t is number of pre-existing cases
     .add(1);
     ++;
     return .size() - 1;
   }

  

Returns:
the number of unique values that have been returned.
 
   public int size() {
     return .size();
   }

  

Returns:
the number draws so far.
 
   public int count() {
     return (int;
   }

  

Parameters:
j Which value to test.
Returns:
The number of times that j has been returned so far.
  public int count(int j) {
    Preconditions.checkArgument(j >= 0);
    if (j < .size()) {
      return (int.get(j);
    } else {
      return 0;
    }
  }
New to GrepCode? Check out our FAQ X