-USING: assocs debugger hashtables help.markup help.syntax
-kernel quotations sequences math ;
+USING: debugger hashtables help.markup help.syntax kernel math
+sequences ;
IN: math.statistics
HELP: geometric-mean
{ $values { "seq" sequence } { "n" number } }
{ $description "Calculates the Z-Score for " { $snippet "seq" } "." } ;
+HELP: dcg
+{ $values
+ { "scores" sequence }
+ { "dcg" number }
+}
+{ $description "Calculates the discounted cumulative gain from a list of scores. The discounted cumulative gain can be used to compare two lists of results against each other given scores for each of the results."
+$nl
+" See " { $url "https://en.wikipedia.org/wiki/Discounted_cumulative_gain" }
+} ;
+
+HELP: ndcg
+{ $values
+ { "scores" sequence }
+ { "ndcg" number }
+}
+{ $description "Calculates the normalized discounted cumulative gain from a list of scores. The ndcg is the discounted cumulative gain divided by the theoretical maximum dcg for the given list."
+$nl
+"See " { $url "https://en.wikipedia.org/wiki/Discounted_cumulative_gain" }
+} ;
+
+{ dcg ndcg } related-words
+
ARTICLE: "histogram" "Computing histograms"
"Counting elements in a sequence:"
{ $subsections
"Counting the frequency of occurrence of elements:"
{ $subsections "histogram" }
"Computing cumulative sequences:"
-{ $subsections "cumulative" } ;
+{ $subsections "cumulative" }
+"Calculating discounted cumulative gain:"
+{ $subsections dcg ndcg } ;
ABOUT: "math.statistics"
-USING: arrays assocs kernel math math.functions math.statistics sequences
-math.order tools.test math.vectors ;
+USING: arrays kernel math math.functions math.order math.vectors
+sequences tools.test ;
FROM: math.ranges => [a,b] ;
+IN: math.statistics
{ 3 } [ { 1 2 3 4 5 } 1 power-mean ] unit-test
{ t } [ { 1 2 3 4 5 } [ 2 power-mean ] [ quadratic-mean ] bi 1e-10 ~ ] unit-test
] unit-test
{ 15+1/2 } [ { 4 8 15 16 23 42 } trimean ] unit-test
+
+{ 0 } [ { } dcg ] unit-test
+{ 0 } [ { } ndcg ] unit-test
+
+{ t } [ { 3 2 3 0 1 2 } dcg 6.861126688593501 1e-6 ~ ] unit-test
+{ t } [ { 3 2 3 0 1 2 } ndcg 0.9608081943360615 1e-6 ~ ] unit-test
\ No newline at end of file
USING: arrays assocs combinators fry generalizations grouping
kernel locals math math.functions math.order math.vectors
sequences sequences.private sorting ;
+FROM: math.ranges => [a,b] ;
IN: math.statistics
: power-mean ( seq p -- x )
: z-score ( seq -- n )
[ demean ] [ sample-std ] bi v/n ;
+
+: dcg ( scores -- dcg )
+ dup length 1 + 2 swap [a,b] [ log 2 log /f ] map v/ sum ;
+
+: ndcg ( scores -- ndcg )
+ [ 0 ] [
+ [ dcg ] [ natural-sort <reversed> dcg ] bi /f
+ ] if-empty ;