private double doItemSimilarity(long itemID1, long itemID2, FastIDSet preferring1)
      throws TasteException {
    double intersection = 0.0;
    double union = 0.0;

    for (Preference pref : getDataModel().getPreferencesForItem(itemID2)) {
      long userID = pref.getUserID();
      double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID);
      if (preferring1.contains(userID)) {
        intersection += weight;
        union -= weight;
      }
      union += weight;
    }
    for (LongPrimitiveIterator it_user = preferring1.iterator(); it_user.hasNext(); ) {
      long userID = (long) it_user.nextLong();
      double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID);
      union += weight;
    }

    if (intersection == 0) {
      return Double.NaN;
    }

    return Math.log(intersection) / Math.log(union);
  }
 private FastIDSet toUserFastIDSet(PreferenceArray array) {
   FastIDSet fastIDSet = new FastIDSet();
   for (Preference preference : array) {
     fastIDSet.add(preference.getUserID());
   }
   return fastIDSet;
 }
 @Override
 public Void call() throws TasteException {
   for (Preference realPref : prefs) {
     float estimatedPreference = Float.NaN;
     try {
       estimatedPreference = recommender.estimatePreference(testUserID, realPref.getItemID());
     } catch (NoSuchUserException nsue) {
       // It's possible that an item exists in the test data but
       // not training data in which case
       // NSEE will be thrown. Just ignore it and move on.
       log.info("User exists in test data but not training data: {}", testUserID);
     } catch (NoSuchItemException nsie) {
       log.info("Item exists in test data but not training data: {}", realPref.getItemID());
     }
     if (Float.isNaN(estimatedPreference)) {
       noEstimateCounter.incrementAndGet();
     } else {
       estimatedPreference = capEstimatedPreference(estimatedPreference);
       processOneEstimate(estimatedPreference, realPref);
     }
   }
   return null;
 }
  private void splitOneUsersPrefs(
      double trainingPercentage,
      FastByIDMap<PreferenceArray> trainingPrefs,
      FastByIDMap<PreferenceArray> testPrefs,
      long userID,
      DataModel dataModel)
      throws TasteException {
    List<Preference> oneUserTrainingPrefs = null;
    List<Preference> oneUserTestPrefs = null;
    PreferenceArray prefs = dataModel.getPreferencesFromUser(userID);
    int size = prefs.length();
    boolean isInstanceOfContextualUserPreferenceArray =
        prefs instanceof ContextualUserPreferenceArray;

    for (int i = 0; i < size; i++) {
      Preference newPref =
          isInstanceOfContextualUserPreferenceArray
              ? new ContextualPreference(
                  userID,
                  prefs.getItemID(i),
                  prefs.getValue(i),
                  ((ContextualUserPreferenceArray) prefs).getContextualPreferences(i))
              : new GenericPreference(userID, prefs.getItemID(i), prefs.getValue(i));

      if (this.idrescorer != null && this.idrescorer.isFiltered(newPref.getItemID())) { // adiciona
        // ratings
        // de
        // source
        // domain
        // sempre
        // em
        // training
        // set
        if (oneUserTrainingPrefs == null) {
          oneUserTrainingPrefs = Lists.newArrayListWithCapacity(3);
        }
        oneUserTrainingPrefs.add(newPref);
        totalOfTrainingRatingsFromSource++;
        continue;
      }

      if (this.contextualCriteria != null
          && isInstanceOfContextualUserPreferenceArray) { // adiciona
        // ratings
        // de outro
        // contexto
        // sempre em
        // training
        // set
        ContextualPreference contextualPref = (ContextualPreference) newPref;
        if (!this.contextualCriteria.containsAllContextualAttributes(
            contextualPref.getContextualPreferences())) {
          if (oneUserTrainingPrefs == null) {
            oneUserTrainingPrefs = Lists.newArrayListWithCapacity(3);
          }
          oneUserTrainingPrefs.add(newPref);
          totalOfTrainingRatingsFromTargetWithoutContext++;
          continue;
        }
      }

      // para ratings do target e do contexto, fazer proporcao definida
      if (random.nextDouble() < trainingPercentage) {
        if (oneUserTrainingPrefs == null) {
          oneUserTrainingPrefs = Lists.newArrayListWithCapacity(3);
        }
        oneUserTrainingPrefs.add(newPref);
        totalOfTrainingRatingsFromTargetWithContext++;
      } else {
        if (oneUserTestPrefs == null) {
          oneUserTestPrefs = Lists.newArrayListWithCapacity(3);
        }
        oneUserTestPrefs.add(newPref);
        totalOfTestRatings++;
      }

      // OLD training/test set
      /*
       * if (random.nextDouble() < trainingPercentage) { if
       * (oneUserTrainingPrefs == null) { oneUserTrainingPrefs =
       * Lists.newArrayListWithCapacity(3); }
       * oneUserTrainingPrefs.add(newPref); totalOfTrainingRatings++; }
       * else { if (oneUserTestPrefs == null) { oneUserTestPrefs =
       * Lists.newArrayListWithCapacity(3); } //testa somente com um tipo
       * de rating (rescorer e/ou context) if(this.idrescorer == null &&
       * this.contextualCriteria == null){ oneUserTestPrefs.add(newPref);
       * totalOfTestRatings++; }else{ if(this.idrescorer != null &&
       * !this.idrescorer.isFiltered(newPref.getItemID())){
       * if(this.contextualCriteria != null &&
       * isInstanceOfContextualUserPreferenceArray){ ContextualPreference
       * contextualPref = (ContextualPreference) newPref;
       * if(this.contextualCriteria
       * .containsAllContextualAttributes(contextualPref
       * .getContextualPreferences())){ oneUserTestPrefs.add(newPref);
       * totalOfTestRatings++; } }else{ oneUserTestPrefs.add(newPref);
       * totalOfTestRatings++; } }else if(this.idrescorer == null &&
       * this.contextualCriteria != null &&
       * isInstanceOfContextualUserPreferenceArray){ ContextualPreference
       * contextualPref = (ContextualPreference) newPref;
       * if(this.contextualCriteria
       * .containsAllContextualAttributes(contextualPref
       * .getContextualPreferences())){ oneUserTestPrefs.add(newPref);
       * totalOfTestRatings++; } } } }
       */
    }
    if (oneUserTrainingPrefs != null) {
      trainingPrefs.put(
          userID,
          isInstanceOfContextualUserPreferenceArray
              ? new ContextualUserPreferenceArray(oneUserTrainingPrefs)
              : new GenericUserPreferenceArray(oneUserTrainingPrefs));
      if (oneUserTestPrefs != null) {
        testPrefs.put(
            userID,
            isInstanceOfContextualUserPreferenceArray
                ? new ContextualUserPreferenceArray(oneUserTestPrefs)
                : new GenericUserPreferenceArray(oneUserTestPrefs));
      }
    }
  }