Parity with c++ code up to estimations

main
Eric Ihli 4 years ago
parent 56109c3810
commit 79fa3e2d80

@ -151,7 +151,7 @@
(format (format
(str "See Good-Turing Without Tears" (str "See Good-Turing Without Tears"
" for why slope must be less than -1." " for why slope must be less than -1."
"\nSlope: %0.2f Intersect %0.2f") "\nSlope: %.2f Intersect %.2f")
(float m) (float m)
(float b))) (float b)))
(fn [x] (fn [x]
@ -255,19 +255,56 @@
([lm rs nrs] ([lm rs nrs]
(estimator lm rs nrs false)) (estimator lm rs nrs false))
([lm rs nrs take-lgt?] ([lm rs nrs take-lgt?]
(fn [x] (fn
(let [i (.indexOf x rs) ([x]
turing-estimate (nth rs i) (let [i (.indexOf rs x)]
stdv (stdv-for-turing-estimate (nth rs (inc i)) (if (= (inc i) (count rs))
turing-estimate (/ (* (inc x)
(lm (inc x)))
(lm x))
(let [turing-estimate (float
(/ (* (inc x)
(nth nrs (inc i))) (nth nrs (inc i)))
lgt-estimate (lm x)] (nth nrs i)))
r-plus-one-squared
(Math/pow (inc x) 2)
term2
(/ (nth nrs (inc i))
(Math/pow (nth nrs i) 2))
term3
(inc (/ (nth nrs (inc i))
(nth nrs i)))
stdv (Math/sqrt (* r-plus-one-squared term2 term3))
lgt-estimate (/ (* (inc x)
(lm (inc x)))
(lm x))]
(assert (>= i 0) (str x " not found")) (assert (>= i 0) (str x " not found"))
(if (or (< (Math/abs (- lgt-estimate turing-estimate)) (let [diff (Math/abs (- lgt-estimate turing-estimate))
(* 1.65 stdv)) take-lgt? (or take-lgt?
take-lgt?) (< diff (* 1.95 stdv)))]
[lgt-estimate (estimator lm rs nrs true)] (println (format (str "%.2f %.2f %.2f"
[turing-estimate (estimator lm rs nrs false)]))))) "\nstdev %.2f"
"\nx %.2f y %.2f"
"\ntake-lgt? %b")
(float (inc x)) (float (nth nrs (inc i))) (float (nth nrs i))
(float stdv)
(float turing-estimate) (float lgt-estimate)
take-lgt?))
(if take-lgt?
lgt-estimate
turing-estimate))))))
([x rs nrs]
(let [i (.indexOf rs x)]
(if take-lgt?
(/ (* (inc x)
(lm (inc x)))
(lm x))
(float (/ (* (inc x)
(nth nrs (inc i)))
(nth nrs i)))))))))
(defn sgt [rs nrs] (defn sgt [rs nrs]
(assert (and (not-empty nrs) (not-empty rs)) (assert (and (not-empty nrs) (not-empty rs))
@ -280,32 +317,20 @@
log-zrs (map #(Math/log %) zrs) log-zrs (map #(Math/log %) zrs)
lm (least-squares-linear-regression log-rs log-zrs) lm (least-squares-linear-regression log-rs log-zrs)
lgts (map lm rs) lgts (map lm rs)
sgts (loop [i 0 estimations (loop [coll rs
result [] e (estimator lm rs zrs)
take-lgt? false] estimations []]
(cond (cond
(= (inc i) l) (empty? coll) estimations
(conj result (last lgts))
:else :else
(let [x (nth zrs i) (let [estimation (e (first coll))]
y (nth lgts i) (recur
stdv (stdv-for-turing-estimate (rest coll)
(nth rs (inc i)) e
(nth zrs i) (conj estimations estimation)))))
(nth zrs (inc i))) #_#_ N* (apply + (map #(apply * %) (map vector rs estimations)))
take-lgt? (or take-lgt? ]
(<= (Math/abs (- x y)) [estimations]))
(* 1.65 stdv)))]
(recur (inc i)
(conj
result
(if take-lgt?
(nth lgts i)
(nth zrs i)))
take-lgt?))))
N* (apply + (map #(apply * %) (map vector rs sgts)))]
))
(comment (comment
(let [rs [1 2 3 4 5 6 7 8 9 10 12 26] (let [rs [1 2 3 4 5 6 7 8 9 10 12 26]

Loading…
Cancel
Save