本文整理汇总了Java中cc.mallet.util.Randoms类的典型用法代码示例。如果您正苦于以下问题:Java Randoms类的具体用法?Java Randoms怎么用?Java Randoms使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Randoms类属于cc.mallet.util包,在下文中一共展示了Randoms类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sample
import cc.mallet.util.Randoms; //导入依赖的package包/类
public Assignment sample (Randoms r)
{
// generate from standard normal
double[] vals = new double [mean.size ()];
for (int k = 0; k < vals.length; k++) {
vals[k] = r.nextGaussian ();
}
// and transform
Vector Z = new DenseVector (vals, false);
DenseVector result = new DenseVector (vals.length);
variance.mult (Z, result);
result = (DenseVector) result.add (mean);
return new Assignment (vars.toVariableArray (), result.getData ());
}
示例2: sampleLocation
import cc.mallet.util.Randoms; //导入依赖的package包/类
public int sampleLocation (Randoms r)
{
double sum = sum();
double sampled = r.nextUniform () * sum;
double cum = 0;
for (int idx = 0; idx < probs.numLocations (); idx++) {
double val = value (idx);
cum += val;
if (sampled <= cum + EPS) {
return idx;
}
}
throw new RuntimeException
("Internal errors: Couldn't sample from potential "+this+"\n"+dumpToString ()+"\n Using value "+sampled);
}
示例3: nextWishart
import cc.mallet.util.Randoms; //导入依赖的package包/类
/**
* A Wishart random variate, based on R code by Bill Venables.
*
* @param sqrtScaleMatrix The lower-triangular matrix square root of the scale matrix.
* To draw from the posterior of a precision (ie inverse covariance) matrix,
* this should be chol( S^{-1} ), where S is the scatter matrix X'X of
* columns of MV normal observations X.
* @param dimension The size of the matrix
* @param degreesOfFreedom The degree of freedom for the Wishart. Should be greater than dimension. For
* a posterior distribution, this is the number of observations + the df of the prior.
*/
public static double[] nextWishart(double[] sqrtScaleMatrix, int dimension,
int degreesOfFreedom, Randoms random) {
double[] sample = new double[sqrtScaleMatrix.length];
for (int row = 0; row < dimension; row++) {
for (int col = 0; col < row; col++) {
sample[ (row * dimension) + col ] = random.nextGaussian(0, 1);
}
sample[ (row * dimension) + row ] = Math.sqrt(random.nextChiSq(degreesOfFreedom));
}
//System.out.println(doubleArrayToString(sample, dimension));
//System.out.println(doubleArrayToString(sqrtScaleMatrix, dimension));
//System.out.println(doubleArrayToString(lowerTriangularProduct(sample, sqrtScaleMatrix, dimension), dimension));
System.out.println(diagonalToString(sample, dimension));
System.out.println(diagonalToString(sqrtScaleMatrix, dimension));
System.out.println(diagonalToString(lowerTriangularProduct(sample, sqrtScaleMatrix, dimension), dimension));
return lowerTriangularCrossproduct(lowerTriangularProduct(sample, sqrtScaleMatrix, dimension), dimension);
}
示例4: testSample
import cc.mallet.util.Randoms; //导入依赖的package包/类
public void testSample ()
{
Variable v1 = new Variable (Variable.CONTINUOUS);
Variable v2 = new Variable (Variable.CONTINUOUS);
Randoms r = new Randoms (2343);
Vector mu = new DenseVector (new double[] { 1.0, 2.0 });
Matrix var = new DenseMatrix (new double[][] {{ 0.5, 2.0 }, { 0, 1 }});
// Matrix var = new DenseMatrix (new double[][] {{ 0.5, 2.0 }, { 2.0, 0.75 }});
VarSet vars = new HashVarSet (new Variable[] { v1, v2 });
Factor f = new NormalFactor (vars, mu, var);
TDoubleArrayList v1lst = new TDoubleArrayList ();
TDoubleArrayList v2lst = new TDoubleArrayList ();
for (int i = 0; i < 100000; i++) {
Assignment assn = f.sample (r);
v1lst.add (assn.getDouble (v1));
v2lst.add (assn.getDouble (v2));
}
checkMeanStd (v1lst, 1.0, Math.sqrt (1/0.5));
checkMeanStd (v2lst, 2.0, Math.sqrt (1/0.75));
}
示例5: testSample
import cc.mallet.util.Randoms; //导入依赖的package包/类
public void testSample ()
{
Variable v = new Variable (3);
double[] vals = new double[] { 1, 3, 2 };
TableFactor ptl = new TableFactor (v, vals);
int[] sampled = new int [100];
Randoms r = new Randoms (32423);
for (int i = 0; i < sampled.length; i++) {
sampled[i] = ptl.sampleLocation (r);
}
double sum = MatrixOps.sum (vals);
double[] counts = new double [vals.length];
for (int i = 0; i < vals.length; i++) {
counts[i] = ArrayUtils.count (sampled, i);
}
MatrixOps.print (counts);
for (int i = 0; i < vals.length; i++) {
double prp = counts[i] / ((double) sampled.length);
assertEquals (vals[i] / sum, prp, 0.1);
}
}
示例6: testCholesky
import cc.mallet.util.Randoms; //导入依赖的package包/类
public static void testCholesky() {
int observations = 1000;
double[] mean = new double[20];
double[] precisionMatrix = new double[ 20 * 20 ];
for (int i=0; i<20; i++) {
precisionMatrix[ (20 * i) + i ] = 1.0;
}
Randoms random = new Randoms();
double[] scatterMatrix = getScatterMatrix(nextMVNormal(observations, mean, precisionMatrix, random));
double[] priorPrecision = new double[20];
Arrays.fill(priorPrecision, 1.0);
nextWishartPosterior(scatterMatrix, observations, priorPrecision, 21, 20, random);
}
示例7: testInitialAssignment
import cc.mallet.util.Randoms; //导入依赖的package包/类
public void testInitialAssignment ()
{
Variable[] vars = new Variable[] { new Variable (3), new Variable (3), new Variable (3) };
Variable[] vars1 = new Variable[]{ vars[0], vars[1] };
double[] vals1 = new double[] { 0, 0.2, 0.8, 0, 0.7, 0.3, 0, 0.5, 0.5 };
Factor tbl1 = new TableFactor (vars1, vals1);
Variable[] vars2 = new Variable[]{ vars[1], vars[2] };
double[] vals2 = new double[] { 0.2, 0.2, 0.8, 0.7, 0, 0.7, 0.3, 0, 0.5 };
Factor tbl2 = new TableFactor (vars2, vals2);
FactorGraph fg = new FactorGraph ();
fg.multiplyBy (tbl1);
fg.multiplyBy (tbl2);
System.out.println (fg.dumpToString ());
GibbsSampler gs = new GibbsSampler (new Randoms (324123), 10);
gs.sample (fg, 10); // assert no exception
}
示例8: randomIndex
import cc.mallet.util.Randoms; //导入依赖的package包/类
public int randomIndex (Randoms r)
{
double f = r.nextUniform();
double sum = 0;
int i;
for (i = 0; i < values.length; i++) {
sum += values[i];
//System.out.print (" sum="+sum);
if (sum >= f)
break;
}
//if (sum < f) throw new IllegalStateException
//System.out.println ("i = "+i+", f = "+f+", sum = "+sum);
assert (sum >= f);
return i;
}
示例9: testContinousSample2
import cc.mallet.util.Randoms; //导入依赖的package包/类
public void testContinousSample2 () throws IOException
{
ModelReader reader = new ModelReader ();
FactorGraph fg = reader.readModel (new BufferedReader (new StringReader (uniformMdlstr2)));
Randoms r = new Randoms (324143);
Assignment allAssn = new Assignment ();
for (int i = 0; i < 10000; i++) {
Assignment row = fg.sample (r);
allAssn.addRow (row);
}
Variable x1 = fg.findVariable ("x2");
Assignment assn1 = (Assignment) allAssn.marginalize (x1);
int[] col = assn1.getColumnInt (x1);
double mean = MatrixOps.sum (col) / ((double)col.length);
assertEquals (0.5, mean, 0.01);
Variable x2 = fg.findVariable ("x2");
Assignment assn2 = (Assignment) allAssn.marginalize (x2);
int[] col2 = assn2.getColumnInt (x2);
double mean2 = MatrixOps.sum (col2) / ((double)col2.length);
assertEquals (0.5, mean2, 0.025);
}
示例10: LDAHyper
import cc.mallet.util.Randoms; //导入依赖的package包/类
public LDAHyper (LabelAlphabet topicAlphabet, double alphaSum, double beta, Randoms random)
{
this.data = new ArrayList<Topication>();
this.topicAlphabet = topicAlphabet;
this.numTopics = topicAlphabet.size();
this.alphaSum = alphaSum;
this.alpha = new double[numTopics];
Arrays.fill(alpha, alphaSum / numTopics);
this.beta = beta;
this.random = random;
oneDocTopicCounts = new int[numTopics];
tokensPerTopic = new int[numTopics];
formatter = NumberFormat.getInstance();
formatter.setMaximumFractionDigits(5);
System.err.println("LDA: " + numTopics + " topics");
}
示例11: addInstances
import cc.mallet.util.Randoms; //导入依赖的package包/类
public void addInstances (InstanceList training) {
initializeForTypes (training.getDataAlphabet());
ArrayList<LabelSequence> topicSequences = new ArrayList<LabelSequence>();
for (Instance instance : training) {
LabelSequence topicSequence = new LabelSequence(topicAlphabet, new int[instanceLength(instance)]);
if (false)
// This method not yet obeying its last "false" argument, and must be for this to work
sampleTopicsForOneDoc((FeatureSequence)instance.getData(), topicSequence, false, false);
else {
Randoms r = new Randoms();
int[] topics = topicSequence.getFeatures();
for (int i = 0; i < topics.length; i++)
topics[i] = r.nextInt(numTopics);
}
topicSequences.add (topicSequence);
}
addInstances (training, topicSequences);
}
示例12: testRandomTrainedOn
import cc.mallet.util.Randoms; //导入依赖的package包/类
private double testRandomTrainedOn (InstanceList training)
{
ClassifierTrainer trainer = new MaxEntTrainer ();
Alphabet fd = dictOfSize (3);
String[] classNames = new String[] {"class0", "class1", "class2"};
Randoms r = new Randoms (1);
Iterator<Instance> iter = new RandomTokenSequenceIterator (r, new Dirichlet(fd, 2.0),
30, 0, 10, 200, classNames);
training.addThruPipe (iter);
InstanceList testing = new InstanceList (training.getPipe ());
testing.addThruPipe (new RandomTokenSequenceIterator (r, new Dirichlet(fd, 2.0),
30, 0, 10, 200, classNames));
System.out.println ("Training set size = "+training.size());
System.out.println ("Testing set size = "+testing.size());
Classifier classifier = trainer.train (training);
System.out.println ("Accuracy on training set:");
System.out.println (classifier.getClass().getName()
+ ": " + new Trial (classifier, training).getAccuracy());
System.out.println ("Accuracy on testing set:");
double testAcc = new Trial (classifier, testing).getAccuracy();
System.out.println (classifier.getClass().getName()
+ ": " + testAcc);
return testAcc;
}
示例13: sample
import cc.mallet.util.Randoms; //导入依赖的package包/类
public Assignment sample (Randoms r)
{
int ri = r.nextInt (numRows ());
Object[] vals = (Object[]) values.get (ri);
Assignment assn = new Assignment ();
Variable[] varr = (Variable[]) vars.toArray (new Variable [numVariables ()]);
assn.addRow (varr, vals);
return assn;
}
示例14: sample
import cc.mallet.util.Randoms; //导入依赖的package包/类
public Assignment sample (Randoms r)
{
Variable[] contVars = Factors.continuousVarsOf (this);
if ((contVars.length == 0) || (contVars.length == numVariables ())) {
return sampleInternal (r);
} else {
Assignment paramAssn = sampleContinuousVars (contVars, r);
FactorGraph discreteSliceFg = (FactorGraph) this.slice (paramAssn);
Assignment discreteAssn = discreteSliceFg.sampleInternal (r);
return Assignment.union (paramAssn, discreteAssn);
}
}
示例15: testLogSample
import cc.mallet.util.Randoms; //导入依赖的package包/类
public void testLogSample ()
{
Variable v = new Variable (2);
double[] vals = new double[] { -30, 0 };
LogTableFactor ptl = LogTableFactor.makeFromLogValues (v, vals);
int idx = ptl.sampleLocation (new Randoms (43));
assertEquals (1, idx);
}