Program: 117235
Project: 117235
Buggy source:
/** 
 * Builds elastic-net penalized regressors for a sequence of regularization parameter lambdas. Each row of the input represents a feature (instead of a data point), i.e., in column-oriented format. This procedure does not assume the data is normalized or centered.
 * @param attrs the attribute list.
 * @param indices the indices.
 * @param values the values.
 * @param y the targets.
 * @param maxNumIters the maximum number of iterations.
 * @param numLambdas the number of lambdas.
 * @param minLambdaRatio the minimum lambda is minLambdaRatio * max lambda.
 * @param l1Ratio the L1 ratio.
 * @return elastic-net penalized regressors.
 */
public GLM[] buildRegressors(int[] attrs,int[][] indices,double[][] values,double[] y,int maxNumIters,int numLambdas,double minLambdaRatio,double l1Ratio){
  double[] w=new double[attrs.length];
  double intercept=0;
  GLM[] glms=new GLM[numLambdas];
  double[] rTrain=new double[y.length];
  for (int i=0; i < rTrain.length; i++) {
    rTrain[i]=y[i];
  }
  double[] sq=new double[values.length];
  for (int i=0; i < values.length; i++) {
    sq[i]=StatUtils.sumSq(values[i]);
  }
  double maxLambda=findMaxLambda(indices,values,y,l1Ratio);
  double alpha=Math.pow(minLambdaRatio,1.0 / numLambdas);
  double lambda=maxLambda;
  for (int g=0; g < glms.length; g++) {
    final double lambda1=lambda * l1Ratio;
    final double lambda2=lambda * (1 - l1Ratio);
    final double tl1=lambda1 * y.length;
    final double tl2=lambda2 * y.length;
    double prevLoss=GLMOptimUtils.computeElasticNetLoss(rTrain,w,lambda1,lambda2);
    for (int iter=0; iter < maxNumIters; iter++) {
      if (fitIntercept) {
        intercept+=OptimUtils.fitIntercept(rTrain);
      }
      doOnePass(indices,values,sq,tl1,tl2,w,rTrain);
      double currLoss=GLMOptimUtils.computeElasticNetLoss(rTrain,w,lambda1,lambda2);
      if (verbose) {
        System.out.println("String_Node_Str" + iter + "String_Node_Str"+ currLoss);
      }
      if (prevLoss - currLoss < epsilon) {
        break;
      }
      prevLoss=currLoss;
    }
    double currLoss=GLMOptimUtils.computeElasticNetLoss(rTrain,w,lambda1,lambda2);
    System.out.println("String_Node_Str" + g + "String_Node_Str"+ lambda+ "String_Node_Str"+ currLoss+ "String_Node_Str"+ StatUtils.rms(rTrain));
    lambda*=alpha;
    glms[g]=GLMOptimUtils.getGLM(attrs,w,intercept);
  }
  return glms;
}

Fixed version (ground truth):
/** 
 * Builds elastic-net penalized regressors for a sequence of regularization parameter lambdas. Each row of the input represents a feature (instead of a data point), i.e., in column-oriented format. This procedure does not assume the data is normalized or centered.
 * @param attrs the attribute list.
 * @param indices the indices.
 * @param values the values.
 * @param y the targets.
 * @param maxNumIters the maximum number of iterations.
 * @param numLambdas the number of lambdas.
 * @param minLambdaRatio the minimum lambda is minLambdaRatio * max lambda.
 * @param l1Ratio the L1 ratio.
 * @return elastic-net penalized regressors.
 */
public GLM[] buildRegressors(int[] attrs,int[][] indices,double[][] values,double[] y,int maxNumIters,int numLambdas,double minLambdaRatio,double l1Ratio){
  double[] w=new double[attrs.length];
  double intercept=0;
  GLM[] glms=new GLM[numLambdas];
  double[] rTrain=new double[y.length];
  for (int i=0; i < rTrain.length; i++) {
    rTrain[i]=y[i];
  }
  double[] sq=new double[values.length];
  for (int i=0; i < values.length; i++) {
    sq[i]=StatUtils.sumSq(values[i]);
  }
  double maxLambda=findMaxLambda(indices,values,y,l1Ratio);
  double alpha=Math.pow(minLambdaRatio,1.0 / numLambdas);
  double lambda=maxLambda;
  for (int g=0; g < glms.length; g++) {
    final double lambda1=lambda * l1Ratio;
    final double lambda2=lambda * (1 - l1Ratio);
    final double tl1=lambda1 * y.length;
    final double tl2=lambda2 * y.length;
    double prevLoss=GLMOptimUtils.computeElasticNetLoss(rTrain,w,lambda1,lambda2);
    for (int iter=0; iter < maxNumIters; iter++) {
      if (fitIntercept) {
        intercept+=OptimUtils.fitIntercept(rTrain);
      }
      doOnePass(indices,values,sq,tl1,tl2,w,rTrain);
      double currLoss=GLMOptimUtils.computeElasticNetLoss(rTrain,w,lambda1,lambda2);
      if (verbose) {
        System.out.println("String_Node_Str" + iter + "String_Node_Str"+ currLoss);
      }
      if (OptimUtils.isConverged(prevLoss,currLoss,epsilon)) {
        break;
      }
      prevLoss=currLoss;
    }
    double currLoss=GLMOptimUtils.computeElasticNetLoss(rTrain,w,lambda1,lambda2);
    System.out.println("String_Node_Str" + g + "String_Node_Str"+ lambda+ "String_Node_Str"+ currLoss+ "String_Node_Str"+ StatUtils.rms(rTrain));
    lambda*=alpha;
    glms[g]=GLMOptimUtils.getGLM(attrs,w,intercept);
  }
  return glms;
}