Update linear regression to train/predict non-log

main
Eric Ihli 3 years ago
parent 32ac272c8a
commit f54a99258f

@ -134,13 +134,15 @@
estimate. It defines significantly different as exceeding 1.65 times the estimate. It defines significantly different as exceeding 1.65 times the
standard deviation of the Turing estimate." standard deviation of the Turing estimate."
[xs ys] [xs ys]
(let [n (count xs) (let [log-xs (map #(Math/log %) xs)
sum-x (apply + xs) log-ys (map #(Math/log %) ys)
sum-y (apply + ys) n (count xs)
sum-x (apply + log-xs)
sum-y (apply + log-ys)
mean-x (/ sum-x n) mean-x (/ sum-x n)
mean-y (/ sum-y n) mean-y (/ sum-y n)
err-x (map #(- % mean-x) xs) err-x (map #(- % mean-x) log-xs)
err-y (map #(- % mean-y) ys) err-y (map #(- % mean-y) log-ys)
err-x-sqr (map #(* % %) err-x) err-x-sqr (map #(* % %) err-x)
m (/ (apply + (map #(apply * %) (map vector err-x err-y))) m (/ (apply + (map #(apply * %) (map vector err-x err-y)))
(apply + err-x-sqr)) (apply + err-x-sqr))

@ -41,10 +41,8 @@
(t/testing "The results of the linear regression model are accurate" (t/testing "The results of the linear regression model are accurate"
(let [r-coll [1 2 3 5 10] (let [r-coll [1 2 3 5 10]
zr-coll [20 10 5 1 2] ;; not really smoothed, but smoothing isn't under test zr-coll [20 10 5 1 2] ;; not really smoothed, but smoothing isn't under test
log-r (map #(Math/log %) r-coll) linear-model (math/least-squares-linear-regression r-coll zr-coll)
log-zr (map #(Math/log %) zr-coll) linear-results (map linear-model r-coll)]
linear-model (math/least-squares-linear-regression log-r log-zr)
linear-results (map linear-model (map #(Math/log %) r-coll))]
(t/is (every? (t/is (every?
(fn [[expected predicted]] (fn [[expected predicted]]
(approx= expected predicted 0.01)) (approx= expected predicted 0.01))
@ -55,7 +53,7 @@
4.81 4.81
2.59 2.59
1.12) 1.12)
(map #(Math/pow Math/E %) linear-results))))))) linear-results))))))
;; The below passes a sanity check in that each r* is slightly less than r. ;; The below passes a sanity check in that each r* is slightly less than r.
#_(t/deftest turing-estimation #_(t/deftest turing-estimation

Loading…
Cancel
Save