Update of /cvsroot/jboost/jboost/src/jboost/booster In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv19099 Modified Files: AbstractBooster.java AdaBoost.java Bag.java BrownBoost.java MixedBinaryPrediction.java NormalizedPrediction.java YabaBoost.java Log Message: Main changes: one sided prediction is optional (USE_ONE_SIDED), cost asymetric code in yababoost/normalboost, new parameters are optional (USE_BETA_RHO), several other changes as well Index: Bag.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/Bag.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** Bag.java 16 May 2007 04:06:02 -0000 1.1.1.1 --- Bag.java 24 Jan 2008 22:30:16 -0000 1.2 *************** *** 12,15 **** --- 12,19 ---- public abstract class Bag { + + /** total weight for examples of each label */ + protected double[] m_w; + /** * Resets the bag to empty (or to its default setting as defined by *************** *** 17,21 **** */ public abstract void reset(); ! /** * If the bag has no weight, then it is considered to be weightless. --- 21,32 ---- */ public abstract void reset(); ! ! /** ! * Get the weights of the bag. ! */ ! public double[] getWeights() { ! return m_w; ! } ! /** * If the bag has no weight, then it is considered to be weightless. Index: NormalizedPrediction.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/NormalizedPrediction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NormalizedPrediction.java 2 Oct 2007 02:32:07 -0000 1.1 --- NormalizedPrediction.java 24 Jan 2008 22:30:16 -0000 1.2 *************** *** 22,29 **** */ public Prediction add(Prediction p); - /* public static boolean isNormPred(Prediction p); */ ! } --- 22,28 ---- */ public Prediction add(Prediction p); /* public static boolean isNormPred(Prediction p); */ ! public double getDeltaT(); } Index: AbstractBooster.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/AbstractBooster.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** AbstractBooster.java 23 Oct 2007 22:45:40 -0000 1.6 --- AbstractBooster.java 24 Jan 2008 22:30:16 -0000 1.7 *************** *** 3,6 **** --- 3,7 ---- import java.io.Serializable; import jboost.controller.Configuration; + import jboost.controller.ControllerConfiguration; import jboost.monitor.Monitor; *************** *** 66,90 **** if (result instanceof jboost.booster.YabaBoost) { ! double c1=0, c2=0, theta=0; ! double rpos=0, c1pos=0, c2pos=0, thetapos=0; ! double rneg=0, c1neg=0, c2neg=0, thetaneg=0; try { c1 = Double.parseDouble(c.getString("c1", "Z1.0")); c2 = Double.parseDouble(c.getString("c2", "Z1.0")); theta = Double.parseDouble(c.getString("theta", "Z0.15")); ! //c1 = Double.parseDouble(c.getString("pos_r", "Z1.0")); ! //c1 = Double.parseDouble(c.getString("pos_c1", "Z1.0")); ! //c2 = Double.parseDouble(c.getString("pos_c2", "Z1.0")); ! //theta = Double.parseDouble(c.getString("pos_theta", "Z0.15")); } catch (NumberFormatException e) { ! String s = "Need to supply r, c1, c2, and theta!"; ! System.err.println(s); ! throw new InstantiationException(s); } - jboost.booster.YabaBoost yaba = (jboost.booster.YabaBoost) result; - yaba.setParams(c1,c2,theta); - yaba.setCostSensitiveParams(rpos, c1pos, c2pos, thetapos, - rneg, c1neg, c2neg, thetaneg); result = yaba; } --- 67,127 ---- + if (result instanceof jboost.booster.YabaBoost) { ! jboost.booster.YabaBoost yaba = (jboost.booster.YabaBoost) result; ! double c1=0, c2=0, theta=0, nc=0; ! double rpos=0, c1pos=0, c2pos=0, thetapos=0, ncpos=0; ! double rneg=0, c1neg=0, c2neg=0, thetaneg=0, ncneg=0; ! ControllerConfiguration conf = (ControllerConfiguration)c; try { c1 = Double.parseDouble(c.getString("c1", "Z1.0")); c2 = Double.parseDouble(c.getString("c2", "Z1.0")); theta = Double.parseDouble(c.getString("theta", "Z0.15")); ! nc = Double.parseDouble(c.getString("nc", "Z0.15")); ! yaba.setParams(c1,c2,theta,nc); ! if (conf.getCostSensitive()) { ! /* ! System.out.println(" runtime: " + rpos + "," + rneg + ! " c1: " + c1pos + "," + c1neg + ! " c2: " + c2pos + "," + c2neg + ! " theta: " + thetapos + "," + thetaneg + ! " nc: " + ncpos + "," + ncneg ! ); ! */ ! rpos = Double.parseDouble(c.getString("pos_c","Z")); ! rneg = Double.parseDouble(c.getString("neg_c","Z")); ! c1pos = Double.parseDouble(c.getString("pos_c1","Z")); ! c1neg = Double.parseDouble(c.getString("neg_c1","Z")); ! /* ! System.out.println(" runtime: " + rpos + "," + rneg + ! " c1: " + c1pos + "," + c1neg + ! " c2: " + c2pos + "," + c2neg + ! " theta: " + thetapos + "," + thetaneg + ! " nc: " + ncpos + "," + ncneg ! ); ! */ ! c2pos = Double.parseDouble(c.getString("pos_c2","Z")); ! c2neg = Double.parseDouble(c.getString("neg_c2","Z")); ! thetapos = Double.parseDouble(c.getString("pos_theta","Z")); ! thetaneg = Double.parseDouble(c.getString("neg_theta","Z")); ! ncpos = Double.parseDouble(c.getString("pos_nc","Z")); ! ncneg = Double.parseDouble(c.getString("neg_nc","Z")); ! /* ! System.out.println(" runtime: " + rpos + "," + rneg + ! " c1: " + c1pos + "," + c1neg + ! " c2: " + c2pos + "," + c2neg + ! " theta: " + thetapos + "," + thetaneg + ! " nc: " + ncpos + "," + ncneg ! ); ! */ ! yaba.setCostSensitiveParams(rpos, c1pos, c2pos, thetapos, ncpos, ! rneg, c1neg, c2neg, thetaneg, ncneg); ! } } catch (NumberFormatException e) { ! System.err.println("Need to supply appropriate parameters!"); ! System.err.println("For YabaBoost normal, we need r, c1, c2, nc, and theta!"); ! System.err.println("For YabaBoost cost sensitive, we need neg_c, pos_c, neg_c1, pos_c1, neg_c2, pos_c2, neg_theta, pos_theta, neg_nc, pos_nc"); ! throw new InstantiationException("Need more params for yaba"); } result = yaba; } Index: MixedBinaryPrediction.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/MixedBinaryPrediction.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** MixedBinaryPrediction.java 13 Dec 2007 07:41:39 -0000 1.4 --- MixedBinaryPrediction.java 24 Jan 2008 22:30:16 -0000 1.5 *************** *** 18,24 **** --- 18,31 ---- public MixedBinaryPrediction(double p) throws NotNormalizedPredException { + (new Exception("asdf")).printStackTrace(); throw new NotNormalizedPredException("MixedBinaryPrediction: Need to normalize based on time!"); } + public static double updateMargin(double currentMargin, double stepSize, double step, double dt, double nc) { + //System.out.println("cm: " + currentMargin + ", deltax: " + stepSize*step + ", dt: " + dt + ", nc: " + nc); + //System.out.println(Math.exp(-dt*nc)*currentMargin + stepSize*step); + return Math.exp(-dt*nc)*currentMargin + stepSize*step; + } + public MixedBinaryPrediction(double p, double dt, double nc) throws NotNormalizedPredException { *************** *** 30,33 **** --- 37,49 ---- + public MixedBinaryPrediction(double p, double dt) + throws NotNormalizedPredException { + super(); + deltaT = dt; + prediction=p; + normalizingConstant = 1; + } + + public Object clone(){ *************** *** 48,52 **** // The prediction p is for h_t double alpha = ((MixedBinaryPrediction) p).prediction; ! prediction = Math.exp(-dt) * prediction + alpha; /* if (Math.abs(alpha) > 1 || Math.abs(prediction) > 1) { --- 64,68 ---- // The prediction p is for h_t double alpha = ((MixedBinaryPrediction) p).prediction; ! prediction = Math.exp(-normalizingConstant*deltaT) * prediction + alpha; /* if (Math.abs(alpha) > 1 || Math.abs(prediction) > 1) { *************** *** 70,73 **** --- 86,97 ---- return this; } + + public double getDeltaT() { + return deltaT; + } + + public double getNormConstant() { + return normalizingConstant; + } public Prediction add(double w, Prediction p) { *************** *** 79,83 **** public boolean equals(Prediction other) { MixedBinaryPrediction bp= (MixedBinaryPrediction) other; ! return (prediction == bp.prediction && dt == bp.dt && normalizingConstant == bp.normalizingConstant); } --- 103,107 ---- public boolean equals(Prediction other) { MixedBinaryPrediction bp= (MixedBinaryPrediction) other; ! return (prediction == bp.prediction && deltaT == bp.deltaT && normalizingConstant == bp.normalizingConstant); } Index: BrownBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/BrownBoost.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** BrownBoost.java 13 Dec 2007 07:21:05 -0000 1.9 --- BrownBoost.java 24 Jan 2008 22:30:16 -0000 1.10 *************** *** 72,76 **** */ protected int[] m_examples; ! /** * Default constructor just calls AdaBoost to --- 72,97 ---- */ protected int[] m_examples; ! ! /** ! * A parameter indicating whether we should use confidence rated ! * predictions. ! */ ! protected static final boolean USE_BETA_RHO = false; ! ! ! /** ! * A parameter indicating whether we should use confidence rated ! * predictions. ! */ ! protected static final boolean USE_CONFIDENCE = false; ! ! ! /** ! * A parameter indicating whether we should use confidence rated ! * predictions. ! */ ! protected static final boolean ONE_SIDED_PREDICTION = USE_CONFIDENCE; ! ! /** * Default constructor just calls AdaBoost to *************** *** 86,90 **** m_examples = new int[m_numExamples]; for (int i=0; i<m_numExamples; i++) { ! m_potentials[i] = calculatePotential(0,m_c); m_examples[i] = i; } --- 107,117 ---- m_examples = new int[m_numExamples]; for (int i=0; i<m_numExamples; i++) { ! if (m_isCostSensitive) { ! m_potentials[i] = calculatePotential(0,m_c,m_labels[i]); ! m_weights[i] = calculateWeight(0,m_c,m_labels[i]); ! } else { ! m_potentials[i] = calculatePotential(0,m_c); ! m_weights[i] = calculateWeight(0,m_c); ! } m_examples[i] = i; } *************** *** 98,105 **** //System.out.println("BrownBoost:\n \t m_c: " + m_c + "\n \t intial potential: " + m_initialPotential); } ! public double getInitialPotential(){ ! return m_initialPotential; } --- 125,160 ---- //System.out.println("BrownBoost:\n \t m_c: " + m_c + "\n \t intial potential: " + m_initialPotential); } ! /* ! public double getRuntime() { ! if (USE_BETA_RHO) { ! return 1.0; ! } else { ! return m_c; ! } ! } ! */ public double getInitialPotential(){ ! return calculatePotential(0.0, m_c); ! } ! ! public double getPositivePotential(){ ! return calculatePotential(0.0, m_posc, POSITIVE_LABEL); ! } ! ! public double getNegativePotential(){ ! return calculatePotential(0.0, m_negc, NEGATIVE_LABEL); ! } ! ! public double getTimeRemaining() { ! if (USE_BETA_RHO) { ! return 1.0 - m_s; ! } else { ! return m_c - m_s; ! } ! } ! ! public double getTimePlayed() { ! return m_s; } *************** *** 109,112 **** --- 164,171 ---- } + public boolean isCostSensitive(){ + return m_isCostSensitive; + } + /** * Checked results with Matlab, and correctness is sufficient. *************** *** 277,285 **** ! protected double solve_constraints(double hyp_err, int[] examples) { if( m_s < 0.001){ m_s = -1; ! return 0; } --- 336,344 ---- ! protected double[] solve_constraints(double hyp_err, int[] examples) { if( m_s < 0.001){ m_s = -1; ! return new double[] {0,0}; } *************** *** 386,394 **** System.err.println("ERROR: Bad solution for t<0"); m_s = m_oldS; ! return(0.0); } //System.out.println("\ns: " + m_s); ! return alpha; } --- 445,453 ---- System.err.println("ERROR: Bad solution for t<0"); m_s = m_oldS; ! return new double[] {0, 0}; } //System.out.println("\ns: " + m_s); ! return new double[] {alpha, t}; } *************** *** 452,455 **** --- 511,518 ---- } + public double calculateWeight(double margin, double time_remaining, short label) { + return calculateWeight(margin, m_s); + } + /** * BrownBoost uses (1-erf(-(margin+s)/c))/2 as the potential function *************** *** 459,462 **** --- 522,530 ---- } + public double calculatePotential(double margin, double time_remaining, short label) { + return calculatePotential(margin, m_s); + } + + /** * BrownBoost uses e^(-(margin+s)^2/c) as the weight calculation *************** *** 464,468 **** public double calculateWeight(double margin, double time_remaining) { double s = time_remaining; ! return Math.exp(-1 * Math.pow(margin+s,2)/m_c); } --- 532,544 ---- public double calculateWeight(double margin, double time_remaining) { double s = time_remaining; ! double mu = -s; ! double weight = Math.exp(-1 * Math.pow(margin+s,2)/m_c); ! if (USE_CONFIDENCE) { ! if (margin < mu) ! weight = 0; ! else ! weight = 2*weight; ! } ! return weight; } *************** *** 472,476 **** public double calculatePotential(double margin, double time_remaining) { double s = time_remaining; ! return (1-erf((margin+s)/Math.sqrt(m_c)))/2; } --- 548,556 ---- public double calculatePotential(double margin, double time_remaining) { double s = time_remaining; ! double pot = (1-erf((margin+s)/Math.sqrt(m_c)))/2; ! if (USE_CONFIDENCE) { ! pot = Math.min(2*pot, 1.0); ! } ! return pot; } *************** *** 504,573 **** - /** - * BrownBag is identical to BinaryBag, except for the method used to - * derive the value of prediction (alpha in the literature). BrownBag - * uses the value of alpha determined by BrownBoost and its variants. - * See comments for AdaBoost.BinaryBag. - * @author Aaron Arvey - */ - class BrownBag extends AdaBoost.BinaryBag{ - - protected BrownBag(int[] list) { - m_w= new double[2]; - reset(); - this.addExampleList(list); - } - - /** compute the binary prediction associated with this bag */ - public BinaryPrediction calcPrediction(double alpha) { - BinaryPrediction ret; - ret = new BinaryPrediction(m_w[1] > m_w[0] ? 1.0 : -1.0 ); - ret.scale(alpha); - return ret; - } - - /** compute the binary prediction associated with this bag */ - public BinaryPrediction calcPrediction(double posAlpha, double negAlpha) { - BinaryPrediction ret; - if (m_w[1] > m_w[0]) { - ret = new BinaryPrediction(1.0); - ret.scale(posAlpha); - } else { - ret = new BinaryPrediction(-1.0); - ret.scale(negAlpha); - } - return ret; - } - - - /** Place holder to ensure that this function is not used in BrownBoost. */ - public BinaryPrediction calcPrediction(){ - //System.err.println("Need to have alpha for prediction in BrownBag.calcPrediction()!"); - return new BinaryPrediction(0); - } - - - /** default constructor */ - protected BrownBag() { - super(); - } - - - /** constructor that copies an existing bag */ - protected BrownBag(BrownBag bag) { - super(bag); - } - - - /** Output the weights in the bag */ - public String toString() { - String s= "BrownBag.\t w0=" + m_w[0] + "\t w1=" + m_w[1] + "\n"; - return s; - } - - - } /* End BrownBag */ - - protected double getHypErr(Bag[] bags, int[][] exampleIndex) { --- 584,587 ---- *************** *** 583,595 **** boolean[] examplesWithHyp = new boolean[m_margins.length]; // Get all examples that have a hypothesis associated with them for (int i= 0; i < exampleIndex.length; i++) { int[] indexes= exampleIndex[i]; ! BinaryPrediction pred = ((BrownBag) bags[i]).calcPrediction(1.0); total += 1; for (int j= 0; j < indexes.length; j++) { int example = indexes[j]; examplesWithHyp[example] = true; double step = getStep(m_labels[example], m_hypPredictions[example]); --- 597,611 ---- boolean[] examplesWithHyp = new boolean[m_margins.length]; + int numExamplesWithHyps = 0; // Get all examples that have a hypothesis associated with them for (int i= 0; i < exampleIndex.length; i++) { int[] indexes= exampleIndex[i]; ! //BinaryPrediction pred = ((BrownBag) bags[i]).calcPrediction(1.0); total += 1; for (int j= 0; j < indexes.length; j++) { int example = indexes[j]; examplesWithHyp[example] = true; + numExamplesWithHyps += 1; double step = getStep(m_labels[example], m_hypPredictions[example]); *************** *** 611,615 **** for (int i=0; i < m_margins.length; i++) { if(!examplesWithHyp[i]){ ! int example = i; m_hypPredictions[example] = 0; double weight = m_weights[example]; --- 627,631 ---- for (int i=0; i < m_margins.length; i++) { if(!examplesWithHyp[i]){ ! int example = i; m_hypPredictions[example] = 0; double weight = m_weights[example]; *************** *** 619,622 **** --- 635,639 ---- potential += calculatePotential(m_margins[i]); } + System.out.println("Num Examples with predictions: " + numExamplesWithHyps); //updatePotential(exampleIndex); *************** *** 669,680 **** // Create a prediction array to accompany the exampleIndex array m_hypPredictions = new double[m_margins.length]; for (int i=0; i < exampleIndex.length; i++){ int[] index = exampleIndex[i]; BrownBag b = (BrownBag)bags[i]; for (int j=0; j < index.length; j++){ int example = index[j]; ! m_hypPredictions[example] = b.calcPrediction(1.0).getClassScores()[0]; } } --- 686,714 ---- + double[] totalWeights = new double[bags.length]; + int maxWeightIndex = 0; + double maxWeight = 0; + for (int i=0; i < bags.length; i++) { + double[] weights = bags[i].getWeights(); + for (int j=0; j < weights.length; j++) { + totalWeights[i] += weights[j]; + if (totalWeights[i] > maxWeight) { + maxWeight = totalWeights[i]; + maxWeightIndex = i; + } + } + } + + // Create a prediction array to accompany the exampleIndex array m_hypPredictions = new double[m_margins.length]; for (int i=0; i < exampleIndex.length; i++){ + if (ONE_SIDED_PREDICTION && i!= maxWeightIndex) + continue; int[] index = exampleIndex[i]; BrownBag b = (BrownBag)bags[i]; for (int j=0; j < index.length; j++){ int example = index[j]; ! m_hypPredictions[example] = b.calcPrediction(1.0,0.0).getClassScores()[0]; } } *************** *** 685,704 **** double gamma = getHypErr(bags, exampleIndex); ! if (m_isCostSensitive) { System.out.println("Solving positive example constraints"); ! double posAlpha = solve_constraints(gamma, m_posExamples); System.out.println("Solving negative example constraints"); ! double negAlpha = solve_constraints(gamma, m_negExamples); for (int i= 0; i < bags.length; i++) { ! p[i]= ((BrownBag) bags[i]).calcPrediction(posAlpha, negAlpha); System.out.println("p[" + i + "] = " + p[i]); } } else { ! double alpha = solve_constraints(gamma, m_examples); for (int i= 0; i < bags.length; i++) { ! p[i]= ((BrownBag) bags[i]).calcPrediction(alpha); ! System.out.println("p[" + i + "] = " + p[i]); } } return p; } --- 719,770 ---- double gamma = getHypErr(bags, exampleIndex); ! if (false) { //m_isCostSensitive) { System.out.println("Solving positive example constraints"); ! double[] sols = solve_constraints(gamma, m_posExamples); ! double posAlpha = sols[0]; ! double posDeltaT = sols[1]; ! System.out.println("Positive alpha, dt: " + posAlpha + "," + posDeltaT); System.out.println("Solving negative example constraints"); ! sols = solve_constraints(gamma, m_negExamples); ! double negAlpha = sols[0]; ! double negDeltaT = sols[1]; ! System.out.println("Negative alpha, dt: " + negAlpha + "," + negDeltaT); for (int i= 0; i < bags.length; i++) { ! p[i]= ((BrownBag) bags[i]).calcPrediction(posAlpha, posDeltaT, negAlpha, negDeltaT); System.out.println("p[" + i + "] = " + p[i]); } } else { ! double[] sols = solve_constraints(gamma, m_examples); ! double alpha = sols[0]; ! double dt = sols[1]; ! if (bags.length > 1) { ! BinaryBag [] bbags = new BinaryBag[2]; ! bbags[0] = (BinaryBag)bags[0]; ! bbags[1] = (BinaryBag)bags[1]; ! for (int i= 0; i < bags.length; i++) { ! if ( (bbags[0].getWeights()[0] > bbags[0].getWeights()[1] && bbags[1].getWeights()[0] > bbags[1].getWeights()[1]) ! || ! (bbags[0].getWeights()[0] < bbags[0].getWeights()[1] && bbags[1].getWeights()[0] < bbags[1].getWeights()[1]) ! || ! true ! ) ! System.out.print("Bag i: " + bags[i]); ! } ! } ! for (int i= 0; i < bags.length; i++) { ! if (i!=maxWeightIndex && ONE_SIDED_PREDICTION) { ! p[i]= ((BrownBag) bags[i]).calcPrediction(0,dt); ! } else { ! p[i]= ((BrownBag) bags[i]).calcPrediction(alpha,dt); ! } ! //System.out.print("Bag i: " + bags[i]); ! //System.out.println("p[" + i + "] = " + p[i]); } } + + for (int i=0; i<totalWeights.length; i++) + System.out.println("Totalweights[" + i + "]=" + totalWeights[i]); + return p; } *************** *** 725,727 **** --- 791,870 ---- } + + + + /** + * BrownBag is identical to BinaryBag, except for the method used to + * derive the value of prediction (alpha in the literature). BrownBag + * uses the value of alpha determined by BrownBoost and its variants. + * See comments for AdaBoost.BinaryBag. + * @author Aaron Arvey + */ + class BrownBag extends AdaBoost.BinaryBag{ + + protected BrownBag(int[] list) { + m_w= new double[2]; + reset(); + this.addExampleList(list); + } + + /** compute the binary prediction associated with this bag */ + public BinaryPrediction calcPrediction(double alpha) { + BinaryPrediction ret; + ret = new BinaryPrediction(m_w[1] > m_w[0] ? 1.0 : -1.0 ); + ret.scale(alpha); + return ret; + } + + /** compute the binary prediction associated with this bag */ + public BinaryPrediction calcPrediction(double alpha, double dt) { + //if (this instanceof BrownBag) + return calcPrediction(alpha); + //if (this instanceof BrownBag) + //return ((YabaBag)this).calcPrediction(alpha,dt); + } + + /** compute the binary prediction associated with this bag */ + public BinaryPrediction calcPrediction(double posAlpha, double posDeltaT, double negAlpha, double negDeltaT) { + BinaryPrediction ret; + if (m_w[1] > m_w[0]) { + ret = new BinaryPrediction(1.0); + ret.scale(posAlpha); + } else { + ret = new BinaryPrediction(-1.0); + ret.scale(negAlpha); + } + return ret; + } + + + /** Place holder to ensure that this function is not used in BrownBoost. */ + public BinaryPrediction calcPrediction(){ + //System.err.println("Need to have alpha for prediction in BrownBag.calcPrediction()!"); + return new BinaryPrediction(0); + } + + + /** default constructor */ + protected BrownBag() { + super(); + } + + + /** constructor that copies an existing bag */ + protected BrownBag(BrownBag bag) { + super(bag); + } + + + /** Output the weights in the bag */ + public String toString() { + String s= "BrownBag.\t w0=" + m_w[0] + "\t w1=" + m_w[1] + "\n"; + return s; + } + + + } /* End BrownBag */ + + } /* End BrownBoost Class */ Index: YabaBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/YabaBoost.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** YabaBoost.java 23 Oct 2007 22:45:40 -0000 1.6 --- YabaBoost.java 24 Jan 2008 22:30:16 -0000 1.7 *************** *** 1,5 **** package jboost.booster; - import java.io.BufferedWriter; import java.io.FileWriter; --- 1,4 ---- *************** *** 72,75 **** --- 71,81 ---- /** + * The normalization constant for updating the score (might be alpha in Freund & Arvey 2008). + */ + protected double m_nc = 1.0; + protected double m_posNc = 1.0; + protected double m_negNc = 1.0; + + /** * A parameter indicating when we should stop the game. * Technically, this should be 0; however due to numerical instability *************** *** 77,89 **** * an approximation of the 0/1 loss instead of the exact 0/1 loss. */ ! protected static final double FINISH_GAME_NOW = 0.05; ! ! ! /** ! * A parameter indicating whether we should use confidence rated ! * predictions. ! */ ! protected static final boolean USE_CONFIDENCE = false; ! --- 83,87 ---- * an approximation of the 0/1 loss instead of the exact 0/1 loss. */ ! protected static final double FINISH_GAME_NOW = 0.02; *************** *** 95,102 **** */ protected static final double THETA_UPDATE_STEP = 0.02; /** ! * Default constructor just calls AdaBoost to ! * get everything initialized */ public YabaBoost() { --- 93,101 ---- */ protected static final double THETA_UPDATE_STEP = 0.02; + /** ! * Default constructor just calls AdaBoost to get everything ! * initialized */ public YabaBoost() { *************** *** 111,126 **** m_c = runtime; m_s = m_c; ! m_initialPotential = calculatePotential(0,m_c); } ! public void setParams(double c1, double c2, double theta) throws Exception { double EPS = 0.01; if (c1 < EPS || c2 < EPS || theta < EPS || m_c < EPS) { ! throw new Exception("Yaba params are bad!\nc1:"+c1+"\nc2:" ! +c2+"\ntheta:"+theta+"\n"); } m_c1 = c1; m_c2 = c2; m_theta = theta; m_origTheta = theta; m_initialPotential = calculatePotential(0,m_c); --- 110,127 ---- m_c = runtime; m_s = m_c; ! m_initialPotential = calculatePotential(0,m_c,NO_LABEL); } ! public void setParams(double c1, double c2, double theta, double nc) throws Exception { double EPS = 0.01; if (c1 < EPS || c2 < EPS || theta < EPS || m_c < EPS) { ! /*throw new Exception("Yaba params are bad!\nc1:"+c1+"\nc2:" ! +c2+"\ntheta:"+theta+"\n");*/ ! System.err.println("Bad yaba params!"); } m_c1 = c1; m_c2 = c2; m_theta = theta; + m_nc = nc; m_origTheta = theta; m_initialPotential = calculatePotential(0,m_c); *************** *** 128,164 **** ! public void setCostSensitiveParams(double pc, double pc1, double pc2, double ptheta, ! double nc, double nc1, double nc2, double ntheta) { m_posc = pc; m_posc1 = pc1; m_posc2 = pc2; m_posTheta = ptheta; m_posOrigTheta = ptheta; ! m_negc = pc; ! m_negc1 = pc1; ! m_negc2 = pc2; ! m_negTheta = ptheta; ! m_negOrigTheta = ptheta; } - public String surfingData() { - StringBuffer ret = new StringBuffer(""); - ret.append(String.format("YabaBoost Params: %.4f %.4f %4f %4f %4f\n", m_c, m_s, m_c1, m_c2, m_theta)); - for (int i=0; i<m_margins.length; i++){ - ret.append(String.format("%.4f\t%.4f\t%.4f\n", m_margins[i], m_weights[i], m_potentials[i])); - } - return ret.toString(); - } - - public void finalizeData() { double EPS = 0.01; ! if (m_c1 < EPS || m_c2 < EPS || m_theta < EPS || m_c < EPS) { System.err.println("Yaba params are bad!\nc:"+m_c+"\nc1:" +m_c1+"\nc2:"+m_c2+"\ntheta:"+m_theta+"\n"); ! System.exit(2); } m_initialPotential = calculatePotential(0,m_c); --- 129,169 ---- ! public void setCostSensitiveParams(double pc, double pc1, double pc2, double ptheta, double pnc, ! double nc, double nc1, double nc2, double ntheta, double nnc) { m_posc = pc; m_posc1 = pc1; m_posc2 = pc2; + m_posNc = pnc; m_posTheta = ptheta; m_posOrigTheta = ptheta; + m_posInitialPotential = calculatePotential(0.0,m_posc,POSITIVE_LABEL); + System.out.println("pc, pc1, pc2, pnc, ptheta, pos pot: " + pc + "," + pc1 + + "," + pc2 + "," + ptheta + "," + pnc + "," + + m_posInitialPotential); ! m_negc = nc; ! m_negc1 = nc1; ! m_negc2 = nc2; ! m_negNc = nnc; ! m_negTheta = ntheta; ! m_negOrigTheta = ntheta; ! m_negInitialPotential = calculatePotential(0.0,m_negc,NEGATIVE_LABEL); ! ! m_isCostSensitive = true; } public void finalizeData() { double EPS = 0.01; ! if (!m_isCostSensitive && (m_c1 < EPS || m_c2 < EPS || m_theta < EPS || m_c < EPS || m_nc < EPS)) { System.err.println("Yaba params are bad!\nc:"+m_c+"\nc1:" +m_c1+"\nc2:"+m_c2+"\ntheta:"+m_theta+"\n"); ! //System.exit(2); ! } else if (m_isCostSensitive && (m_posc1 < EPS || m_posc2 < EPS || m_posTheta < EPS || m_posc < EPS || m_posNc < EPS || ! m_negc1 < EPS || m_negc2 < EPS || m_negTheta < EPS || m_negc < EPS || m_negNc < EPS)) { ! System.err.println("Yaba cost sensitive params are bad!\nc:"+m_c+"\nc1:" ! +m_c1+"\nc2:"+m_c2+"\ntheta:"+m_theta+"\n"); ! ! //System.exit(2); } m_initialPotential = calculatePotential(0,m_c); *************** *** 168,172 **** + "\t m_c2: " + m_c2 + "\n" + "\t m_theta: " + m_theta + "\n" ! + "\t initial potential: " + m_initialPotential + "\n"; System.out.println(yabaout); --- 173,191 ---- + "\t m_c2: " + m_c2 + "\n" + "\t m_theta: " + m_theta + "\n" ! + "\t m_nc: " + m_nc + "\n" ! + "\t m_initialPotential: " + m_initialPotential + "\n" ! + "\t m_posc: " + m_posc + "\n" ! + "\t m_posc1: " + m_posc1 + "\n" ! + "\t m_posc2: " + m_posc2 + "\n" ! + "\t m_posTheta: " + m_posTheta + "\n" ! + "\t m_posNc: " + m_posNc + "\n" ! + "\t m_posInitialPotential: " + m_posInitialPotential + "\n" ! + "\t m_negc: " + m_negc + "\n" ! + "\t m_negc1: " + m_negc1 + "\n" ! + "\t m_negc2: " + m_negc2 + "\n" ! + "\t m_negTheta: " + m_negTheta + "\n" ! + "\t m_negNc: " + m_negNc + "\n" ! + "\t m_negInitialPotential: " + m_negInitialPotential + "\n" ! + "\t m_isCostSensitive: " + m_isCostSensitive + "\n" + "\n"; System.out.println(yabaout); *************** *** 205,218 **** step = getStep(m_labels[example], m_hypPredictions[example]); ! EPS = 0.0001; if (Math.abs(step) > EPS) { ! new_margin = (1-alpha)*margin + alpha*step; } else { new_margin = margin; } ! new_weight = calculateWeight(new_margin, new_time_remaining); ! new_pot = calculatePotential(new_margin, new_time_remaining); ! orig_pot = calculatePotential(orig_margin, orig_time_remaining); vars.B += new_weight*step; --- 224,238 ---- step = getStep(m_labels[example], m_hypPredictions[example]); ! EPS = 0.000001; if (Math.abs(step) > EPS) { ! new_margin = MixedBinaryPrediction.updateMargin(margin, alpha, step, t, m_nc); } else { new_margin = margin; } ! new_weight = calculateWeight(new_margin, new_time_remaining, m_labels[example]); ! new_pot = calculatePotential(new_margin, new_time_remaining, m_labels[example]); ! orig_pot = calculatePotential(orig_margin, orig_time_remaining, m_labels[example]); ! //System.out.println("New weight is: " + new_weight); vars.B += new_weight*step; *************** *** 278,282 **** * @return alpha - An appropriate value of alpha that satisfies constraints. */ ! protected double solve_constraints(double hyp_err, int[] examples) { /* * If the game has a small amount of time remaining, quit now. --- 298,303 ---- * @return alpha - An appropriate value of alpha that satisfies constraints. */ ! protected double[] solve_constraints(double hyp_err, int[] examples) { ! /* * If the game has a small amount of time remaining, quit now. *************** *** 286,290 **** if( m_s < FINISH_GAME_NOW){ m_s = -1; ! return 0; } --- 307,311 ---- if( m_s < FINISH_GAME_NOW){ m_s = -1; ! return new double[] {0,0}; } *************** *** 308,311 **** --- 329,333 ---- double t=0.3; double alpha=0.1; + /* *************** *** 370,377 **** // Increment alpha alpha += alpha_step; - //System.out.print(" alpha: " + alpha); // Deal with boundary cases, keep margin normalized ! if(alpha < 0 || alpha > 1) { alpha = Math.round(alpha); alpha_step = -alpha_step; --- 392,398 ---- // Increment alpha alpha += alpha_step; // Deal with boundary cases, keep margin normalized ! if(alpha < -0.1 || alpha > 1) { alpha = Math.round(alpha); alpha_step = -alpha_step; *************** *** 464,470 **** if (t < 0) { - dump_everything(vars); System.err.println("The change in time is negtive!"); System.err.println("Outputting all data to outfile!"); System.exit(2); } --- 485,491 ---- if (t < 0) { System.err.println("The change in time is negtive!"); System.err.println("Outputting all data to outfile!"); + dump_everything(vars); System.exit(2); } *************** *** 480,484 **** m_lastAlpha = alpha; ! return alpha; } --- 501,505 ---- m_lastAlpha = alpha; ! return new double[] {alpha, t}; } *************** *** 503,514 **** double[] value = new double[] { -p, p }; int[] indexes = exampleIndex[i]; for (int j= 0; j < indexes.length; j++) { int example = indexes[j]; - double alpha = Math.abs(p); // Set margins to new margin // value[m_labels[example]] is equal to alpha*step double EPS = 0.0001; ! if (Math.abs(p) - m_lastAlpha > EPS) { System.err.println("Prediction was messed up!"); System.err.println("alpha was: " + m_lastAlpha); --- 524,537 ---- double[] value = new double[] { -p, p }; int[] indexes = exampleIndex[i]; + double alpha = Math.abs(p); + double dt = ((MixedBinaryPrediction)predictions[i]).getDeltaT(); + double step; for (int j= 0; j < indexes.length; j++) { int example = indexes[j]; // Set margins to new margin // value[m_labels[example]] is equal to alpha*step double EPS = 0.0001; ! if (Math.abs(Math.abs(p) - m_lastAlpha) > EPS) { System.err.println("Prediction was messed up!"); System.err.println("alpha was: " + m_lastAlpha); *************** *** 516,531 **** } m_oldMargins[example] = m_margins[example]; ! m_margins[example] = (1-alpha)*m_margins[example] ! + value[m_labels[example]]; ! ! if(Math.abs(m_margins[example]) > 1){ ! System.out.println("m_margins[" +example+ "]: " ! + m_margins[example] + " greater than 1!!!!"); ! } } } ! ! if (m_hypPredictions.length != m_margins.length || m_hypPredictions.length != m_numExamples) { System.err.println("WARNING: m_hypPredictions is not the same length as the margins"); } --- 539,549 ---- } m_oldMargins[example] = m_margins[example]; ! step = (value[m_labels[example]] >= 0) ? 1.0 : -1.0; ! m_margins[example] = MixedBinaryPrediction.updateMargin(m_margins[example], Math.abs(value[m_labels[example]]), step, dt, m_nc); } } ! ! if (m_hypPredictions == null || m_hypPredictions.length != m_margins.length || m_hypPredictions.length != m_numExamples) { System.err.println("WARNING: m_hypPredictions is not the same length as the margins"); } *************** *** 556,559 **** --- 574,578 ---- */ public double calculatePotential(double margin) { + //System.out.println("potential being called"); return calculatePotential(margin, m_s); } *************** *** 564,581 **** * @return weight of any example with the given margin */ ! public double calculateWeight(double margin, double time_remaining) { ! double s = m_c - time_remaining; ! double mu = m_theta - m_c1* ( Math.exp(-s) - Math.exp(-m_c) ); ! double sd = Math.sqrt(m_c2*(Math.exp(-2*s) - Math.exp(-2*m_c))) + FINISH_GAME_NOW; ! double norm = 1 / Math.sqrt(Math.PI * sd * sd); ! double ret = norm * Math.exp(- Math.pow((margin-mu)/sd,2)); ! if (USE_CONFIDENCE) { ! if (margin < mu) ! ret = 0; ! else ! ret = 2*ret; } return ret; } --- 583,646 ---- * @return weight of any example with the given margin */ ! public double calculateWeight(double margin, double time_remaining){ ! return calculateWeight(margin, time_remaining, (short)NO_LABEL); ! } ! /** ! * @param margin ! * @param time time remaining in game ! * @param label the label of the example (for asymmetric weighting) ! * @return weight of any example with the given margin ! */ ! public double calculateWeight(double margin, double time_remaining, short label) { ! double ret; ! if (label==POSITIVE_LABEL && m_isCostSensitive) { ! ret = calculateWeight(margin, time_remaining, m_c, m_posc1, m_posc2, m_posTheta, m_posNc); ! } else if (label==NEGATIVE_LABEL && m_isCostSensitive) { ! ret = calculateWeight(margin, time_remaining, m_c, m_negc1, m_negc2, m_negTheta, m_negNc); ! } else { ! ret = calculateWeight(margin, time_remaining, m_c, m_c1, m_c2, m_theta, m_nc); ! } ! return ret; ! } ! ! ! ! /** ! * @param margin ! * @param time time remaining in game ! * @param c total time of game ! * @param c1 scale of mean ! * @param c2 scale of variance ! * @param theta mean of the final function ! * @param nc normalization constant ! * @return weight of any example with the given margin ! */ ! public double calculateWeight(double margin, double time_remaining, ! double c, double c1, double c2, ! double theta, double nc) { ! double s=0, mu=0, sd=0, norm=0, ret=0; ! s = c - time_remaining; ! mu = theta - c1* ( Math.exp(-s) - Math.exp(-c) ); ! sd = Math.sqrt(c2*(Math.exp(-2*s) - Math.exp(-2*c))) + FINISH_GAME_NOW; ! ! /** ! * To translate between notations (Freund & Arvey -> code ! * here): rho is c1, theta is theta, alpha is nc. ! */ ! if (USE_BETA_RHO) { ! double rho = c1; ! double alpha = nc; ! double t = s; ! mu = rho + Math.exp(-alpha*(1-t))*(theta - rho); ! sd = Math.sqrt( (Math.exp(2*alpha*(1-t)) - 1) / (2*alpha) ); } + norm = 1 / Math.sqrt(Math.PI * sd * sd); + ret = norm * Math.exp(- Math.pow((margin-mu)/sd,2)); + if (USE_CONFIDENCE) { + if (margin < mu) ret = 0; + else ret = 2*ret; + } return ret; } *************** *** 584,594 **** /** * @param margin * @return potential of example with given margin */ public double calculatePotential(double margin, double time_remaining) { ! double s = m_c - time_remaining; ! double mu = m_theta - m_c1 * (Math.exp(-s) - Math.exp(-m_c)); ! double sd = Math.sqrt(m_c2 * (Math.exp(-2*s) - Math.exp(-2*m_c))) + FINISH_GAME_NOW; ! double pot = (1 - erf((margin-mu)/sd)) / 2; if (USE_CONFIDENCE) { pot = Math.min(2*pot, 1.0); --- 649,717 ---- /** * @param margin + * @param time_remaining * @return potential of example with given margin */ public double calculatePotential(double margin, double time_remaining) { ! return calculatePotential(margin, time_remaining, NO_LABEL); ! } ! ! /** ! * @param margin ! * @param time_remaining ! * @param label ! * @return potential of example with given margin ! */ ! public double calculatePotential(double margin, double time_remaining, short label) { ! double ret = 0; ! if (label==POSITIVE_LABEL && m_isCostSensitive) { ! ret = calculatePotential(margin, time_remaining, m_c, m_posc1, m_posc2, m_posTheta, m_posNc); ! } else if (label==NEGATIVE_LABEL && m_isCostSensitive) { ! ret = calculatePotential(margin, time_remaining, m_c, m_negc1, m_negc2, m_negTheta, m_negNc); ! } else { ! ret = calculatePotential(margin, time_remaining, m_c, m_c1, m_c2, m_theta, m_nc); ! } ! return ret; ! } ! ! /** ! * @param margin ! * @return potential of example with given margin ! */ ! double calculatePotential(double margin, double time_remaining, double c, double c1, double c2, double theta, double nc) { ! double s, mu, sd, pot; ! s = c - time_remaining; ! mu = theta - c1 * (Math.exp(-s) - Math.exp(-c)); ! sd = Math.sqrt(c2 * (Math.exp(-2*s) - Math.exp(-2*c))) + FINISH_GAME_NOW; ! ! /** ! * To translate between notations (Freund & Arvey -> code ! * here): rho is c1, theta is theta, alpha is nc. ! */ ! if (USE_BETA_RHO) { ! double rho = c1; ! double alpha = nc; ! double t = s; ! mu = rho + Math.exp(-alpha*(1-t))*(theta - rho); ! sd = Math.sqrt( (Math.exp(2*alpha*(1-t)) - 1) / (2*alpha) ); ! /* ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! System.out.println("Using beta, rho potential"); ! System.out.println("Using beta, rho potentialb"); ! */ ! } ! ! pot = (1 - erf((margin-mu)/sd)) / 2; ! if (USE_CONFIDENCE) { pot = Math.min(2*pot, 1.0); *************** *** 612,616 **** public String getParamString() { ! String ret = String.format("YabaBoost s=%.4f c=%.4f c1=%.4f c2=%.4f theta=%.4f confidence=%b ", m_s, m_c, m_c1, m_c2, m_theta, USE_CONFIDENCE); return ret; } --- 735,747 ---- public String getParamString() { ! String ret = String.format("YabaBoost s=%.4f c=%.4f c1=%.4f c2=%.4f theta=%.4f nc=%.4f " + ! "poss=%.4f negs=%.4f posc=%.4f negc=%.4f posc1=%.4f " + ! "negc1=%.4f posc2=%.4f negc2=%.4f posnc=%.4f negnc=%.4f " + ! "postheta=%.4f negtheta=%.4f confidence=%b " + ! "asymmetric=%b onesided=%b betarho=%b", ! m_s, m_c, m_c1, m_c2, m_theta, m_nc, ! -5.0, -5.0, m_posc, m_negc, m_posc1, m_negc1, m_posc2, m_negc2, ! m_posNc, m_negNc, m_posTheta, m_negTheta, ! USE_CONFIDENCE, m_isCostSensitive, ONE_SIDED_PREDICTION, USE_BETA_RHO); return ret; } *************** *** 635,640 **** public MixedBinaryPrediction calcPrediction(double alpha) { MixedBinaryPrediction ret = new MixedBinaryPrediction(0); try { ! ret = new MixedBinaryPrediction(m_w[1] > m_w[0] ? 1.0 : -1.0 ); } catch (NotNormalizedPredException e){ System.err.println(e.getStackTrace()); --- 766,777 ---- public MixedBinaryPrediction calcPrediction(double alpha) { MixedBinaryPrediction ret = new MixedBinaryPrediction(0); + return ret; + } + + /** compute the binary prediction associated with this bag */ + public MixedBinaryPrediction calcPrediction(double alpha, double dt) { + MixedBinaryPrediction ret = new MixedBinaryPrediction(0,0); try { ! ret = new MixedBinaryPrediction(m_w[1] > m_w[0] ? 1.0 : -1.0, dt); } catch (NotNormalizedPredException e){ System.err.println(e.getStackTrace()); *************** *** 695,699 **** protected MixedBinaryPrediction getZeroPred() { ! return new MixedBinaryPrediction(0); } --- 832,836 ---- protected MixedBinaryPrediction getZeroPred() { ! return new MixedBinaryPrediction(0,0); } Index: AdaBoost.java =================================================================== RCS file: /cvsroot/jboost/jboost/src/jboost/booster/AdaBoost.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** AdaBoost.java 23 Oct 2007 22:45:40 -0000 1.4 --- AdaBoost.java 24 Jan 2008 22:30:16 -0000 1.5 *************** *** 43,46 **** --- 43,53 ---- protected int m_numNegExamples; + /** Constant for positive label for cost sensitive stuff */ + protected final short POSITIVE_LABEL = 0; + /** Constant for negative label for cost sensitive stuff */ + protected final short NEGATIVE_LABEL = 1; + /** Constant for non cost sensitive stuff */ + protected final short NO_LABEL = -99; + /** sampling weights for the examples */ protected double[] m_sampleWeights; *************** *** 101,105 **** m_numExamples++; m_tmpList.add(new TmpData(index, (short) l, weight)); ! if (l==1) m_numPosExamples++; } else { failed= "AdaBoost.addExample received index " + index + ", when it expected index " + --- 108,112 ---- m_numExamples++; m_tmpList.add(new TmpData(index, (short) l, weight)); ! if (l==POSITIVE_LABEL) m_numPosExamples++; } else { failed= "AdaBoost.addExample received index " + index + ", when it expected index " + *************** *** 160,167 **** m_weights[index]= m_oldWeights[index]= defaultWeight; m_labels[index]= a.getLabel(); ! if (a.getLabel()==1) m_posExamples[m_posIndex++] = index; ! else m_negExamples[m_negIndex++] = index; m_sampleWeights[index]= a.getWeight(); } --- 167,179 ---- m_weights[index]= m_oldWeights[index]= defaultWeight; m_labels[index]= a.getLabel(); ! if (a.getLabel()==POSITIVE_LABEL) m_posExamples[m_posIndex++] = index; ! else if (a.getLabel()==NEGATIVE_LABEL) m_negExamples[m_negIndex++] = index; + else { + System.err.println("Label of example is unknown to adaboost"); + System.exit(2); + } + m_sampleWeights[index]= a.getWeight(); } *************** *** 311,314 **** --- 323,340 ---- */ public Prediction[] getPredictions(Bag[] bags, int[][] exampleIndex) { + // Code to see how often the splitting partition predicts the same way on both sides + /* + if (bags.length > 1) { + BinaryBag [] bbags = new BinaryBag[2]; + bbags[0] = (BinaryBag)bags[0]; + bbags[1] = (BinaryBag)bags[1]; + for (int i= 0; i < bags.length; i++) { + if ( (bbags[0].getWeights()[0] > bbags[0].getWeights()[1] && bbags[1].getWeights()[0] > bbags[1].getWeights()[1]) + || + (bbags[0].getWeights()[0] < bbags[0].getWeights()[1] && bbags[1].getWeights()[0] < bbags[1].getWeights()[1]) + ) + System.out.print("Bag i: " + bags[i]); + } + }*/ return getPredictions(bags); } *************** *** 408,414 **** class BinaryBag extends Bag { - /** total weight for examples of each label */ - protected double[] m_w; - /** default constructor */ protected BinaryBag() { --- 434,437 ---- |