public static TableFactor retainMass (DiscreteFactor ptl, double alpha) { int[] idxs = new int [ptl.numLocations ()]; double[] vals = new double [ptl.numLocations ()]; for (int i = 0; i < idxs.length; i++) { idxs[i] = ptl.indexAtLocation (i); vals[i] = ptl.logValue (i); } RankedFeatureVector rfv = new RankedFeatureVector (new Alphabet(), idxs, vals); TIntArrayList idxList = new TIntArrayList (); TDoubleArrayList valList = new TDoubleArrayList (); double mass = Double.NEGATIVE_INFINITY; double logAlpha = Math.log (alpha); for (int rank = 0; rank < rfv.numLocations (); rank++) { int idx = rfv.getIndexAtRank (rank); double val = rfv.value (idx); mass = Maths.sumLogProb (mass, val); idxList.add (idx); valList.add (val); if (mass > logAlpha) { break; } } int[] szs = computeSizes (ptl); SparseMatrixn m = new SparseMatrixn (szs, idxList.toNativeArray (), valList.toNativeArray ()); TableFactor result = new TableFactor (computeVars (ptl)); result.setValues (m); return result; }
public static TableFactor retainMass (DiscreteFactor ptl, double alpha) { int[] idxs = new int [ptl.numLocations ()]; double[] vals = new double [ptl.numLocations ()]; for (int i = 0; i < idxs.length; i++) { idxs[i] = ptl.indexAtLocation (i); vals[i] = ptl.logValue (i); } RankedFeatureVector rfv = new RankedFeatureVector (new Alphabet(), idxs, vals); TIntArrayList idxList = new TIntArrayList (); TDoubleArrayList valList = new TDoubleArrayList (); double mass = Double.NEGATIVE_INFINITY; double logAlpha = Math.log (alpha); for (int rank = 0; rank < rfv.numLocations (); rank++) { int idx = rfv.getIndexAtRank (rank); double val = rfv.value (idx); mass = Maths.sumLogProb (mass, val); idxList.add (idx); valList.add (val); if (mass > logAlpha) { break; } } int[] szs = computeSizes (ptl); SparseMatrixn m = new SparseMatrixn (szs, idxList.toNativeArray (), valList.toNativeArray ()); TableFactor result = new TableFactor (computeVars (ptl)); result.setValues (m); return result; }
public static TableFactor retainMass (DiscreteFactor ptl, double alpha) { int[] idxs = new int [ptl.numLocations ()]; double[] vals = new double [ptl.numLocations ()]; for (int i = 0; i < idxs.length; i++) { idxs[i] = ptl.indexAtLocation (i); vals[i] = ptl.logValue (i); } RankedFeatureVector rfv = new RankedFeatureVector (new Alphabet(), idxs, vals); TIntArrayList idxList = new TIntArrayList (); TDoubleArrayList valList = new TDoubleArrayList (); double mass = Double.NEGATIVE_INFINITY; double logAlpha = Math.log (alpha); for (int rank = 0; rank < rfv.numLocations (); rank++) { int idx = rfv.getIndexAtRank (rank); double val = rfv.value (idx); mass = Maths.sumLogProb (mass, val); idxList.add (idx); valList.add (val); if (mass > logAlpha) { break; } } int[] szs = computeSizes (ptl); SparseMatrixn m = new SparseMatrixn (szs, idxList.toNativeArray (), valList.toNativeArray ()); TableFactor result = new TableFactor (computeVars (ptl)); result.setValues (m); return result; }