public static void testAggregation() { NormalEstimator ne = new NormalEstimator(0.01); NormalEstimator one = new NormalEstimator(0.01); NormalEstimator two = new NormalEstimator(0.01); java.util.Random r = new java.util.Random(1); for (int i = 0; i < 100; i++) { double z = r.nextDouble(); ne.addValue(z, 1); if (i < 50) { one.addValue(z, 1); } else { two.addValue(z, 1); } } try { System.out.println("\n\nFull\n"); System.out.println(ne.toString()); System.out.println("Prob (0): " + ne.getProbability(0)); System.out.println("\nOne\n" + one.toString()); System.out.println("Prob (0): " + one.getProbability(0)); System.out.println("\nTwo\n" + two.toString()); System.out.println("Prob (0): " + two.getProbability(0)); one = one.aggregate(two); System.out.println("\nAggregated\n"); System.out.println(one.toString()); System.out.println("Prob (0): " + one.getProbability(0)); } catch (Exception ex) { ex.printStackTrace(); } }
/** * Add a new data value to the current estimator. * * @param data the new data value * @param weight the weight assigned to the data value */ @Override public void addValue(double data, double weight) { if (weight == 0) { return; } data = round(data); m_SumOfWeights += weight; m_SumOfValues += data * weight; m_SumOfValuesSq += data * data * weight; computeParameters(); }
/** * Main method for testing this class. * * @param argv should contain a sequence of numeric values */ public static void main(String[] argv) { try { if (argv.length == 0) { System.out.println("Please specify a set of instances."); return; } NormalEstimator newEst = new NormalEstimator(0.01); for (int i = 0; i < argv.length; i++) { double current = Double.valueOf(argv[i]).doubleValue(); System.out.println(newEst); System.out.println("Prediction for " + current + " = " + newEst.getProbability(current)); newEst.addValue(current, 1); } NormalEstimator.testAggregation(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
double mean = Math.log(Math.abs(n.getMean())) / Math.log(10.0); double precision = Math.log(Math.abs(n.getPrecision())) / Math.log(10.0); double width = (mean > precision) ? mean : precision; String mean = Utils.doubleToString(n.getMean(), maxWidth, 4).trim(); temp.append(pad(mean, " ", maxWidth + 1 - mean.length(), true)); for (int j = 0; j < m_Instances.numClasses(); j++) { NormalEstimator n = (NormalEstimator) m_Distributions[counter][j]; String stdDev = Utils.doubleToString(n.getStdDev(), maxWidth, 4) .trim(); temp.append(pad(stdDev, " ", maxWidth + 1 - stdDev.length(), true)); for (int j = 0; j < m_Instances.numClasses(); j++) { NormalEstimator n = (NormalEstimator) m_Distributions[counter][j]; String weight = Utils.doubleToString(n.getSumOfWeights(), maxWidth, 4).trim(); temp.append(pad(weight, " ", maxWidth + 1 - weight.length(), true)); for (int j = 0; j < m_Instances.numClasses(); j++) { NormalEstimator n = (NormalEstimator) m_Distributions[counter][j]; String precision = Utils.doubleToString(n.getPrecision(), maxWidth, 4).trim(); temp.append(pad(precision, " ", maxWidth + 1 - precision.length(),
/** * Constructor * * @param numCondSymbols the number of conditioning symbols * @param precision the precision to which numeric values are given. For * example, if the precision is stated to be 0.1, the values in the * interval (0.25,0.35] are all treated as 0.3. */ public NDConditionalEstimator(int numCondSymbols, double precision) { m_Estimators = new NormalEstimator [numCondSymbols]; for(int i = 0; i < numCondSymbols; i++) { m_Estimators[i] = new NormalEstimator(precision); } }
/** * Add a new data value to the current estimator. * * @param data the new data value * @param given the new value that data is conditional upon * @param weight the weight assigned to the data value */ public void addValue(double data, double given, double weight) { m_Estimators[(int)given].addValue(data, weight); }
@Override public NormalEstimator aggregate(NormalEstimator toAggregate) throws Exception { m_SumOfWeights += toAggregate.m_SumOfWeights; m_SumOfValues += toAggregate.m_SumOfValues; m_SumOfValuesSq += toAggregate.m_SumOfValuesSq; if (toAggregate.m_Precision < m_Precision) { m_Precision = toAggregate.m_Precision; } computeParameters(); return this; }
/** * Get a probability estimator for a value * * @param given the new value that data is conditional upon * @return the estimator for the supplied value given the condition */ public Estimator getEstimator(double given) { Estimator result = new DiscreteEstimator(m_Estimators.length,false); for(int i = 0; i < m_Estimators.length; i++) { result.addValue(i,m_Weights.getProbability(i) *m_Estimators[i].getProbability(given)); } return result; }
/** * Get a probability estimate for a value * * @param data the value to estimate the probability of * @return the estimated probability of the supplied value */ @Override public double getProbability(double data) { data = round(data); double zLower = (data - m_Mean - (m_Precision / 2)) / m_StandardDev; double zUpper = (data - m_Mean + (m_Precision / 2)) / m_StandardDev; double pLower = Statistics.normalProbability(zLower); double pUpper = Statistics.normalProbability(zUpper); return pUpper - pLower; }
/** * Main method for testing this class. * * @param argv should contain a sequence of numeric values */ public static void main(String[] argv) { try { if (argv.length == 0) { System.out.println("Please specify a set of instances."); return; } NormalEstimator newEst = new NormalEstimator(0.01); for (int i = 0; i < argv.length; i++) { double current = Double.valueOf(argv[i]).doubleValue(); System.out.println(newEst); System.out.println("Prediction for " + current + " = " + newEst.getProbability(current)); newEst.addValue(current, 1); } NormalEstimator.testAggregation(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
double mean = Math.log(Math.abs(n.getMean())) / Math.log(10.0); double precision = Math.log(Math.abs(n.getPrecision())) / Math.log(10.0); double width = (mean > precision) ? mean : precision; String mean = Utils.doubleToString(n.getMean(), maxWidth, 4).trim(); temp.append(pad(mean, " ", maxWidth + 1 - mean.length(), true)); for (int j = 0; j < m_Instances.numClasses(); j++) { NormalEstimator n = (NormalEstimator) m_Distributions[counter][j]; String stdDev = Utils.doubleToString(n.getStdDev(), maxWidth, 4) .trim(); temp.append(pad(stdDev, " ", maxWidth + 1 - stdDev.length(), true)); for (int j = 0; j < m_Instances.numClasses(); j++) { NormalEstimator n = (NormalEstimator) m_Distributions[counter][j]; String weight = Utils.doubleToString(n.getSumOfWeights(), maxWidth, 4).trim(); temp.append(pad(weight, " ", maxWidth + 1 - weight.length(), true)); for (int j = 0; j < m_Instances.numClasses(); j++) { NormalEstimator n = (NormalEstimator) m_Distributions[counter][j]; String precision = Utils.doubleToString(n.getPrecision(), maxWidth, 4).trim(); temp.append(pad(precision, " ", maxWidth + 1 - precision.length(),
/** * Constructor * * @param numCondSymbols the number of conditioning symbols * @param precision the precision to which numeric values are given. For * example, if the precision is stated to be 0.1, the values in the * interval (0.25,0.35] are all treated as 0.3. */ public NDConditionalEstimator(int numCondSymbols, double precision) { m_Estimators = new NormalEstimator [numCondSymbols]; for(int i = 0; i < numCondSymbols; i++) { m_Estimators[i] = new NormalEstimator(precision); } }
/** * Add a new data value to the current estimator. * * @param data the new data value * @param given the new value that data is conditional upon * @param weight the weight assigned to the data value */ public void addValue(double data, double given, double weight) { m_Estimators[(int)given].addValue(data, weight); }
@Override public NormalEstimator aggregate(NormalEstimator toAggregate) throws Exception { m_SumOfWeights += toAggregate.m_SumOfWeights; m_SumOfValues += toAggregate.m_SumOfValues; m_SumOfValuesSq += toAggregate.m_SumOfValuesSq; if (toAggregate.m_Precision < m_Precision) { m_Precision = toAggregate.m_Precision; } computeParameters(); return this; }
/** * Get a probability estimator for a value * * @param given the new value that data is conditional upon * @return the estimator for the supplied value given the condition */ public Estimator getEstimator(double given) { Estimator result = new DiscreteEstimator(m_Estimators.length,false); for(int i = 0; i < m_Estimators.length; i++) { result.addValue(i,m_Weights.getProbability(i) *m_Estimators[i].getProbability(given)); } return result; }
/** * Get a probability estimate for a value * * @param data the value to estimate the probability of * @return the estimated probability of the supplied value */ @Override public double getProbability(double data) { data = round(data); double zLower = (data - m_Mean - (m_Precision / 2)) / m_StandardDev; double zUpper = (data - m_Mean + (m_Precision / 2)) / m_StandardDev; double pLower = Statistics.normalProbability(zLower); double pUpper = Statistics.normalProbability(zUpper); return pUpper - pLower; }
public static void testAggregation() { NormalEstimator ne = new NormalEstimator(0.01); NormalEstimator one = new NormalEstimator(0.01); NormalEstimator two = new NormalEstimator(0.01); java.util.Random r = new java.util.Random(1); for (int i = 0; i < 100; i++) { double z = r.nextDouble(); ne.addValue(z, 1); if (i < 50) { one.addValue(z, 1); } else { two.addValue(z, 1); } } try { System.out.println("\n\nFull\n"); System.out.println(ne.toString()); System.out.println("Prob (0): " + ne.getProbability(0)); System.out.println("\nOne\n" + one.toString()); System.out.println("Prob (0): " + one.getProbability(0)); System.out.println("\nTwo\n" + two.toString()); System.out.println("Prob (0): " + two.getProbability(0)); one = one.aggregate(two); System.out.println("\nAggregated\n"); System.out.println(one.toString()); System.out.println("Prob (0): " + one.getProbability(0)); } catch (Exception ex) { ex.printStackTrace(); } }
/** * Add a new data value to the current estimator. * * @param data the new data value * @param weight the weight assigned to the data value */ @Override public void addValue(double data, double weight) { if (weight == 0) { return; } data = round(data); m_SumOfWeights += weight; m_SumOfValues += data * weight; m_SumOfValuesSq += data * data * weight; computeParameters(); }
/** * Constructor * * @param numSymbols the number of symbols * @param precision the precision to which numeric values are given. For * example, if the precision is stated to be 0.1, the values in the * interval (0.25,0.35] are all treated as 0.3. */ public DNConditionalEstimator(int numSymbols, double precision) { m_Estimators = new NormalEstimator [numSymbols]; for(int i = 0; i < numSymbols; i++) { m_Estimators[i] = new NormalEstimator(precision); } m_Weights = new DiscreteEstimator(numSymbols, true); }
/** * Add a new data value to the current estimator. * * @param data the new data value * @param given the new value that data is conditional upon * @param weight the weight assigned to the data value */ public void addValue(double data, double given, double weight) { m_Estimators[(int)data].addValue(given, weight); m_Weights.addValue((int)data, weight); }