ZScore implements PreferenceTransform { @Override public float getTransformedValue(Preference pref) throws TasteException { RunningAverageAndStdDev meanAndStdev = meanAndStdevs.get(pref.getUserID()); if (meanAndStdev.getCount() > 1) { double stdev = meanAndStdev.getStandardDeviation(); if (stdev > 0.0) { return (float) ((pref.getValue() - meanAndStdev.getAverage()) / stdev); } } return 0.0f; } ZScore(DataModel dataModel); @Override float getTransformedValue(Preference pref); @Override void refresh(Collection<Refreshable> alreadyRefreshed); @Override String toString();  }
@Test public void testOnePref() throws Exception { DataModel dataModel = getDataModel(new long[] {1}, new Double[][] {{1.0}}); PreferenceTransform zScore = new ZScore(dataModel); assertEquals(0.0, zScore.getTransformedValue(new GenericPreference(1, 0, 1.0f)), EPSILON); }
@Test public void testAllSame() throws Exception { DataModel dataModel = getDataModel(new long[] {1}, new Double[][] {{1.0,1.0,1.0}}); PreferenceTransform zScore = new ZScore(dataModel); assertEquals(0.0, zScore.getTransformedValue(new GenericPreference(1, 0, 1.0f)), EPSILON); assertEquals(0.0, zScore.getTransformedValue(new GenericPreference(1, 1, 1.0f)), EPSILON); assertEquals(0.0, zScore.getTransformedValue(new GenericPreference(1, 2, 1.0f)), EPSILON); }
@Test public void testStdev() throws Exception { DataModel dataModel = getDataModel(new long[] {1}, new Double[][] {{-1.0,-2.0}}); PreferenceTransform zScore = new ZScore(dataModel); assertEquals(Math.sqrt(2.0)/2.0, zScore.getTransformedValue(new GenericPreference(1, 0, -1.0f)), EPSILON); assertEquals(-Math.sqrt(2.0)/2.0, zScore.getTransformedValue(new GenericPreference(1, 1, -2.0f)), EPSILON); }
@Test public void testExample() throws Exception { DataModel dataModel = getDataModel(new long[] {1}, new Double[][] {{5.0, 7.0, 9.0}}); PreferenceTransform zScore = new ZScore(dataModel); assertEquals(-1.0, zScore.getTransformedValue(new GenericPreference(1, 0, 5.0f)), EPSILON); assertEquals(0.0, zScore.getTransformedValue(new GenericPreference(1, 1, 7.0f)), EPSILON); assertEquals(1.0, zScore.getTransformedValue(new GenericPreference(1, 2, 9.0f)), EPSILON); }