Calculate the F beta value from counts

@param beta Beta of value to use
@param tp   True positive count
@param fp   False positive count
@param fn   False negative count
@return F beta
970104
public static double fBeta(double beta, long tp, long fp, long fn) {
        double prec = tp / ((double) tp + fp);
        double recall = tp / ((double) tp + fn);
        return fBeta(beta, prec, recall);
    }
931844
def _evaluate(self,x,y):
        '''
        
        '''
        if _isscalar(x):
            x_pos = max(min(self.xSearchFunc(self.x_list,x),self.x_n-1),1)
            y_pos = max(min(self.ySearchFunc(self.y_list,y),self.y_n-1),1)
        else:
            x_pos = self.xSearchFunc(self.x_list,x)
            x_pos[x_pos < 1] = 1
            x_pos[x_pos > self.x_n-1] = self.x_n-1
            y_pos = self.ySearchFunc(self.y_list,y)
            y_pos[y_pos < 1] = 1
            y_pos[y_pos > self.y_n-1] = self.y_n-1
        alpha = (x - self.x_list[x_pos-1])/(self.x_list[x_pos] - self.x_list[x_pos-1])
        beta = (y - self.y_list[y_pos-1])/(self.y_list[y_pos] - self.y_list[y_pos-1])
        f = (
             (1-alpha)*(1-beta)*self.f_values[x_pos-1,y_pos-1]
          +  (1-alpha)*beta*self.f_values[x_pos-1,y_pos]
          +  alpha*(1-beta)*self.f_values[x_pos,y_pos-1]
          +  alpha*beta*self.f_values[x_pos,y_pos])
        return f
448991
def log_(t, alpha=1, beta=1):
    """"""
    return beta * np.log(alpha * np.maximum(0, t - 10 * beta) + 1)
156394
public static double logpdf(double x, double mu, double beta) {
    if(x == Double.NEGATIVE_INFINITY) {
      return Double.NEGATIVE_INFINITY;
    }
    final double z = (x - mu) / beta;
    return -z - FastMath.exp(-z) - FastMath.log(beta);
  }
395812
def tpr(y, z):
    
    tp, tn, fp, fn = contingency_table(y, z)
    return tp / (tp + fn)
1221249
public static FMeasureCounts fromTPFPFN(final double truePositives,
      final double falsePositives, final double falseNegatives) {
    return fromFPFNKeyCountSysCount(falsePositives, falseNegatives,
        truePositives + falseNegatives, truePositives + falsePositives);
  }
1285939
def F_beta(self, beta):
        
        try:
            F_dict = {}
            for i in self.TP.keys():
                F_dict[i] = F_calc(
                    TP=self.TP[i],
                    FP=self.FP[i],
                    FN=self.FN[i],
                    beta=beta)
            return F_dict
        except Exception:
            return {}
247877
public static function precision(int $tp, int $fp) : float
    {
        return $tp / (($tp + $fp) ?: EPSILON);
    }
890674
public double falsePositiveRate() {
    double fpr = 0;
    for(int i = 0; i < confusion.length; i++) {
      fpr += falsePositiveRate(i) * colSum(i);
    }
    return fpr / totalInstances();
  }
1191340
public static double fCdf(double x, int f1, int f2) {
        if(x<0 || f1<=0 || f2<=0) {
            throw new IllegalArgumentException("All the parameters must be positive.");
        }
        
        double Z = x/(x + (double)f2/f1);
        double FCdf = betaCdf(Z,f1/2.0,f2/2.0);
        
        return FCdf;
    }
1358876
def f1(y_true, y_pred, round=True):
    
    y_true, y_pred = _mask_value_nan(y_true, y_pred)
    if round:
        y_true = np.round(y_true)
        y_pred = np.round(y_pred)
    return skm.f1_score(y_true, y_pred)
1372680
private static function _beta($p, $q) {
		if ($p <= 0.0 || $q <= 0.0 || ($p + $q) > LOG_GAMMA_X_MAX_VALUE) {
			return 0.0;
		} else {
			return exp(self::_logBeta($p, $q));
		}
	}
434155
public static void insideAlgorithmFirstOrderExpect(final Hypergraph graph, final HyperpotentialFoe w,
            final Algebra s, final Scores scores) {
        final int n = graph.getNodes().size();
        final double[] beta = new double[n];
        final double[] betaFoe = new double[n];
        // \beta_i = 0 \forall i
        Arrays.fill(beta, s.zero());
        Arrays.fill(betaFoe, s.zero());
        graph.applyTopoSort(new HyperedgeFn() {

            @Override
            public void apply(Hyperedge e) {
                // \beta_{H(e)} += w_e \prod_{j \in T(e)} \beta_j
                double prod = s.one();
                double prodFoe = s.zero();
                for (Hypernode jNode : e.getTailNodes()) {
                    int j = jNode.getId();
                    // prod = s.times(prod, beta[j]);
                    // prodFoe = s.plus(s.times(prod, betaFoe[j]), s.times(beta[j], prodFoe));
                    double p1 = prod;
                    double p2 = beta[j];
                    double r1 = prodFoe;
                    double r2 = betaFoe[j];
                    prod = s.times(p1, p2);
                    prodFoe = s.plus(s.times(p1, r2), s.times(p2, r1));
                }
                // prod = s.times(prod, w.getScore(e, s));
                // prodFoe = s.plus(s.times(prod, w.getScoreFoe(e, s)), s.times(w.getScore(e, s), prodFoe));
                double p1 = prod;
                double p2 = w.getScore(e, s);
                double r1 = prodFoe;
                double r2 = w.getScoreFoe(e, s);
                prod = s.times(p1, p2);
                prodFoe = s.plus(s.times(p1, r2), s.times(p2, r1));
                
                int i = e.getHeadNode().getId();
                //if (log.isTraceEnabled()) { log.trace(String.format("old beta[%d] = %f",  i, s.toReal(beta[i]))); }
                beta[i] = s.plus(beta[i], prod);
                betaFoe[i] = s.plus(betaFoe[i], prodFoe);
                assert !s.isNaN(beta[i]);
                assert !s.isNaN(betaFoe[i]);
                // log.debug(String.format("%s w_e=%f beta[%d] = %.3f betaFoe[%d] = %.3f", e.getLabel(), s.toReal(w.getScore(e, s)), i, s.toReal(beta[i]), i, s.toReal(betaFoe[i]))); 
            }
            
        });
        scores.beta = beta;
        scores.betaFoe = betaFoe;
    }
