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)
(nth nrs (inc i))) (lm (inc x)))
lgt-estimate (lm x)] (lm x))
(assert (>= i 0) (str x " not found")) (let [turing-estimate (float
(if (or (< (Math/abs (- lgt-estimate turing-estimate)) (/ (* (inc x)
(* 1.65 stdv)) (nth nrs (inc i)))
take-lgt?) (nth nrs i)))
[lgt-estimate (estimator lm rs nrs true)] r-plus-one-squared
[turing-estimate (estimator lm rs nrs false)]))))) (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"))
(let [diff (Math/abs (- lgt-estimate turing-estimate))
take-lgt? (or take-lgt?
(< diff (* 1.95 stdv)))]
(println (format (str "%.2f %.2f %.2f"
"\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
(let [estimation (e (first coll))]
:else (recur
(let [x (nth zrs i) (rest coll)
y (nth lgts i) e
stdv (stdv-for-turing-estimate (conj estimations estimation)))))
(nth rs (inc i)) #_#_ N* (apply + (map #(apply * %) (map vector rs estimations)))
(nth zrs i) ]
(nth zrs (inc i))) [estimations]))
take-lgt? (or take-lgt?
(<= (Math/abs (- x y))
(* 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