// static ArrayList<Double> error_prob=new ArrayList<>() ;
  double CalculateThreshold(
      double[] xvalues,
      int[] yvalues,
      double[] probabilities,
      double[] goodnessweight,
      double[] threshold,
      int q,
      ArrayList<ArrayList<Double>> fvalues,
      double bound,
      char[] c) {

    ArrayList<Double> error_prob = new ArrayList<>();
    double ht_error = 0;
    double normalizationfac = 0;
    // char c;

    int arraysize = yvalues.length;
    double[] normalizedprobabilities = new double[arraysize];
    int[] correctness = new int[yvalues.length];

    int[] h1 = new int[arraysize];
    int aindex;
    int bindex;
    ArrayList<Integer> errors = new ArrayList<>();
    ArrayList<Integer> indexes = new ArrayList<>();
    double[] prenormalised_array = new double[arraysize];
    ArrayList<Double> fval = new ArrayList<>();

    ArrayList<ArrayList<Integer>> final_wrong = new ArrayList<ArrayList<Integer>>();
    int errorsize = 0;
    // Special Case when bindex=0
    bindex = 0;
    int count = 0;
    double zeroerror = 0;
    ArrayList<Integer> zerowrong = new ArrayList<>();
    for (int j = bindex; j < yvalues.length; j++) {
      if (yvalues[j] != yvalues[0]) {
        count++;
        zeroerror += probabilities[j];
        zerowrong.add(j);
        // System.out.println("b prob "+probabilities[j]);
      }
    }

    error_prob.add(zeroerror);
    indexes.add(0);
    final_wrong.add(zerowrong);

    for (int i = 1; i < yvalues.length; i++) {
      int acount = 0;
      int bcount = 0;
      int a = yvalues[i - 1];
      int b = yvalues[i];
      double error = 0;
      ArrayList<Integer> wrong = new ArrayList<>();
      if (a != b) {
        aindex = i - 1;
        bindex = i;
        //    System.out.println("Index of a "+(i-1));
        //   System.out.println("Index of b "+(i));
        for (int j = 0; j < aindex; j++) {
          if (yvalues[j] != a) {
            acount++;
            error += probabilities[j];
            wrong.add(j);
          }
        }
        for (int j = bindex; j < yvalues.length; j++) {
          if (yvalues[j] != b) {
            bcount++;
            error += probabilities[j];
            wrong.add(j);
          }
        }
        errors.add(acount + bcount);

        indexes.add(aindex);
        error_prob.add(error);
        //                for(int k=0;k<wrong.size();k++)
        //                {
        //                    System.out.println("Wrong values "+wrong.get(k));
        //                }
        final_wrong.add(wrong);
      }
    }
    double min = error_prob.get(0);
    int minindex = indexes.get(0);
    int valindex = 0;
    for (int k = 0; k < error_prob.size(); k++) {

      //  System.out.println("Error is "+error_prob.get(k));
      //  System.out.println("Index of a is "+indexes.get(k));
      if (error_prob.get(k) < min) {
        min = error_prob.get(k);
        minindex = indexes.get(k);
        valindex = k;
      }
    }
    int v = minindex;
    int w = minindex + 1;

    // Special Case: When valindex = 0
    /** ************************************************************************ */
    //        if(valindex==0)
    //        {
    //            double r=error_prob.get(0);
    //            double s=error_prob.get(1);
    //            System.out.println("Before 0 "+error_prob.get(0));
    //            System.out.println("Between 0 and 1 "+error_prob.get(1));
    //            if(r<s)
    //            {
    //                threshold[q]=0;
    //                c='>';
    //
    //            }
    //            else
    //            {
    //              threshold[q]=(xvalues[v]+xvalues[w])/2;
    //              if(yvalues[minindex]>0)
    //              {
    //                c='<';
    //              }
    //            else
    //            {
    //                c='>';
    //            }
    //            }
    //
    //        }
    //        else
    /** ****************************************************************** */
    {
      // System.out.println("Min error is "+min);
      // System.out.println("Min index is "+v);
      threshold[q] = (xvalues[v] + xvalues[w]) / 2;
      if (yvalues[minindex] > 0) {
        c[q] = '<';
      } else {
        c[q] = '>';
      }
    }

    System.out.println(
        "The selected weak classifier h_" + (q + 1) + ": I(x " + c[q] + " " + threshold[q] + ")");
    ht_error = error_prob.get(valindex);
    System.out.println("The error e of h_" + (q + 1) + ": " + ht_error);
    goodnessweight[q] = CalculateWeight(ht_error);
    System.out.println("The weight of h_" + (q + 1) + ": " + goodnessweight[q]);
    ArrayList<Integer> wrongarray = final_wrong.get(valindex);
    //        for(int i=0;i<wrongarray.size();i++)
    //        {
    //                System.out.println("Wrong indexes are "+wrongarray.get(i));
    //        }
    int g = 0;
    for (int h = 0; h < yvalues.length; h++) {
      correctness[h] = 1;
    }

    for (int i = 0; i < wrongarray.size(); i++) {
      correctness[wrongarray.get(i)] = 0;
    }
    //         for(int h=0;h<yvalues.length;h++)
    //        {
    //            System.out.print(correctness[h]+" ");
    //        }
    //        System.out.println(" ");
    CalculateQ(correctness, goodnessweight[q], arraysize, probabilities, prenormalised_array);
    for (int i = 0; i < arraysize; i++) {
      //   System.out.print(prenormalised_array[i]+" ");
      normalizationfac += prenormalised_array[i];
    }

    System.out.println("Normalization Factor Zt :" + normalizationfac);
    bound *= normalizationfac;

    CalculateNormalizedProb(prenormalised_array, normalizationfac, normalizedprobabilities);
    System.out.print("Normalized Probabilities :");
    for (int i = 0; i < arraysize; i++) {
      System.out.print(normalizedprobabilities[i] + " ");
    }
    System.out.println(" ");
    if (q == 0) {
      System.out.println(
          "The boosted classifier:f_"
              + (q + 1)
              + "(x)= "
              + goodnessweight[q]
              + " I(x "
              + c[q]
              + " "
              + threshold[q]
              + ")");
    } else {
      System.out.print("The boosted classifier:f_" + (q + 1) + "(x)= ");
      for (int p = 0; p <= q; p++) {
        if (p < q) {
          System.out.print(goodnessweight[p] + " I(x " + c[p] + " " + threshold[p] + ")+");
        } else {
          System.out.print(goodnessweight[p] + " I(x " + c[p] + " " + threshold[p] + ")");
        }
      }
      System.out.println(" ");
    }

    System.arraycopy(normalizedprobabilities, 0, probabilities, 0, probabilities.length);

    /** *****Special Case: Valindex=0********* */
    //        if(valindex==0)
    //        {
    //            v=0;
    //            w=0;
    //
    //        }
    /** *************************************** */
    int a1 = yvalues[v];
    int b1 = yvalues[w];
    for (int j = 0; j <= v; j++) {
      h1[j] = a1;
    }
    for (int j = w; j < yvalues.length; j++) {
      h1[j] = b1;
    }
    //
    // Temp F array multiplied with alpha
    for (int y = 0; y < h1.length; y++) {
      fval.add(h1[y] * goodnessweight[q]);
    }

    fvalues.add(fval);
    // System.out.println("Bound: "+errorbound);
    return bound;
  }
