about summary refs log tree commit diff
path: root/sourcecodes/k-best/src/ls_NML.c
diff options
context:
space:
mode:
authorziejd22017-09-14 15:39:41 -0500
committerziejd22017-09-14 15:57:50 -0500
commitc4f926438dcb8abe805e910399940f79ff643c4b (patch)
tree3146d05ada5cf4b48d9bdd16c1baa498e8df7192 /sourcecodes/k-best/src/ls_NML.c
parent57ebf49403b75dcf8482d174b59f7fd2a961d98e (diff)
downloadBNW-c4f926438dcb8abe805e910399940f79ff643c4b.tar.gz
Add files via upload
Diffstat (limited to 'sourcecodes/k-best/src/ls_NML.c')
-rw-r--r--sourcecodes/k-best/src/ls_NML.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/sourcecodes/k-best/src/ls_NML.c b/sourcecodes/k-best/src/ls_NML.c
new file mode 100644
index 00000000..6c3523a4
--- /dev/null
+++ b/sourcecodes/k-best/src/ls_NML.c
@@ -0,0 +1,77 @@
+#include <math.h>
+#include "get_local_scores.h"
+#include "ilogi.h"
+#include "reg.h"
+#include "ls_NML.h"
+
+extern int*     nof_vals;
+extern int      N;
+
+#define BIG_NML_DATA (1<<16)
+
+/* NML = log(ML) - log(REGRET) */
+
+score_t big_nml_score(int i, varset_t psi, int nof_freqs){
+
+  int vc_v = nof_vals[i];
+  int* freqp = freqmem;
+  int* end_freqp = freqp + nof_freqs * vc_v;
+
+  score_t res = 0.0;
+
+  for(;freqp < end_freqp; freqp += vc_v) {
+    int  pcfreq = 0;
+    int v;
+    for(v = 0; v<vc_v; ++v) {
+      int freq = freqp[v];
+      if (freq) {
+	pcfreq += freq;
+	res += (freq < BIG_NML_DATA) ? ilogi[freq] : freq * log(freq);
+      }
+    }
+    res -= (pcfreq < BIG_NML_DATA) ? ilogi[pcfreq] : pcfreq * log(pcfreq);
+    res -= log(reg(pcfreq, vc_v)); /* regret */
+  }
+
+  return res;
+}
+
+score_t nml_score(int i, varset_t psi, int nof_freqs){
+
+  int vc_v = nof_vals[i];
+  int* freqp = freqmem;
+  int* end_freqp = freqp + nof_freqs * vc_v;
+
+  score_t res = 0.0;
+
+  for(;freqp < end_freqp; freqp += vc_v) {
+    int  pcfreq = 0;
+    int v;
+    for(v = 0; v<vc_v; ++v) {
+      int freq = freqp[v];
+      if (freq) {
+	pcfreq += freq;
+	res += ilogi[freq];
+      }
+    }
+    res -= ilogi[pcfreq];
+    res -= log(reg(pcfreq, vc_v)); /* regret */
+  }
+
+  return res;
+}
+
+
+scorefun init_NML_scorer(){
+  if (N<BIG_NML_DATA){
+    ensure_ilogi(N);
+    return nml_score;
+  } else {
+    ensure_ilogi(BIG_NML_DATA);
+    return big_nml_score;
+  }
+}
+
+void free_NML_scorer(){
+  free_ilogi();
+}