/** * Returns {@code true} if the two integer vectors are equal to one another. Two {@code Vector} * insances are considered equal if they contain the same number of elements and all corresponding * pairs of {@code int} values are equal. */ public static boolean equals(IntegerVector v1, IntegerVector v2) { if (v1.length() == v2.length()) { int length = v1.length(); for (int i = 0; i < length; ++i) { if (v1.get(i) != v2.get(i)) return false; } return true; } return false; }
@Test public void testRange2() { // create a combinatorics vector () IntegerVector vector = IntegerFactory.range(0); System.out.println(vector); assertEquals(0, vector.getSize()); assertEquals(false, vector.hasDuplicates()); }
/** * Return a list of indices representing the next values that would be selected using the given * probabilities */ public IntegerVector getNextIndicesDeterministicallyUsingProbs(DoubleVector probs) { // Check the number of indices is equal to number of probabilities if (size() != probs.size()) { throw new ErrorException( "Number of probabilities (" + probs.size() + ") does not match the receiver (" + size() + ")"); } // Calculate the total number of samples for this selection (One more than the sum of the counts // so far) double n = 0; double cumProb = 0.0; for (int i = 0; i < size(); i++) { n += get(i); cumProb += probs.get(i); } double x = n + 1; if (Math.abs(cumProb - 1.0) > 0.001) { throw new ErrorException("Probabilities do not sum to 1.000"); } // Loop through indices IntegerVector sortedIndices = new IntegerVector(); DoubleVector sortedDiffs = new DoubleVector(); for (int i = 0; i < probs.size(); i++) { // Calculate the expected number of events for this probability double expectedNumber = x * probs.get(i); // Select the index with the largest difference between the current // number and the expected number of events double difference = expectedNumber - get(i); int destIndex = 0; for (int j = 0; j < sortedDiffs.size(); j++) { if (sortedDiffs.get(j) < difference) { break; } destIndex++; } sortedDiffs.add(destIndex, difference); sortedIndices.add(destIndex, i); } return sortedIndices; }
@Test public void testRange1() { // create a combinatorics vector (1, 2, 3) IntegerVector vector = IntegerFactory.range(3); System.out.println(vector); assertEquals(3, vector.getSize()); assertEquals(false, vector.hasDuplicates()); assertEquals(1, vector.getValue(0)); assertEquals(2, vector.getValue(1)); assertEquals(3, vector.getValue(2)); }
/** * Creates a copy of a given {@code IntegerVector} with the same type as the original. * * @param source The {@code Vector} to copy. * @return A copy of {@code source} with the same type. */ public static IntegerVector copyOf(IntegerVector source) { IntegerVector result = null; if (source instanceof TernaryVector) { TernaryVector v = (TernaryVector) source; int[] pos = v.positiveDimensions(); int[] neg = v.negativeDimensions(); result = new TernaryVector( source.length(), Arrays.copyOf(pos, pos.length), Arrays.copyOf(neg, neg.length)); } else if (source instanceof SparseVector) { result = new SparseHashIntegerVector(source.length()); copyFromSparseVector(result, source); } else { result = new DenseIntVector(source.length()); for (int i = 0; i < source.length(); ++i) result.set(i, source.get(i)); } return result; }
/** * Select indices deterministically from the given probability distribution. The float buffer * receiver of this method is the number of times that each index has been fully selected. The * given split determines how many distinct indices will be selected, and by how much they will be * incremented (the total increments add up to one). The same index can be selected twice. */ public IntegerVector selectIndicesDeterministicallyUsingProbs_SplitAllowDuplicateIndex( DoubleVector probs, DoubleVector split) { double n; double maxDifference; double cumProb; double expectedNumber; double difference; int selectedIndex; IntegerVector selectedIndices; // Check that the number of indices is equal to the number of probabilities if (!(this.size() == probs.size())) { throw new ErrorException("Number of probabilities does not match the number the receiver"); } // Check that there is a valid split if (split.size() == 0) { throw new ErrorException("No split given"); } /*else { if( Math.abs( (split.sum() - 1.0) ) > 0.001 ) { throw new ErrorException( "Split does not sum to 1.0" ); } }*/ selectedIndices = new IntegerVector(1, 1); // Calculate the total number of samples for this selection so far n = 0.0; cumProb = 0.0; for (int j = 1; j <= this.size(); j++) { n = (n + this.get(j - 1)); cumProb = (cumProb + probs.get(j - 1)); } if (Math.abs((cumProb - 1.0)) > 0.001) { throw new ErrorException("Probabilities do not sum to 1.000"); } for (int i = 1; i <= split.size(); i++) { n = (n + split.get(i - 1)); // Loop through the indices maxDifference = -100000.0; selectedIndex = 0; for (int j = 1; j <= probs.size(); j++) { if (probs.get(j - 1) > 0.0) { // Calculate the expected number of events for this // probability expectedNumber = (n * probs.get(j - 1)); // Select the index with the largest difference between // the current number and the expected number of events difference = (expectedNumber - this.get(j - 1)); if (!(maxDifference + 1.0E-10 >= difference)) { maxDifference = difference; selectedIndex = j; } } } if (selectedIndex == 0) { throw new ErrorException("Error in method"); } // Increment the count for this index this.addAt(split.get(i - 1), selectedIndex - 1); selectedIndices.add(selectedIndex); } return selectedIndices; }
/** * Select an index deterministically from the given probability distribution. The double buffer * receiver of this method is the content of each index has been selected. Ignore is a list of all * indices that cannot be selected. */ public int selectIndexDeterministicallyUsingProbsIgnore( DoubleVector probs, IntegerVector ignore) { double n; double x; double maxDifference; double cumProb; double expectedNumber; double difference; int selectedIndex; // Check the number of indices is equal to number of probabilities if (size() != probs.size()) { throw new ErrorException( "Number of probabilities (" + probs.size() + ") does not match the receiver (" + size() + ")"); } // Calculate the total number of samples for this selection // (One more than the sum of the counts so far) n = 0; cumProb = 0.0; for (int i = 0; i < size(); i++) { n += get(i); cumProb += probs.get(i); } x = n + 1; if (Math.abs(cumProb - 1.0) > 0.001) { throw new ErrorException("Probabilities do not sum to 1.000"); } // Loop through indices maxDifference = -100000.0; selectedIndex = -1; for (int i = 0; i < probs.size(); i++) { if (ignore.contains(i) || probs.get(i) == 0.0) continue; // Calculate the expected number of events for this probability expectedNumber = x * probs.get(i); // Select the index with the largest difference between the current // number and the expected number of events difference = expectedNumber - get(i); if (!(maxDifference + 1.0E-10 >= difference)) { maxDifference = difference; selectedIndex = i; } } if (selectedIndex < 0) { throw new ErrorException("Error in Method"); } return selectedIndex; }
/** * Copies values from a {@code SparseVector} into another vector * * @param destination The {@code Vector to copy values into. * @param source The {@code @SparseVector} to copy values from. */ private static void copyFromSparseVector(IntegerVector destination, IntegerVector source) { int[] nonZeroIndices = ((SparseVector) source).getNonZeroIndices(); for (int index : nonZeroIndices) destination.set(index, source.get(index)); }