Пример #2
0
  /**
   * Normalizes the copy number signal, allele A signal and allele B signal chromosome by
   * chromosome.
   */
  public void localNormalization() {

    double chrMean = 0;
    double[] chrCopyNumber = null;

    String chr = chrID[0] + "";
    int chrStartPos = 0;
    for (int i = 0; i < copyNumber.length; i++) {

      if (!chr.equals(chrID[i])) {

        chrCopyNumber = new double[i - chrStartPos];
        System.arraycopy(copyNumber, chrStartPos, chrCopyNumber, 0, i - chrStartPos);
        chrMean = robustMu(chrCopyNumber);
        // Local normalization is done only when the difference is small, otherwise, it may result
        // in some artifial
        if (abs(2 - chrMean) < 0.05) {
          for (int j = chrStartPos; j < i; j++) {

            copyNumber[j] = copyNumber[j] / chrMean * 2;
          }
        }

        chrStartPos = i;
        chr = chrID[i] + "";
      }
    }

    chrCopyNumber = new double[copyNumber.length - chrStartPos];
    System.arraycopy(copyNumber, chrStartPos, chrCopyNumber, 0, copyNumber.length - chrStartPos);
    chrMean = robustMu(chrCopyNumber);

    if (abs(2 - chrMean) < 0.075) {
      for (int j = chrStartPos; j < copyNumber.length; j++) {

        copyNumber[j] = copyNumber[j] / chrMean * 2;
      }
    }

    // 	double chrMean = 0;
    // 	double[] chrCopyNumber = null;

    // 	String chr = chrID[0] + "";
    // 	int chrStartPos = 0;
    // 	int chrLength = 0;
    // 	for (int i = 0; i < copyNumber.length; i ++) {

    // 	    if(!chr.equals(chrID[i])) {

    // 		chrCopyNumber = new double[chrLength];
    // 		int k = 0;
    // 		for (int j = chrStartPos; j < i; j ++) {
    // 		    if (!isOutlier[j]) {
    // 			chrCopyNumber[k] = copyNumber[j];
    // 			k ++;
    // 		    }
    // 		}

    // 		chrMean = robustMu(chrCopyNumber);

    // 		if (abs(2 - chrMean) < 0.1) {
    // 		    for (int j = chrStartPos; j < i; j ++) {

    // 			copyNumber[j] = copyNumber[j] / chrMean * 2;

    // 		    }
    // 		}

    // 		chrStartPos = i;
    // 		chrLength = 0;
    // 		chr = chrID[i] + "";

    // 	    }

    // 	    if (!isOutlier[i]) {
    // 		chrLength ++;
    // 	    }

    // 	}

    // 	chrCopyNumber = new double[chrLength];
    // 	int k = 0;
    // 	for (int j = chrStartPos; j < copyNumber.length; j ++) {
    // 	    if (!isOutlier[j]) {
    // 		chrCopyNumber[k] = copyNumber[j];
    // 		k ++;
    // 	    }
    // 	}

    // 	chrMean = robustMu(chrCopyNumber);

    // 	if (abs(2 - chrMean) < 0.1) {
    // 	    for (int j = chrStartPos; j < copyNumber.length; j ++) {

    // 		copyNumber[j] = copyNumber[j] / chrMean * 2;

    // 	    }
    // 	}

  }