public double UserSimilarity(long userID1, long userID2)
        {
            IDataModel dataModel = getDataModel();
            FastIDSet  xPrefs    = dataModel.GetItemIDsFromUser(userID1);
            FastIDSet  yPrefs    = dataModel.GetItemIDsFromUser(userID2);

            int xPrefsSize = xPrefs.Count();
            int yPrefsSize = yPrefs.Count();

            if (xPrefsSize == 0 && yPrefsSize == 0)
            {
                return(Double.NaN);
            }
            if (xPrefsSize == 0 || yPrefsSize == 0)
            {
                return(0.0);
            }

            int intersectionSize =
                xPrefsSize < yPrefsSize?yPrefs.IntersectionSize(xPrefs) : xPrefs.IntersectionSize(yPrefs);

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

            int unionSize = xPrefsSize + yPrefsSize - intersectionSize;

            return((double)intersectionSize / (double)unionSize);
        }
        public double UserSimilarity(long userID1, long userID2)
        {
            IDataModel dataModel        = getDataModel();
            FastIDSet  prefs1           = dataModel.GetItemIDsFromUser(userID1);
            FastIDSet  prefs2           = dataModel.GetItemIDsFromUser(userID2);
            int        prefs1Size       = prefs1.Count();
            int        prefs2Size       = prefs2.Count();
            int        intersectionSize = prefs1Size < prefs2Size?prefs2.IntersectionSize(prefs1) : prefs1.IntersectionSize(prefs2);

            return(doSimilarity(prefs1Size, prefs2Size, intersectionSize));
        }
        public override int GetNumUsersWithPreferenceFor(long itemID1, long itemID2)
        {
            FastIDSet userIDs1 = preferenceForItems.Get(itemID1);

            if (userIDs1 == null)
            {
                return(0);
            }
            FastIDSet userIDs2 = preferenceForItems.Get(itemID2);

            if (userIDs2 == null)
            {
                return(0);
            }
            return(userIDs1.Count() < userIDs2.Count()
        ? userIDs2.IntersectionSize(userIDs1)
        : userIDs1.IntersectionSize(userIDs2));
        }
Exemplo n.º 4
0
        public double UserSimilarity(long userID1, long userID2)
        {
            IDataModel dataModel = getDataModel();
            FastIDSet  prefs1    = dataModel.GetItemIDsFromUser(userID1);
            FastIDSet  prefs2    = dataModel.GetItemIDsFromUser(userID2);

            long prefs1Size       = prefs1.Count();
            long prefs2Size       = prefs2.Count();
            long intersectionSize =
                prefs1Size < prefs2Size?prefs2.IntersectionSize(prefs1) : prefs1.IntersectionSize(prefs2);

            if (intersectionSize == 0)
            {
                return(Double.NaN);
            }
            long   numItems      = dataModel.GetNumItems();
            double logLikelihood =
                LogLikelihood.logLikelihoodRatio(intersectionSize,
                                                 prefs2Size - intersectionSize,
                                                 prefs1Size - intersectionSize,
                                                 numItems - prefs1Size - prefs2Size + intersectionSize);

            return(1.0 - 1.0 / (1.0 + logLikelihood));
        }