-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeighborCFExt.java
1711 lines (1435 loc) · 54.7 KB
/
NeighborCFExt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.hudup.alg.cf;
import java.rmi.RemoteException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.hudup.core.Constants;
import net.hudup.core.Util;
import net.hudup.core.alg.cf.NeighborCF;
import net.hudup.core.data.DataConfig;
import net.hudup.core.data.Dataset;
import net.hudup.core.data.Profile;
import net.hudup.core.data.RatingVector;
import net.hudup.core.logistic.DSUtil;
import net.hudup.core.logistic.Inspector;
import net.hudup.core.logistic.NextUpdate;
import net.hudup.core.logistic.Vector2;
import net.hudup.core.parser.TextParserUtil;
import net.hudup.data.DocumentVector;
import net.hudup.evaluate.ui.EvaluateGUI;
/**
* This class sets up an advanced version of neighbor collaborative filtering (Neighbor CF) algorithm with more similarity measures.
* <br>
* There are many authors who contributed measure to this class.<br>
* Authors Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu contributed PSS measures and NHSM measure.<br>
* Authors Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi contributed BC and BCF measures.<br>
* Author Hyung Jun Ahn contributed PIP measure.<br>
* Authors Keunho Choi and Yongmoo Suh contributed PC measure.<br>
* Authors Suryakant and Tripti Mahara contributed MMD measure and CjacMD measure.<br>
* Authors Junmei Feng, Xiaoyi Fengs, Ning Zhang, and Jinye Peng contributed Feng model.<br>
* Authors Yi Mua, Nianhao Xiao, Ruichun Tang, Liang Luo, and Xiaohan Yin contributed Mu measure.<br>
* Authors Yung-Shen Lin, Jung-Yi Jiang, Shie-Jue Lee contributed SMTP measure.<br>
* Author Ali Amer contributed Amer and Amer2 measures.<br>
* Author Loc Nguyen contributed TA (triangle area) measure.<br>
* Authors Ali Amer and Loc Nguyen contributed quasi-TfIdf measure. Quasi-TfIdf measure is an extension of Amer2 measure and the ideology of TF and IDF.<br>
* Author Ali Amer contributed numerical nearby similarity measure (MMNS).
*
* @author Loc Nguyen
* @version 1.0
*
*/
public abstract class NeighborCFExt extends NeighborCF {
/**
* Serial version UID for serializable class.
*/
private static final long serialVersionUID = 1L;
/**
* Name of PSS measure.
*/
public static final String PSS = "pss";
/**
* Name of NHSM measure.
*/
public static final String NHSM = "nhsm";
/**
* Name of BCF measure.
*/
public static final String BCF = "bcf";
/**
* Name of BCFJ measure (BCF + Jaccard).
*/
public static final String BCFJ = "bcfj";
/**
* Name of SRC measure.
*/
public static final String SRC = "src";
/**
* Name of PIP measure.
*/
public static final String PIP = "pip";
/**
* Name of PC measure.
*/
public static final String PC = "pc";
/**
* Name of MMD measure.
*/
public static final String MMD = "mmd";
/**
* Name of CjacMD measure which is developed by Suryakant and Tripti Mahara.
*/
public static final String CJACMD = "mmd";
/**
* Name of Feng measure.
*/
public static final String FENG = "feng";
/**
* Name of Mu measure.
*/
public static final String MU = "mu";
/**
* Name of SMTP measure.
*/
public static final String SMTP = "smtp";
/**
* Name of Amer measure.
*/
public static final String AMER = "amer";
/**
* Name of Amer2 measure.
*/
public static final String AMER2 = "amer2";
/**
* Name of Amer2 + Jaccard measure.
*/
public static final String AMER2J = "amer2j";
/**
* Name of Quasi-TfIdf measure.
*/
public static final String QUASI_TFIDF = "qti";
/**
* Name of Quasi-TfIdf + Jaccard measure.
*/
public static final String QUASI_TFIDF_JACCARD = "qtij";
/**
* Name of triangle area measure.
*/
public static final String TA = "ta";
/**
* Name of triangle area + Jaccard measure.
*/
public static final String TAJ = "taj";
/**
* Name of Coco measure.
*/
public static final String COCO = "coco";
/**
* Name of numerical nearby similarity measure (MMNS).
*/
public static final String NNMS = "mmns";
/**
* Value bins.
*/
public static final String VALUE_BINS_FIELD = "value_bins";
/**
* Default value bins.
*/
public static final String VALUE_BINS_DEFAULT = "1, 2, 3, 4, 5";
/**
* BCF median mode.
*/
public static final String BCF_MEDIAN_MODE_FIELD = "bcf_median";
/**
* Default BCF median mode.
*/
public static final boolean BCF_MEDIAN_MODE_DEFAULT = true;
/**
* Mu alpha field.
*/
public static final String MU_ALPHA_FIELD = "mu_alpha";
/**
* Default Mu alpha.
*/
public static final double MU_ALPHA_DEFAULT = 0.5;
/**
* Name of lambda field.
*/
public static final String SMTP_LAMBDA_FIELD = "smtp_lambda";
/**
* Default lambda field.
*/
public static final double SMTP_LAMBDA_DEFAULT = 0.5;
/**
* Name of general variance field.
*/
public static final String SMTP_GENERAL_VAR_FIELD = "smtp_general_var";
/**
* Default general variance field.
*/
public static final boolean SMTP_GENERAL_VAR_DEFAULT = false;
/**
* TA normalized mode.
*/
public static final String TA_NORMALIZED_FIELD = "ta_normalized";
/**
* Default TA normalized mode.
*/
public static final boolean TA_NORMALIZED_DEFAULT = false;
/**
* Value bins.
*/
protected List<Double> valueBins = Util.newList();
/**
* Rank bins.
*/
protected Map<Double, Integer> rankBins = Util.newMap();
/**
* Column module (column vector length) cache.
*/
protected Map<Integer, Object> bcfColumnModuleCache = Util.newMap();
/**
* Default constructor.
*/
public NeighborCFExt() {
// TODO Auto-generated constructor stub
}
@Override
public synchronized void setup(Dataset dataset, Object...params) throws RemoteException {
// TODO Auto-generated method stub
super.setup(dataset, params);
this.valueBins = extractConfigValueBins();
this.rankBins = convertValueBinsToRankBins(this.valueBins);
}
@Override
public synchronized void unsetup() throws RemoteException {
// TODO Auto-generated method stub
super.unsetup();
this.rankBins.clear();
this.valueBins.clear();
this.bcfColumnModuleCache.clear();
}
@Override
public List<String> getSupportedMeasures() {
// TODO Auto-generated method stub
List<String> measures = super.getSupportedMeasures();
Set<String> mSet = Util.newSet();
mSet.addAll(measures);
mSet.add(PSS);
// mSet.add(NHSM);
mSet.add(BCF);
// mSet.add(BCFJ);
mSet.add(SRC);
mSet.add(PIP);
mSet.add(PC);
mSet.add(MMD);
// mSet.add(CJACMD);
mSet.add(SMTP);
mSet.add(AMER);
mSet.add(AMER2);
// mSet.add(AMER2J);
mSet.add(QUASI_TFIDF);
// mSet.add(QUASI_TFIDF_JACCARD);
mSet.add(TA);
// mSet.add(TAJ);
mSet.add(COCO);
mSet.add(NNMS);
measures.clear();
measures.addAll(mSet);
Collections.sort(measures);
return measures;
}
/**
* Checking whether the similarity measure requires to declare discrete bins in configuration ({@link #VALUE_BINS_FIELD}).
* @return true if the similarity measure requires to declare discrete bins in configuration ({@link #VALUE_BINS_FIELD}). Otherwise, return false.
*/
public boolean requireDiscreteRatingBins() {
return requireDiscreteRatingBins(getMeasure());
}
/**
* Given specified measure, checking whether the similarity measure requires to declare discrete bins in configuration ({@link #VALUE_BINS_FIELD}).
* @param measure specified measure.
* @return true if the similarity measure requires to declare discrete bins in configuration ({@link #VALUE_BINS_FIELD}). Otherwise, return false.
*/
protected boolean requireDiscreteRatingBins(String measure) {
if (measure == null)
return false;
else if (measure.equals(BCF) || measure.equals(BCFJ) || measure.equals(MMD))
return true;
else
return false;
}
@Override
protected boolean isCachedSim() {
// TODO Auto-generated method stub
String measure = getMeasure();
if (measure == null)
return false;
else if (measure.equals(PC))
return false;
else
return super.isCachedSim();
}
@Override
protected double sim0(String measure, RatingVector vRating1, RatingVector vRating2, Profile profile1, Profile profile2, Object...params) {
// TODO Auto-generated method stub
if (measure.equals(PSS))
return pss(vRating1, vRating2, profile1, profile2);
else if (measure.equals(NHSM))
return nhsm(vRating1, vRating2, profile1, profile2);
else if (measure.equals(BCF))
return bcf(vRating1, vRating2, profile1, profile2);
else if (measure.equals(BCFJ))
return bcfj(vRating1, vRating2, profile1, profile2);
else if (measure.equals(SRC))
return src(vRating1, vRating2, profile1, profile2);
else if (measure.equals(PIP))
return pip(vRating1, vRating2, profile1, profile2);
else if (measure.equals(PC)) {
if ((params == null) || (params.length < 1) || !(params[0] instanceof Number))
return Constants.UNUSED;
else {
int fixedColumnId = ((Number)(params[0])).intValue();
return pc(vRating1, vRating2, profile1, profile2, fixedColumnId);
}
}
else if (measure.equals(MMD))
return mmd(vRating1, vRating2, profile1, profile2);
else if (measure.equals(CJACMD))
return cosine(vRating1, vRating2, profile1, profile2) + mmd(vRating1, vRating2, profile1, profile2) + jaccard(vRating1, vRating2, profile1, profile2);
else if (measure.equals(FENG))
return feng(vRating1, vRating2, profile1, profile2);
else if (measure.equals(MU))
return mu(vRating1, vRating2, profile1, profile2);
else if (measure.equals(SMTP))
return smtp(vRating1, vRating2, profile1, profile2);
else if (measure.equals(AMER))
return amer(vRating1, vRating2, profile1, profile2, this.itemIds);
else if (measure.equals(AMER2))
return amer2(vRating1, vRating2, profile1, profile2);
else if (measure.equals(AMER2J))
return amer2j(vRating1, vRating2, profile1, profile2);
else if (measure.equals(QUASI_TFIDF))
return quasiTfIdf(vRating1, vRating2, profile1, profile2);
else if (measure.equals(QUASI_TFIDF_JACCARD))
return quasiTfIdfJaccard(vRating1, vRating2, profile1, profile2);
else if (measure.equals(TA))
return triangleArea(vRating1, vRating2, profile1, profile2);
else if (measure.equals(TAJ))
return triangleAreaJaccard(vRating1, vRating2, profile1, profile2);
else if (measure.equals(COCO))
return coco(vRating1, vRating2, profile1, profile2);
else if (measure.equals(NNMS))
return mmns(vRating1, vRating2, profile1, profile2);
else
return super.sim0(measure, vRating1, vRating2, profile1, profile2, params);
}
/**
* Calculating the PSS measure between two pairs. PSS measure is developed by Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu.
* @return PSS measure between both two rating vectors and profiles.
*/
protected abstract double pss(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2);
/**
* Calculating the PSS measure between two rating vectors. PSS measure is developed by Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu, and implemented by Loc Nguyen.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param fieldMeans map of field means.
* @author Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu.
* @return PSS measure between two rating vectors.
*/
protected double pss(RatingVector vRating1, RatingVector vRating2, Map<Integer, Double> fieldMeans) {
Set<Integer> common = commonFieldIds(vRating1, vRating2);
if (common.size() == 0) return Constants.UNUSED;
double pss = 0.0;
for (int id : common) {
double r1 = vRating1.get(id).value;
double r2 = vRating2.get(id).value;
double pro = 1.0 - 1.0 / (1.0 + Math.exp(-Math.abs(r1-r2)));
//Note: I think that it is better to use mean instead of median for significant.
//At the worst case, median is always approximate to mean given symmetric distribution like normal distribution.
//Moreover, in fact, general user mean is equal to general item mean.
//However, I still use rating median because of respecting authors' ideas.
double sig = 1.0 / (1.0 + Math.exp(
-Math.abs(r1-ratingMedian)*Math.abs(r2-ratingMedian)));
double singular = 1.0 - 1.0 / (1.0 + Math.exp(-Math.abs((r1+r2)/2.0 - fieldMeans.get(id))));
pss += pro * sig * singular;
}
return pss;
}
/**
* Calculating the NHSM measure between two pairs. NHSM measure is developed by Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Haifeng Liu, Zheng Hu, Ahmad Mian, Hui Tian, Xuzhen Zhu.
* @return NHSM measure between both two rating vectors and profiles.
*/
protected double nhsm(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
double urp = urp(vRating1, vRating2, profile1, profile2);
double jaccard2 = jaccard2(vRating1, vRating2, profile1, profile2);
return pss(vRating1, vRating2, profile1, profile2) * jaccard2 * urp;
}
/**
* Calculate the Bhattacharyya measure from specified rating vectors. BC measure is modified by Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi, and implemented by Loc Nguyen.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi.
* @return Bhattacharyya measure from specified rating vectors.
*/
@NextUpdate
protected double bc(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
Task task = new Task() {
@Override
public Object perform(Object...params) {
List<Double> bins = valueBins;
if (bins.isEmpty())
bins = extractValueBins(vRating1, vRating2);
Set<Integer> ids1 = vRating1.fieldIds(true);
Set<Integer> ids2 = vRating2.fieldIds(true);
int n1 = ids1.size();
int n2 = ids2.size();
if (n1 == 0 || n2 == 0) return Constants.UNUSED;
double bc = 0;
for (double bin : bins) {
int count1 = 0, count2 = 0;
for (int id1 : ids1) {
if (vRating1.get(id1).value == bin)
count1++;
}
for (int id2 : ids2) {
if (vRating2.get(id2).value == bin)
count2++;
}
bc += Math.sqrt( ((double)count1/(double)n1) * ((double)count2/(double)n2) );
}
return bc;
}
};
return (double)cacheTask(vRating1.id(), vRating2.id(), this.columnSimCache, task);
}
/**
* Calculating the advanced BCF measure between two pairs. BCF measure is developed by Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi.
* @return BCF measure between both two rating vectors and profiles.
*/
@NextUpdate
protected double bcf(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
Set<Integer> columnIds1 = vRating1.fieldIds(true);
Set<Integer> columnIds2 = vRating2.fieldIds(true);
if (columnIds1.size() == 0 || columnIds2.size() == 0)
return Constants.UNUSED;
double bcSum = 0;
boolean medianMode = getConfig().getAsBoolean(BCF_MEDIAN_MODE_FIELD);
for (int columnId1 : columnIds1) {
RatingVector columnVector1 = getColumnRating(columnId1);
if (columnVector1 == null) continue;
double columnModule1 = bcfCalcColumnModule(columnVector1);
if (!Util.isUsed(columnModule1) || columnModule1 == 0) continue;
double value1 = medianMode? vRating1.get(columnId1).value-this.ratingMedian : vRating1.get(columnId1).value-vRating1.mean();
for (int columnId2 : columnIds2) {
RatingVector columnVector2 = columnId2 == columnId1 ? columnVector1 : getColumnRating(columnId2);
if (columnVector2 == null) continue;
double columnModule2 = bcfCalcColumnModule(columnVector2);
if (!Util.isUsed(columnModule2) || columnModule2 == 0) continue;
double bc = bc(columnVector1, columnVector2, profile1, profile2);
if (!Util.isUsed(bc)) continue;
double value2 = medianMode? vRating2.get(columnId2).value-this.ratingMedian : vRating2.get(columnId2).value-vRating2.mean();
double loc = value1 * value2 / (columnModule1*columnModule2);
if (!Util.isUsed(loc)) continue;
bcSum += bc * loc;
}
}
return bcSum;
}
/**
* Calculating the advanced BCFJ measure (BCF + Jaccard) between two pairs. BCF measure is developed by Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Bidyut Kr. Patra, Raimo Launonen, Ville Ollikainen, Sukumar Nandi.
* @return BCFJ measure between both two rating vectors and profiles.
*/
protected double bcfj(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
return bcf(vRating1, vRating2, profile1, profile2) + jaccard(vRating1, vRating2, profile1, profile2);
}
/**
* Calculating module (length) of column rating vector for BCF measure.
* @param columnVector specified column rating vector.
* @return module (length) of column rating vector.
*/
protected double bcfCalcColumnModule(RatingVector columnVector) {
double ratingMedian = this.ratingMedian;
Task task = new Task() {
@Override
public Object perform(Object...params) {
if (columnVector == null) return Constants.UNUSED;
Set<Integer> fieldIds = columnVector.fieldIds(true);
double columnModule = 0;
boolean medianMode = getConfig().getAsBoolean(BCF_MEDIAN_MODE_FIELD);
for (int fieldId : fieldIds) {
double deviate = medianMode ? columnVector.get(fieldId).value-ratingMedian : columnVector.get(fieldId).value;
columnModule += deviate * deviate;
}
return Math.sqrt(columnModule);
}
};
return (double)cacheTask(columnVector.id(), this.bcfColumnModuleCache, task);
}
/**
* Calculating the Spearman Rank Correlation (SRC) measure between two pairs.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @return Spearman Rank Correlation (SRC) measure between both two rating vectors and profiles.
*/
protected double src(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
Map<Double, Integer> bins = rankBins;
if (bins.isEmpty())
bins = extractRankBins(vRating1, vRating2);
Set<Integer> common = commonFieldIds(vRating1, vRating2);
if (common.size() == 0) return Constants.UNUSED;
double sum = 0;
for (int id : common) {
double v1 = vRating1.get(id).value;
int r1 = bins.get(v1);
double v2 = vRating2.get(id).value;
int r2 = bins.get(v2);
int d = r1 - r2;
sum += d*d;
}
double n = common.size();
return 1.0 - 6*sum/(n*(n*n-1));
}
/**
* Calculating the PIP measure between two pairs. PIP measure is developed by Hyung Jun Ahn, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Hyung Jun Ahn.
* @return NHSM measure between both two rating vectors and profiles.
*/
protected abstract double pip(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2);
/**
* Calculating the PIP measure between two rating vectors. PIP measure is developed by Hyung Jun Ahn and implemented by Loc Nguyen.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param fieldMeans map of field means.
* @author Hyung Jun Ahn
* @return PIP measure between two rating vectors.
*/
protected double pip(RatingVector vRating1, RatingVector vRating2, Map<Integer, Double> fieldMeans) {
Set<Integer> common = commonFieldIds(vRating1, vRating2);
if (common.size() == 0) return Constants.UNUSED;
double pip = 0.0;
for (int id : common) {
double r1 = vRating1.get(id).value;
double r2 = vRating2.get(id).value;
boolean agreed = agree(r1, r2);
double d = agreed ? Math.abs(r1-r2) : 2*Math.abs(r1-r2);
double pro = (2*(config.getMaxRating()-config.getMinRating())+1) - d;
pro = pro*pro;
double impact = (Math.abs(r1-ratingMedian)+1) * (Math.abs(r2-ratingMedian)+1);
if (!agreed)
impact = 1 / impact;
double mean = fieldMeans.get(id);
double pop = 1;
if ((r1 > mean && r2 > mean) || (r1 < mean && r2 < mean)) {
double bias = (r1+r2)/2 - mean;
pop = 1 + bias*bias;
}
pip += pro * impact * pop;
}
return pip;
}
/**
* Checking whether two ratings are agreed.
* @param rating1 first rating.
* @param rating2 second rating.
* @return true if two ratings are agreed.
*/
protected boolean agree(double rating1, double rating2) {
if ( (rating1 > this.ratingMedian && rating2 < this.ratingMedian) || (rating1 < this.ratingMedian && rating2 > this.ratingMedian) )
return false;
else
return true;
}
/**
* Calculating the PC measure between two rating vectors. PC measure is developed by Keunho Choi and Yongmoo Suh. It implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @param fixedColumnId fixed column identifier.
* @author Hyung Jun Ahn.
* @return PC measure between both two rating vectors and profiles.
*/
protected abstract double pc(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2, int fixedColumnId);
/**
* Calculating the PC measure between two rating vectors. PC measure is developed by Keunho Choi and Yongmoo Suh. It implemented by Loc Nguyen.
* @param vRating1 the first rating vectors.
* @param vRating2 the second rating vectors.
* @param fixedColumnId fixed field (column) identifier.
* @param fieldMeans mean value of field ratings.
* @author Keunho Choi, Yongmoo Suh
* @return PC measure between two rating vectors.
*/
protected double pc(RatingVector vRating1, RatingVector vRating2, int fixedColumnId, Map<Integer, Double> fieldMeans) {
Set<Integer> common = commonFieldIds(vRating1, vRating2);
if (common.size() == 0) return Constants.UNUSED;
double vx = 0, vy = 0;
double vxy = 0;
for (int fieldId : common) {
double mean = fieldMeans.get(fieldId);
double d1 = vRating1.get(fieldId).value - mean;
double d2 = vRating2.get(fieldId).value - mean;
Task columnSimTask = new Task() {
@Override
public Object perform(Object...params) {
RatingVector fixedColumnVector = getColumnRating(fixedColumnId);
RatingVector columnVector = getColumnRating(fieldId);
if (fixedColumnVector == null || columnVector == null)
return Constants.UNUSED;
else
return fixedColumnVector.corr(columnVector);
}
};
double columnSim = (double)cacheTask(fixedColumnId, fieldId, this.columnSimCache, columnSimTask);
columnSim = columnSim * columnSim;
vx += d1 * d1 * columnSim;
vy += d2 * d2 * columnSim;
vxy += d1 * d2 * columnSim;
}
if (vx == 0 || vy == 0)
return Constants.UNUSED;
else
return vxy / Math.sqrt(vx * vy);
}
/**
* Calculating the Mean Measure of Divergence (MMD) measure between two pairs.
* Suryakant and Tripti Mahara proposed use of MMD for collaborative filtering. Loc Nguyen implements it.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Suryakant, Tripti Mahara
* @return MMD measure between both two rating vectors and profiles.
*/
protected double mmd(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
Set<Integer> ids1 = vRating1.fieldIds(true);
Set<Integer> ids2 = vRating2.fieldIds(true);
int N1 = ids1.size();
int N2 = ids2.size();
if (N1 == 0 || N2 == 0) return Constants.UNUSED;
List<Double> bins = valueBins;
if (bins.isEmpty())
bins = extractValueBins(vRating1, vRating2);
double sum = 0;
for (double bin : bins) {
int n1 = 0, n2 = 0;
for (int id1 : ids1) {
if (vRating1.get(id1).value == bin)
n1++;
}
for (int id2 : ids2) {
if (vRating2.get(id2).value == bin)
n2++;
}
double thetaBias = mmdTheta(n1, N1) - mmdTheta(n2, N2);
sum += thetaBias*thetaBias - 1/(0.5+n1) - 1/(0.5+n2);
}
return 1 / (1 + sum/bins.size());
}
/**
* Theta transformation of Mean Measure of Divergence (MMD) measure.
* The default implementation is Grewal transformation.
* @param n number of observations having a trait.
* @param N number of observations
* @return Theta transformation of Mean Measure of Devergence (MMD) measure.
*/
protected double mmdTheta(int n, int N) {
return 1 / Math.sin(1-2*(n/N));
}
/**
* Calculating the Feng measure between two pairs.
* Junmei Feng, Xiaoyi Fengs, Ning Zhang, and Jinye Peng developed the Triangle measure. Loc Nguyen implements it.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Junmei Feng, Xiaoyi Fengs, Ning Zhang, Jinye Peng
* @return Feng measure between both two rating vectors and profiles.
*/
protected double feng(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
double s1 = coj(vRating1, vRating2, profile1, profile2);
Set<Integer> ids1 = vRating1.fieldIds(true);
Set<Integer> ids2 = vRating2.fieldIds(true);
Set<Integer> common = Util.newSet();
common.addAll(ids1);
common.retainAll(ids2);
double s2 = 1 / ( 1 + Math.exp(-common.size()*common.size()/(ids1.size()*ids2.size())) );
double s3 = urp(vRating1, vRating2, profile1, profile2);
return s1 * s2 * s3;
}
/**
* Calculating the Mu measure between two pairs.
* Yi Mua, Nianhao Xiao, Ruichun Tang, Liang Luo, and Xiaohan Yin developed Mu measure. Loc Nguyen implements it.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Yi Mua, Nianhao Xiao, Ruichun Tang, Liang Luo, Xiaohan Yin
* @return Mu measure between both two rating vectors and profiles.
*/
@NextUpdate
protected double mu(RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
double alpha = config.getAsReal(MU_ALPHA_FIELD);
double pearson = corr(vRating1, vRating2, profile1, profile2);
double hg = 1 - bc(vRating1, vRating2, profile1, profile2);
// double hg = bc(vRating1, vRating2, profile1, profile2);
double jaccard = jaccard(vRating1, vRating2, profile1, profile2);
return alpha*pearson + (1-alpha)*(hg+jaccard);
}
/**
* Calculating the SMTP measure between two pairs. SMTP is developed by Yung-Shen Lin, Jung-Yi Jiang, Shie-Jue Lee, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @author Yung-Shen Lin, Jung-Yi Jiang, Shie-Jue Lee.
* @return SMTP measure between both two rating vectors.
*/
protected double smtp(
RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2) {
List<Integer> common = commonFieldIdsAsList(vRating1, vRating2);
common.retainAll(this.itemVars.keySet());
if (common.size() == 0) return Constants.UNUSED;
double[] data1 = new double[common.size()];
double[] data2 = new double[common.size()];
double[] vars = new double[common.size()];
boolean useGeneralVar = getConfig().getAsBoolean(SMTP_GENERAL_VAR_FIELD);
for (int i = 0; i < common.size(); i++) {
int id = common.get(i);
data1[i] = vRating1.get(id).value;
data2[i] = vRating2.get(id).value;
if (useGeneralVar)
vars[i] = this.ratingVar;
else
vars[i] = this.itemVars.get(id);
}
DocumentVector vector1 = new DocumentVector(data1);
DocumentVector vector2 = new DocumentVector(data2);
double lamda = getConfig().getAsReal(SMTP_LAMBDA_FIELD);
return vector1.smtp(vector2, lamda, vars);
}
/**
* Calculating the Amer measure between two pairs. Amer measure is developed by Ali Amer, and implemented by Loc Nguyen.
* The first pair includes the first rating vector and the first profile.
* The second pair includes the second rating vector and the second profile.
*
* @param vRating1 first rating vector.
* @param vRating2 second rating vector.
* @param profile1 first profile.
* @param profile2 second profile.
* @param itemIds set of all item identifiers
* @author Ali Amer.
* @return Amer measure between both two rating vectors and profiles.
*/
protected double amer(
RatingVector vRating1, RatingVector vRating2,
Profile profile1, Profile profile2, Set<Integer> itemIds) {
if (itemIds == null)
itemIds = Util.newSet();
itemIds.addAll(unionFieldIds(vRating1, vRating2));
int N = itemIds.size();
if (N == 0) return Constants.UNUSED;
int Na = 0, Nb = 0, Nab = 0, F = 0;
for (int itemId : itemIds) {
boolean rated1 = vRating1.isRated(itemId);
boolean rated2 = vRating2.isRated(itemId);
if (rated1) Na++;
if (rated2) Nb++;
if (rated1 && rated2) Nab++;
if ((rated1 && !rated2) || (!rated1 && rated2)) F++;
}
return ((1.0 - F/N) + (2.0*Nab / (Na + Nb))) / 2.0;
}