1104536
protected static double rawQuantile(double p, double alpha, double beta, final double logbeta) {
    // Initial estimate for x
    double x;
    {
      // Very fast approximation of y.
      double tmp = FastMath.sqrt(-2 * FastMath.log(p));
      double y = tmp - (2.30753 + 0.27061 * tmp) / (1. + (0.99229 + 0.04481 * tmp) * tmp);

      if(alpha > 1 && beta > 1) {
        double r = (y * y - 3.) / 6.;
        double s = 1. / (alpha + alpha - 1.);
        double t = 1. / (beta + beta - 1.);
        double h = 2. / (s + t);
        double w = y * FastMath.sqrt(h + r) / h - (t - s) * (r + 5. / 6. - 2. / (3. * h));
        x = alpha / (alpha + beta * FastMath.exp(w + w));
      }
      else {
        double r = beta + beta;
        double t = 1. / (9. * beta);
        final double a = 1. - t + y * FastMath.sqrt(t);
        t = r * a * a * a;
        if(t <= 0.) {
          x = 1. - FastMath.exp((FastMath.log1p(-p) + FastMath.log(beta) + logbeta) / beta);
        }
        else {
          t = (4. * alpha + r - 2.) / t;
          if(t <= 1.) {
            x = FastMath.exp((FastMath.log(p * alpha) + logbeta) / alpha);
          }
          else {
            x = 1. - 2. / (t + 1.);
          }
        }
      }
      // Degenerate initial approximations
      if(x < 3e-308 || x > 1 - 2.22e-16) {
        x = 0.5;
      }
    }

    // Newon-Raphson method using the CDF
    {
      final double ialpha = 1 - alpha;
      final double ibeta = 1 - beta;

      // Desired accuracy, as from GNU R adoption of AS 109
      final double acu = Math.max(1e-300, FastMath.pow(10., -13 - 2.5 / (alpha * alpha) - .5 / (p * p)));
      double prevstep = 0., y = 0., stepsize = 1;

      for(int outer = 0; outer < 1000; outer++) {
        // Current CDF value
        double ynew = cdf(x, alpha, beta);
        if(Double.isInfinite(ynew)) { // Degenerated.
          return Double.NaN;
        }
        // Error gradient
        ynew = (ynew - p) * FastMath.exp(logbeta + ialpha * FastMath.log(x) + ibeta * FastMath.log1p(-x));
        if(ynew * y <= 0.) {
          prevstep = Math.max(Math.abs(stepsize), 3e-308);
        }
        // Inner loop: try different step sizes: y * 3^-i
        double g = 1, xnew = 0.;
        for(int inner = 0; inner < 1000; inner++) {
          stepsize = g * ynew;
          if(Math.abs(stepsize) < prevstep) {
            xnew = x - stepsize; // Candidate x
            if(xnew >= 0. && xnew <= 1.) {
              // Close enough
              if(prevstep <= acu || Math.abs(ynew) <= acu) {
                return x;
              }
              if(xnew != 0. && xnew != 1.) {
                break;
              }
            }
          }
          g /= 3.;
        }
        // Convergence
        if(Math.abs(xnew - x) < 1e-15 * x) {
          return x;
        }
        // Iterate with new values
        x = xnew;
        y = ynew;
      }
    }
    // Not converged in Newton-Raphson
    throw new ArithmeticException("Beta quantile computation did not converge.");
  }
332469
public static double beta(double x, double y) {
        return Math.exp(Gamma.lgamma(x) + Gamma.lgamma(y) - Gamma.lgamma(x + y));
    }
184686
public double getCount(A first, B second) {
    if (counts.containsKey(first) && counts.get(first).containsKey(second)) {
      return counts.get(first).get(second);
    }
    return 0.0;
  }
1208483
private void backward(TrellisNode[][] trellis) {
        int n = trellis.length - 1;
        TrellisNode[] tn = trellis[n];
        for (int i = 0; i < numClasses; i++) {
            tn[i].beta = 1.0;
        }

        for (int t = n; t-- > 0;) {
            TrellisNode[] tt = trellis[t];
            TrellisNode[] tt1 = trellis[t+1];
            for (int i = 0; i < numClasses; i++) {
                TrellisNode tti = tt[i];
                tti.beta = 0.0;
                for (int j = 0; j < numClasses; j++) {
                    tti.beta += tt1[j].expScores[i] * tt1[j].beta;
                }
            }

            // Normalize beta values since they increase quickly
            double sum = 0.0;
            for (int i = 0; i < numClasses; i++) {
                sum += tt[i].beta;
            }
            for (int i = 0; i < numClasses; i++) {
                tt[i].beta /= sum;
            }
        }
    }