Program: 117224
Project: 117224
Buggy source:
/** 
 * Builds a classifier.
 * @param gam the GAM.
 * @param terms the list of feature interaction pairs.
 * @param trainSet the training set.
 * @param maxNumIters the maximum number of iterations.
 */
public void buildClassifier(GAM gam,List<IntPair> terms,Instances trainSet,int maxNumIters){
  List<BoostedEnsemble> regressors=new ArrayList<>();
  int[] indices=new int[terms.size()];
  for (int i=0; i < indices.length; i++) {
    indices[i]=gam.terms.indexOf(terms.get(i));
    regressors.add(new BoostedEnsemble());
  }
  double[] target=new double[trainSet.size()];
  for (int i=0; i < target.length; i++) {
    target[i]=trainSet.get(i).getTarget();
  }
  Instances[] bags=Bagging.createBags(trainSet,baggingIters);
  SquareCutter cutter=new SquareCutter(true);
  BaggedEnsembleLearner learner=new BaggedEnsembleLearner(bags.length,cutter);
  double[] pTrain=new double[trainSet.size()];
  double[] rTrain=new double[trainSet.size()];
  OptimUtils.computePseudoResidual(pTrain,target,rTrain);
  for (int i=0; i < pTrain.length; i++) {
    Instance instance=trainSet.get(i);
    pTrain[i]=gam.regress(instance);
  }
  for (int iter=0; iter < maxNumIters; iter++) {
    int k=iter % terms.size();
    for (int i=0; i < trainSet.size(); i++) {
      trainSet.get(i).setTarget(rTrain[i]);
    }
    BoostedEnsemble boostedEnsemble=regressors.get(k);
    IntPair term=terms.get(k);
    cutter.setAttIndices(term.v1,term.v2);
    BaggedEnsemble baggedEnsemble=learner.build(bags);
    if (learningRate != 1) {
      for (int i=0; i < baggedEnsemble.size(); i++) {
        Function2D func=(Function2D)baggedEnsemble.get(i);
        func.multiply(learningRate);
      }
    }
    boostedEnsemble.add(baggedEnsemble);
    for (int i=0; i < trainSet.size(); i++) {
      Instance instance=trainSet.get(i);
      double pred=baggedEnsemble.regress(instance);
      pTrain[i]+=pred;
      rTrain[i]=OptimUtils.getPseudoResidual(pTrain[i],target[i]);
    }
    double measure=metric.eval(pTrain,target);
    if (verbose) {
      System.out.println("String_Node_Str" + iter + "String_Node_Str"+ k+ "String_Node_Str"+ measure);
    }
  }
  for (int i=0; i < target.length; i++) {
    trainSet.get(i).setTarget(target[i]);
  }
  List<Attribute> attributes=trainSet.getAttributes();
  for (int i=0; i < regressors.size(); i++) {
    BoostedEnsemble boostedEnsemble=regressors.get(i);
    IntPair term=terms.get(i);
    Function2D function=CompressionUtils.compress(term.v1,term.v2,boostedEnsemble);
    Attribute f1=attributes.get(term.v1);
    Attribute f2=attributes.get(term.v2);
    int n1=-1;
    if (f1.getType() == Type.BINNED) {
      n1=((BinnedAttribute)f1).getNumBins();
    }
 else     if (f1.getType() == Type.NOMINAL) {
      n1=((NominalAttribute)f1).getCardinality();
    }
    int n2=-1;
    if (f2.getType() == Type.BINNED) {
      n2=((BinnedAttribute)f2).getNumBins();
    }
 else     if (f1.getType() == Type.NOMINAL) {
      n2=((NominalAttribute)f2).getCardinality();
    }
    Regressor newRegressor=function;
    if (n1 > 0 && n2 > 0) {
      newRegressor=CompressionUtils.convert(n1,n2,function);
    }
    if (indices[i] < 0) {
      gam.add(new int[]{term.v1,term.v2},newRegressor);
    }
 else {
      Regressor regressor=gam.regressors.get(indices[i]);
      if (regressor instanceof Function2D) {
        Function2D func=(Function2D)regressor;
        func.add(function);
      }
 else       if (regressor instanceof Array2D) {
        Array2D ary=(Array2D)regressor;
        ary.add((Array2D)newRegressor);
      }
 else {
        throw new RuntimeException("String_Node_Str");
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Builds a classifier.
 * @param gam the GAM.
 * @param terms the list of feature interaction pairs.
 * @param trainSet the training set.
 * @param maxNumIters the maximum number of iterations.
 */
public void buildClassifier(GAM gam,List<IntPair> terms,Instances trainSet,int maxNumIters){
  List<BoostedEnsemble> regressors=new ArrayList<>();
  int[] indices=new int[terms.size()];
  for (int i=0; i < indices.length; i++) {
    indices[i]=gam.terms.indexOf(terms.get(i));
    regressors.add(new BoostedEnsemble());
  }
  double[] target=new double[trainSet.size()];
  for (int i=0; i < target.length; i++) {
    target[i]=trainSet.get(i).getTarget();
  }
  Instances[] bags=Bagging.createBags(trainSet,baggingIters);
  SquareCutter cutter=new SquareCutter(true);
  BaggedEnsembleLearner learner=new BaggedEnsembleLearner(bags.length,cutter);
  double[] pTrain=new double[trainSet.size()];
  double[] rTrain=new double[trainSet.size()];
  for (int i=0; i < pTrain.length; i++) {
    Instance instance=trainSet.get(i);
    pTrain[i]=gam.regress(instance);
  }
  OptimUtils.computePseudoResidual(pTrain,target,rTrain);
  for (int iter=0; iter < maxNumIters; iter++) {
    int k=iter % terms.size();
    for (int i=0; i < trainSet.size(); i++) {
      trainSet.get(i).setTarget(rTrain[i]);
    }
    BoostedEnsemble boostedEnsemble=regressors.get(k);
    IntPair term=terms.get(k);
    cutter.setAttIndices(term.v1,term.v2);
    BaggedEnsemble baggedEnsemble=learner.build(bags);
    if (learningRate != 1) {
      for (int i=0; i < baggedEnsemble.size(); i++) {
        Function2D func=(Function2D)baggedEnsemble.get(i);
        func.multiply(learningRate);
      }
    }
    boostedEnsemble.add(baggedEnsemble);
    for (int i=0; i < trainSet.size(); i++) {
      Instance instance=trainSet.get(i);
      double pred=baggedEnsemble.regress(instance);
      pTrain[i]+=pred;
      rTrain[i]=OptimUtils.getPseudoResidual(pTrain[i],target[i]);
    }
    double measure=metric.eval(pTrain,target);
    if (verbose) {
      System.out.println("String_Node_Str" + iter + "String_Node_Str"+ k+ "String_Node_Str"+ measure);
    }
  }
  for (int i=0; i < target.length; i++) {
    trainSet.get(i).setTarget(target[i]);
  }
  List<Attribute> attributes=trainSet.getAttributes();
  for (int i=0; i < regressors.size(); i++) {
    BoostedEnsemble boostedEnsemble=regressors.get(i);
    IntPair term=terms.get(i);
    Function2D function=CompressionUtils.compress(term.v1,term.v2,boostedEnsemble);
    Attribute f1=attributes.get(term.v1);
    Attribute f2=attributes.get(term.v2);
    int n1=-1;
    if (f1.getType() == Type.BINNED) {
      n1=((BinnedAttribute)f1).getNumBins();
    }
 else     if (f1.getType() == Type.NOMINAL) {
      n1=((NominalAttribute)f1).getCardinality();
    }
    int n2=-1;
    if (f2.getType() == Type.BINNED) {
      n2=((BinnedAttribute)f2).getNumBins();
    }
 else     if (f1.getType() == Type.NOMINAL) {
      n2=((NominalAttribute)f2).getCardinality();
    }
    Regressor newRegressor=function;
    if (n1 > 0 && n2 > 0) {
      newRegressor=CompressionUtils.convert(n1,n2,function);
    }
    if (indices[i] < 0) {
      gam.add(new int[]{term.v1,term.v2},newRegressor);
    }
 else {
      Regressor regressor=gam.regressors.get(indices[i]);
      if (regressor instanceof Function2D) {
        Function2D func=(Function2D)regressor;
        func.add(function);
      }
 else       if (regressor instanceof Array2D) {
        Array2D ary=(Array2D)regressor;
        ary.add((Array2D)newRegressor);
      }
 else {
        throw new RuntimeException("String_Node_Str");
      }
    }
  }
}