public void testStddev()
        {
            IRunningAverageAndStdDev runningAverage = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, runningAverage.GetCount());
            Assert.True(Double.IsNaN(runningAverage.GetAverage()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(1, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(runningAverage.GetStandardDeviation()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(2, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(7.0);
            Assert.AreEqual(3, runningAverage.GetCount());
            Assert.AreEqual(3.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.464101552963257, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(5.0);
            Assert.AreEqual(4, runningAverage.GetCount());
            Assert.AreEqual(3.5, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.0, runningAverage.GetStandardDeviation(), EPSILON);
        }
        public void testFull()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, average.GetCount());
            Assert.True(Double.IsNaN(average.GetAverage()));
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(-4.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(1.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(5.0 * 1.4142135623730951, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(4.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(-2.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));
        }
        public void testFull()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, average.GetCount());
            Assert.True(Double.IsNaN(average.GetAverage()));
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(-4.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(1.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(5.0 * 1.4142135623730951, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(4.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(-2.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));
        }
        public void testFullBig()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            var r = RandomUtils.getRandom();
            for (int i = 0; i < 1000000; i++) {
              average.AddDatum(r.nextDouble() * 1000.0);
            }
            Assert.AreEqual(500.0, average.GetAverage(), SMALL_EPSILON);
            Assert.AreEqual(1000.0 / Math.Sqrt(12.0), average.GetStandardDeviation(), SMALL_EPSILON);
        }
 public void testAverageAndStdDev()
 {
     IRunningAverageAndStdDev avg = new FullRunningAverageAndStdDev();
     IRunningAverageAndStdDev inverted = new InvertedRunningAverageAndStdDev(avg);
     Assert.AreEqual(0, inverted.GetCount());
     avg.AddDatum(1.0);
     Assert.AreEqual(1, inverted.GetCount());
     Assert.AreEqual(-1.0, inverted.GetAverage(), EPSILON);
     avg.AddDatum(2.0);
     Assert.AreEqual(2, inverted.GetCount());
     Assert.AreEqual(-1.5, inverted.GetAverage(), EPSILON);
     Assert.AreEqual(Math.Sqrt(2.0)/2.0, inverted.GetStandardDeviation(), EPSILON);
 }
        public void testFullBig()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            var r = RandomUtils.getRandom();

            for (int i = 0; i < 1000000; i++)
            {
                average.AddDatum(r.nextDouble() * 1000.0);
            }
            Assert.AreEqual(500.0, average.GetAverage(), SMALL_EPSILON);
            Assert.AreEqual(1000.0 / Math.Sqrt(12.0), average.GetStandardDeviation(), SMALL_EPSILON);
        }
        public void testAverageAndStdDev()
        {
            IRunningAverageAndStdDev avg      = new FullRunningAverageAndStdDev();
            IRunningAverageAndStdDev inverted = new InvertedRunningAverageAndStdDev(avg);

            Assert.AreEqual(0, inverted.GetCount());
            avg.AddDatum(1.0);
            Assert.AreEqual(1, inverted.GetCount());
            Assert.AreEqual(-1.0, inverted.GetAverage(), EPSILON);
            avg.AddDatum(2.0);
            Assert.AreEqual(2, inverted.GetCount());
            Assert.AreEqual(-1.5, inverted.GetAverage(), EPSILON);
            Assert.AreEqual(Math.Sqrt(2.0) / 2.0, inverted.GetStandardDeviation(), EPSILON);
        }
Ejemplo n.º 8
0
  public static LoadStatistics runLoad(IRecommender recommender, int howMany) {
    IDataModel dataModel = recommender.GetDataModel();
    int numUsers = dataModel.GetNumUsers();
    double sampleRate = 1000.0 / numUsers;
    var userSampler =
        SamplinglongPrimitiveIterator.MaybeWrapIterator(dataModel.GetUserIDs(), sampleRate);
    
	if (userSampler.MoveNext()) 
		recommender.Recommend(userSampler.Current, howMany); // Warm up
    
	var callables = new List<Action>();
    while (userSampler.MoveNext()) {
      callables.Add(new LoadCallable(recommender, userSampler.Current).call);
    }
    AtomicInteger noEstimateCounter = new AtomicInteger();
    IRunningAverageAndStdDev timing = new FullRunningAverageAndStdDev();
    AbstractDifferenceRecommenderEvaluator.execute(callables, noEstimateCounter, timing);
    return new LoadStatistics(timing);
  }
        public void testStddev()
        {
            IRunningAverageAndStdDev runningAverage = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, runningAverage.GetCount());
            Assert.True(Double.IsNaN(runningAverage.GetAverage()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(1, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(runningAverage.GetStandardDeviation()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(2, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(7.0);
            Assert.AreEqual(3, runningAverage.GetCount());
            Assert.AreEqual(3.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.464101552963257, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(5.0);
            Assert.AreEqual(4, runningAverage.GetCount());
            Assert.AreEqual(3.5, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.0, runningAverage.GetStandardDeviation(), EPSILON);
        }
 private static double computeThreshold(IPreferenceArray prefs) {
   if (prefs.Length() < 2) {
     // Not enough data points -- return a threshold that allows everything
     return Double.NegativeInfinity;
   }
   IRunningAverageAndStdDev stdDev = new FullRunningAverageAndStdDev();
   int size = prefs.Length();
   for (int i = 0; i < size; i++) {
     stdDev.AddDatum(prefs.GetValue(i));
   }
   return stdDev.GetAverage() + stdDev.GetStandardDeviation();
 }
  private double getEvaluation(FastByIDMap<IPreferenceArray> testPrefs, IRecommender recommender)
  {
    reset();
    var estimateCallables = new List<Action>();
    AtomicInteger noEstimateCounter = new AtomicInteger();
    foreach (var entry in testPrefs.EntrySet()) {
      estimateCallables.Add( () => {
		  var testUserID = entry.Key;
		  var prefs = entry.Value;

		  foreach (IPreference realPref in 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);
			}
		  }


	  });
         // new PreferenceEstimateCallable(recommender, entry.Key, entry.Value, noEstimateCounter));
    }
    log.Info("Beginning evaluation of {} users", estimateCallables.Count);
    IRunningAverageAndStdDev timing = new FullRunningAverageAndStdDev();
    execute(estimateCallables, noEstimateCounter, timing);
    return computeFinalEvaluation();
  }