/** * Discretize the given value * * @param value the value to discretize * @param min the min of the distribution * @param max the max of the distribution * @param binCount the number of bins * @return the discretized value */ public static int discretize(double value, double min, double max, int binCount) { int discreteValue = (int) (binCount * normalize(value, min, max)); return clamp(discreteValue, 0, binCount - 1); }
/** * This returns the sum of products for the given * numbers. * * @param nums the sum of products for the give numbers * @return the sum of products for the given numbers */ public static double sumOfProducts(double[]... nums) { if (nums == null || nums.length < 1) return 0; double sum = 0; for (int i = 0; i < nums.length; i++) { /* The ith column for all of the rows */ double[] column = column(i, nums); sum += times(column); } return sum; }//end sumOfProducts
/** * This returns the permutation of n choose r. * * @param n the n to choose * @param r the number of elements to choose * @return the permutation of these numbers */ public static double permutation(double n, double r) { double nFac = MathUtils.factorial(n); double nMinusRFac = MathUtils.factorial((n - r)); return nFac / nMinusRFac; }//end permutation
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(double[] vector) { /* split coordinate system */ List<double[]> coords = coordSplit(vector); /* x vals */ double[] x = coords.get(0); /* y vals */ double[] y = coords.get(1); double meanX = sum(x) / x.length; double meanY = sum(y) / y.length; double sumOfMeanDifferences = sumOfMeanDifferences(x, y); double xDifferenceOfMean = sumOfMeanDifferencesOnePoint(x); double w_1 = sumOfMeanDifferences / xDifferenceOfMean; double w_0 = meanY - (w_1) * meanX; double[] ret = new double[vector.length]; ret[0] = w_0; ret[1] = w_1; return ret; }//end weightsFor
/** * Returns the log-odds for a given probability. * * @param prob the probability * @return the log-odds after the probability has been mapped to * [Utils.SMALL, 1-Utils.SMALL] */ public static /*@pure@*/ double probToLogOdds(double prob) { if (gr(prob, 1) || (sm(prob, 0))) { throw new IllegalArgumentException("probToLogOdds: probability must " + "be in [0,1] " + prob); } double p = SMALL + (1.0 - 2 * SMALL) * prob; return Math.log(p / (1 - p)); }
/** * Converts an array containing the natural logarithms of * probabilities stored in a vector back into probabilities. * The probabilities are assumed to sum to one. * * @param a an array holding the natural logarithms of the probabilities * @return the converted array */ public static double[] logs2probs(double[] a) { double max = a[maxIndex(a)]; double sum = 0.0; double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = Math.exp(a[i] - max); sum += result[i]; } normalize(result, sum); return result; }//end logs2probs
/** * This returns the determination coefficient of two vectors given a length * * @param y1 the first vector * @param y2 the second vector * @param n the length of both vectors * @return the determination coefficient or r^2 */ public static double determinationCoefficient(double[] y1, double[] y2, int n) { return Math.pow(correlation(y1, y2), 2); }
/** * This returns the entropy for a given vector of probabilities. * @param probabilities the probabilities to getFromOrigin the entropy for * @return the entropy of the given probabilities. */ public static double information(double[] probabilities) { double total = 0.0; for (double d : probabilities) { total += (-1.0 * log2(d) * d); } return total; }//end information
/** * This will return the bernoulli trial for the given event. * A bernoulli trial is a mechanism for detecting the probability * of a given event occurring k times in n independent trials * * @param n the number of trials * @param k the number of times the target event occurs * @param successProb the probability of the event happening * @return the probability of the given event occurring k times. */ public static double bernoullis(double n, double k, double successProb) { double combo = MathUtils.combination(n, k); double q = 1 - successProb; return combo * Math.pow(successProb, k) * Math.pow(q, n - k); }//end bernoullis
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(double[] vector) { /* split coordinate system */ List<double[]> coords=coordSplit(vector); /* x vals */ double[] x=coords.get(0); /* y vals */ double[] y=coords.get(1); double meanX=sum(x)/x.length; double meanY=sum(y)/y.length; double sumOfMeanDifferences=sumOfMeanDifferences(x,y); double xDifferenceOfMean=sumOfMeanDifferencesOnePoint(x); double w_1=sumOfMeanDifferences/xDifferenceOfMean; double w_0=meanY - (w_1) * meanX; double[] ret = new double[vector.length]; ret[0]=w_0; ret[1]=w_1; return ret; }//end weightsFor
/** * Returns the log-odds for a given probability. * * @param prob the probability * * @return the log-odds after the probability has been mapped to * [Utils.SMALL, 1-Utils.SMALL] */ public static /*@pure@*/ double probToLogOdds(double prob) { if (gr(prob, 1) || (sm(prob, 0))) { throw new IllegalArgumentException("probToLogOdds: probability must " + "be in [0,1] "+prob); } double p = SMALL + (1.0 - 2 * SMALL) * prob; return Math.log(p / (1 - p)); }
/** * Converts an array containing the natural logarithms of * probabilities stored in a vector back into probabilities. * The probabilities are assumed to sum to one. * * @param a an array holding the natural logarithms of the probabilities * @return the converted array */ public static double[] logs2probs(double[] a) { double max = a[maxIndex(a)]; double sum = 0.0; double[] result = new double[a.length]; for(int i = 0; i < a.length; i++) { result[i] = Math.exp(a[i] - max); sum += result[i]; } normalize(result, sum); return result; }//end logs2probs /**
/** * This returns the determination coefficient of two vectors given a length * @param y1 the first vector * @param y2 the second vector * @param n the length of both vectors * @return the determination coefficient or r^2 */ public static double determinationCoefficient(double[] y1, double[] y2, int n) { return Math.pow(correlation(y1,y2),2); }
/** * This returns the entropy for a given vector of probabilities. * * @param probabilities the probabilities to getFromOrigin the entropy for * @return the entropy of the given probabilities. */ public static double information(double[] probabilities) { double total = 0.0; for (double d : probabilities) { total += (-1.0 * log2(d) * d); } return total; }//end information
/** * This will return the bernoulli trial for the given event. * A bernoulli trial is a mechanism for detecting the probability * of a given event occurring k times in n independent trials * @param n the number of trials * @param k the number of times the target event occurs * @param successProb the probability of the event happening * @return the probability of the given event occurring k times. */ public static double bernoullis(double n,double k,double successProb) { double combo = MathUtils.combination(n, k); double p = successProb; double q= 1 - successProb; return combo * Math.pow(p,k) * Math.pow(q,n-k); }//end bernoullis /**
/** * This returns the minimized loss values for a given vector. * It is assumed that the x, y pairs are at * vector[i], vector[i+1] * * @param vector the vector of numbers to getFromOrigin the weights for * @return a double array with w_0 and w_1 are the associated indices. */ public static double[] weightsFor(List<Double> vector) { /* split coordinate system */ List<double[]> coords = coordSplit(vector); /* x vals */ double[] x = coords.get(0); /* y vals */ double[] y = coords.get(1); double meanX = sum(x) / x.length; double meanY = sum(y) / y.length; double sumOfMeanDifferences = sumOfMeanDifferences(x, y); double xDifferenceOfMean = sumOfMeanDifferencesOnePoint(x); double w_1 = sumOfMeanDifferences / xDifferenceOfMean; double w_0 = meanY - (w_1) * meanX; //double w_1=(n*sumOfProducts(x,y) - sum(x) * sum(y))/(n*sumOfSquares(x) - Math.pow(sum(x),2)); // double w_0=(sum(y) - (w_1 * sum(x)))/n; double[] ret = new double[vector.size()]; ret[0] = w_0; ret[1] = w_1; return ret; }//end weightsFor
/** * Discretize the given value * @param value the value to discretize * @param min the min of the distribution * @param max the max of the distribution * @param binCount the number of bins * @return the discretized value */ public static int discretize(double value, double min, double max, int binCount) { int discreteValue = (int) (binCount * normalize(value, min, max)); return clamp(discreteValue, 0, binCount - 1); }
/** * This returns the sum of products for the given * numbers. * @param nums the sum of products for the give numbers * @return the sum of products for the given numbers */ public static double sumOfProducts(double[]... nums) { if(nums==null || nums.length < 1) return 0; double sum=0; for(int i=0;i<nums.length;i++) { /* The ith column for all of the rows */ double[] column=column(i,nums); sum+=times(column); } return sum; }//end sumOfProducts