Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Gets the participation ratio of the given items against the global counts.
- *
- * Each List is a set of points, which is assumed to be the end point of the algorithm (despite whether the algorithm
- * has run to completion). These points are then checked for types against the global count of types. If a given item contains
- * all points of a given type, then its Correlation reduces to Correlation.MAX_VALUE, or 1/1. Otherwise,
- * it is the ratio to points of a given type in the item, over the global counts. If a given List<Type> is
- * duplicated in the input values, the output values will have one entry with the values summed.
- *
- * @param items The items, as a result of the closure
- * @param globalCounts The global counts - all types, associated to the number of points in each
- * @return The list of types in the input, associated with the correlation of included versus results
- */
- Map<List<Type>, Correlation> getParticipationRatio(final Iterable<? extends List<Point>> items, final Map<Point.Type, Integer> globalCounts) {
- /*
- 1) Make sure all local counts only have unique points - that's what we got coming in, that's what we shoudl
- compare against going out.
- 3) Check local counts against global counts
- */
- //Step 1 - Make sure all points are unique
- final Set<Point> uniquePoints = Sets.newHashSet();
- List<Set<Point>> uniqueItems = Lists.newArrayList();
- for(List<Point> ps : items) {
- Set<Point> resultSet = Sets.filter(Sets.newHashSet(ps), new Predicate<Point>() { //we filter out the repeat items while maintaining structure
- @Override
- public boolean apply(final Point input) {
- return uniquePoints.add(input);
- }
- });
- uniqueItems.add(resultSet);
- }
- //Step 2 - perform our result-finding
- Map<List<Type>, Correlation> result = Maps.newHashMap();
- for(Collection<Point> ps : uniqueItems) {
- List<Point> pli = Lists.newArrayList(ps);
- List<Type> types = getTypes(pli);
- int global = 0; //determine the global counts
- for(Type t : types) {
- Integer i = globalCounts.get(t); //an unregistered type in the global counts *should* be an error condition
- if(i == null) {
- throw new ConstraintViolationException("Global counts does not have a registered value for " + t);
- }
- global += i;
- }
- final Correlation correlation = new Correlation(ps.size(), global); //current counts over total counts
- Correlation oldCorrelation = result.get(types); //if we've already done this, we need to add the fractions
- if(oldCorrelation == null) {
- oldCorrelation = Correlation.MIN_VALUE;
- }
- result.put(types, oldCorrelation.add(correlation)); //we add the fractions here
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement