|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.knowceans.util.RandomSamplers
public class RandomSamplers
Instance-based samplers with diverse sampling methods, including beta, gamma, multinomial, and Dirichlet distributions as well as Dirichlet processes, using Sethurahman's stick-breaking construction and Chinese restaurant process. The random generator used is provided in the constructor.
| Nested Class Summary | |
|---|---|
class |
RandomSamplers.CrpData
data structure for a Chinese restaurant process CrpData |
| Field Summary | |
|---|---|
double |
lastRand
|
| Constructor Summary | |
|---|---|
RandomSamplers()
init random sampler using Mersenne twister with default seed. |
|
RandomSamplers(java.util.Random rand)
init random sampler with random number generator provided. |
|
| Method Summary | |
|---|---|
int |
binarySearch(double[] a,
double p)
perform a binary search and return the first index i at which a[i] >= p. |
double |
enumClass(double alpha,
int numdata)
enumclass(alpha,numdata) The expected number of tables in a CRP with concentration parameter alpha and numdata items. |
java.util.Random |
getRand()
|
static void |
main(java.lang.String[] args)
|
int |
randBernoulli(double p)
draw a Bernoulli sample. |
double[] |
randBeta(double[] aa,
double[] bb)
randbeta(aa, bb) Generates beta samples, one for each element in aa/bb, and scale 1. |
double |
randBeta(double aa,
double bb)
beta as two-dimensional Dirichlet |
int |
randBinom(double N,
double p)
draw a binomial sample (by counting Bernoulli samples). |
double |
randConParam(double alpha,
int numgroup,
int[] numdata,
int[] numtable,
double alphaa,
double alphab,
int numiter)
randconparam(alpha,numdata,numclass,aa,bb,numiter) Generates a sample from a concentration parameter of a HDP with gamma(aa,bb) prior, and number of classes and data items given in numdata, numclass (has to be row vectors). |
double |
randConParam(double alpha,
int numdata,
int numtopic,
double alphaa,
double alphab,
int numiter)
Sample the Dirichlet process concetration parameter given the topic and data counts and gamma hyperparameters alphaa and alphab. |
RandomSamplers.CrpData |
randCrp(double[] alpha,
int numdata)
[cc numclass] = randcrp(alpha,numdata) Generates a partition of numdata items with concentration parameter alpha, which can be an array, in which case the Chinese restaurant process has "two new tables to chose for each new customer". |
RandomSamplers.CrpData |
randCrp(double alpha,
int numdata)
|
double[] |
randDir(double[] aa)
randdir(aa) generates one Dirichlet sample vector according to the parameters alpha. |
double[][] |
randDir(double[][] aa,
int direction)
Generate as many Dirichlet column samples as there are columns (direction = 1; randdir(A, 1)) or row samples as there are rows (direction = 2, randdir(A, 2)) in aa (aa[][]), taking the respective parameters. |
double[] |
randDir(double[] mean,
double precision)
randdir(aa) generates one Dirichlet sample vector according to the parameters alpha. |
double[][] |
randDir(double[] aa,
int repetitions)
Generate n Dirichlet samples taking parameters aa. |
double[] |
randDir(double a,
int dimension)
symmetric Dirichlet sample. |
double[] |
randDmm(double[] probs,
double[][] mean,
double[] precision)
DMM sampling |
double[] |
randDmm(double[] probs,
double[][] mean,
double[] precision,
int[] component)
DMM sampling |
double[][] |
randDmm(int n,
double[] probs,
double[][] means,
double[] precisions)
DMM sampling |
double[][] |
randDmm(int n,
double[] probs,
double[][] means,
double[] precisions,
int[] components)
DMM sampling |
double |
randGamma(double rr)
self-contained gamma generator. |
double[] |
randGamma(double[] aa)
randgamma(aa) Generates gamma samples, one for each element in aa. |
double |
randGamma(double shape,
double scale)
sample from gamma distribution with defined shape a and scale b: |
double |
randGmm(double[] probs,
double[] mean,
double[] sigma)
GMM sampling |
double |
randGmm(double[] probs,
double[] mean,
double[] sigma,
int[] component)
GMM sampling |
double[] |
randGmm(int n,
double[] probs,
double[] mean,
double[] sigma)
GMM sampling |
double[] |
randGmm(int n,
double[] probs,
double[] mean,
double[] sigma,
int[] components)
GMM sampling |
int |
randMult(double[] pp)
Creates one multinomial sample given the parameter vector pp. |
int[] |
randMult(double[] pp,
int repetitions)
Multiply sample a multinomial distribution and return a vector with all samples. |
int |
randMultDirect(double[] pp)
Creates one multinomial sample given the parameter vector pp. |
int |
randMultDirect(double[] pp,
double randnum)
Like randMultDirect, but the random number is given as argument. |
int[] |
randMultFreqs(double[] pp,
int repetitions)
Multiply sample a multinomial distribution and return a vector with category frequencies. |
int |
randMultSimple(double[] pp)
old version of the randMult method |
double |
randNorm(double mu,
double sigma)
uses same approach as java.util.Random() |
int |
randNumTable(double alpha,
int numdata)
randnumtable(weights,maxtable) For each entry in weights and maxtables, generates the number of tables given concentration parameter (weights) and number of data items (maxtable). |
int[] |
randPerm(int size)
Random permutation of size elements (symbols '0'.. |
int[] |
randPerm(int[] set)
Random permutation of existing set of integers. |
int[][] |
randPerm(int size,
int parts)
Hierarchical random permutation. |
double[] |
randStick(double[] alpha,
int numclass)
randstick(alpha,numclass) Generates stick-breaking weights with concentration parameter for numclass "sticks". |
java.lang.String |
randString(int length,
byte[] alphabet)
create a random string of length alphanumeric characters. |
double |
randUniform(double numvalue)
|
int |
randUniform(int numvalue)
|
void |
setRand(java.util.Random rand)
|
double[] |
stirling(int nn)
[ss lmss] = stirling(nn) Gives unsigned Stirling numbers of the first kind s(nn,*) in ss. |
void |
testMult()
|
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public double lastRand
| Constructor Detail |
|---|
public RandomSamplers()
public RandomSamplers(java.util.Random rand)
rand - | Method Detail |
|---|
public static void main(java.lang.String[] args)
public double randNorm(double mu,
double sigma)
mu - sigma -
public double randGmm(double[] probs,
double[] mean,
double[] sigma)
probs - mixture responsibilitiesmean - mean vectorsigma - stddev vector
public double randGmm(double[] probs,
double[] mean,
double[] sigma,
int[] component)
probs - mixture responsibilitiesmean - mean vectorsigma - stddev vectorcomponent - [out] component[0] is filled with the sampled component
index, but can be null if not needed.
public double[] randGmm(int n,
double[] probs,
double[] mean,
double[] sigma)
n - number of samples to take (this saves the calculation of the
cumulative probabilities for successive trials)probs - mixture responsibilitiesmean - mean vectorsigma - stddev vector
public double[] randGmm(int n,
double[] probs,
double[] mean,
double[] sigma,
int[] components)
n - number of samples to take (this saves the calculation of the
cumulative probabilities for successive trials)probs - mixture responsibilitiesmean - mean vectorsigma - stddev vectorcomponents - [out] n-vector is filled with the sampled component
indices (ignored if null)
public double[] randDmm(double[] probs,
double[][] mean,
double[] precision)
probs - mixture responsibilitiesmean - mean vector of vectorsprecision - precision vector
public double[] randDmm(double[] probs,
double[][] mean,
double[] precision,
int[] component)
probs - mixture responsibilitiesmean - mean vector of vectorsprecision - precision vectorcomponent - [out] sampled component of the mixture (or ignored if
null)
public double[][] randDmm(int n,
double[] probs,
double[][] means,
double[] precisions)
probs - mixture responsibilitiesmeans - mean vector of vectorsprecisions - precision vector
public double[][] randDmm(int n,
double[] probs,
double[][] means,
double[] precisions,
int[] components)
n - number of trialsprobs - mixture responsibilitiesmeans - mean vector of vectorsprecisions - precision vectorcomponents - n-vector is filled with the sampled component indices
(ignored if null)
public double randBeta(double aa,
double bb)
aa - bb -
public double[] randBeta(double[] aa,
double[] bb)
aa - public double randGamma(double rr)
rr - shape parameter
public double[] randGamma(double[] aa)
aa -
public double randGamma(double shape,
double scale)
x ~ x^(a-1) * exp(-x/b) / ( gamma(a) * b^a )
E(x) = ab, V(x) = (ab)^2. Note that instead of the scale parameter b, often a rate parameter r = 1/b is used: E(x) = a/r, V(x) = (a/r)^2. For sampling, the following are equivalent: Gamma(a,1)*b <=> Gamma(a,b), with shape parametrisation; Gamma(a,1)/r <=> Gamma(a,r) with rate parametrisation.
shape - scale -
public int[] randPerm(int size)
size -
public int[] randPerm(int[] set)
set -
public int[][] randPerm(int size,
int parts)
size - parts -
public double[] randDir(double a,
int dimension)
aa -
public double[] randDir(double[] aa)
aa - normdim -
public double[] randDir(double[] mean,
double precision)
mean - (mean_i = alpha_i / sum_j alpha_j)precision - (precision = alpha_i / mean_i)
public double[][] randDir(double[][] aa,
int direction)
aa - direction - -- 2 is more efficient (row-major Java matrix structure)
public double[][] randDir(double[] aa,
int repetitions)
aa -
public int[] randMultFreqs(double[] pp,
int repetitions)
pp - repetitions -
public int[] randMult(double[] pp,
int repetitions)
pp - repetitions -
public int randMultSimple(double[] pp)
pp -
public void testMult()
public int randMult(double[] pp)
public int randMultDirect(double[] pp)
public int randMultDirect(double[] pp,
double randnum)
public int binarySearch(double[] a,
double p)
a - p -
public int randBinom(double N,
double p)
N - p - public int randBernoulli(double p)
p - success probability
public double randConParam(double alpha,
int numgroup,
int[] numdata,
int[] numtable,
double alphaa,
double alphab,
int numiter)
Modification of Escobar and West. Works for multiple groups of data. numdata, numclass are row vectors, one element per group. After Teh (npbayes).
alpha - alphanumgroup - number of components ??numdata - number of data items per classnumtable - number of per DPalphaa - hyperparameter (gamma shape)alphab - hyperparameter (gamma scale)numiter - number of iterations
public double randConParam(double alpha,
int numdata,
int numtopic,
double alphaa,
double alphab,
int numiter)
alpha - numdata - numtopic - alphaa - alphab - numiter -
public RandomSamplers.CrpData randCrp(double alpha,
int numdata)
public RandomSamplers.CrpData randCrp(double[] alpha,
int numdata)
alpha - numdata -
public int randNumTable(double alpha,
int numdata)
weights - maxtable -
public double[] randStick(double[] alpha,
int numclass)
alpha - numclass -
public double enumClass(double alpha,
int numdata)
alpha - numdata -
public java.lang.String randString(int length,
byte[] alphabet)
length - of outputalphabet - alphabet to be used or null
public double[] stirling(int nn)
nn -
public double randUniform(double numvalue)
numclass -
public int randUniform(int numvalue)
numclass -
public final java.util.Random getRand()
public final void setRand(java.util.Random rand)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||