public double likelihood() {
    double c1 = 0.0;
    double c3 = -1.0 / (double) Math.sqrt(2.0 * variance);
    double sum = 0.0;

    double cpart = -0.5 * Math.log(Math.sqrt(2.0 * Math.PI * variance));

    for (int i = 0; i < data.size(); i++) {
      for (int k = 0; k < channels.length; k++) {
        c1 += cpart;
        double pi = 0.0;

        for (Integer t : transcripts.keySet()) {
          if (transcripts.get(t).contains(i)) {
            double gammai = gammas[t][k];
            double dit = delta(i, t);

            double pit = gammai * Math.exp(-lambda * dit);
            pi += pit;
          }
        }

        double zi = Math.log(pi);
        double err = data.values(i)[channels[k]] - zi;

        sum += (err * err);
      }
    }

    return c1 + c3 * sum;
  }
  public double error() {
    double sum = 0.0;

    for (int i = 0; i < data.size(); i++) {
      for (int k = 0; k < channels.length; k++) {
        double pi = 0.0;

        for (Integer t : transcripts.keySet()) {
          if (transcripts.get(t).contains(i)) {
            double gammai = gammas[t][k];
            double dit = delta(i, t);

            double pit = gammai * Math.exp(-lambda * dit);
            pi += pit;
          }
        }

        double zi = Math.log(pi);
        double err = Math.abs(data.values(i)[channels[k]] - zi);

        sum += err;
      }
    }

    return sum;
  }
  public ClusterLikelihoods(
      ClusterData d, Integer[] chs, int[] five, int[] three, double v, double l) {
    data = d;
    channels = chs.clone();

    gammas = new double[five.length][channels.length];
    for (int t = 0; t < gammas.length; t++) {
      for (int i = 0; i < gammas[t].length; i++) {
        gammas[t][i] = 1.0;
      }
    }

    transcripts = new TreeMap<Integer, Set<Integer>>();
    fiveprime = five;
    threeprime = three;

    if (fiveprime.length != threeprime.length) {
      String msg = String.format("%d != %d", fiveprime.length, threeprime.length);
      throw new IllegalArgumentException(msg);
    }

    variance = v;
    lambda = l;

    for (int t = 0; t < fiveprime.length; t++) {
      transcripts.put(t, new TreeSet<Integer>());
    }

    for (int i = 0; i < data.size(); i++) {
      int loc = data.location(i);
      for (int t = 0; t < fiveprime.length; t++) {
        if (fiveprime[t] <= loc && threeprime[t] >= loc) {
          transcripts.get(t).add(i);
        }
      }
    }
  }
 public double delta(int i, int t) {
   int diff = threeprime[t] - data.location(i);
   return diff >= 0 ? (double) diff : 0.0;
 }