Skip to content

Commit

Permalink
attempt at quadratic regression search
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlawrenceaspden committed Aug 26, 2013
1 parent 4308266 commit 1e69e11
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions quadraticregression.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
;; What is the best fit for a+bx+cxx here?
(defn fit [[ a b c]]
(let [predictions (map #(+ a (* b %) (* c % %)) (range 1 8))]
[(reduce + (map #(* % %)
(map - predictions
[2 6 14 26 40 60 96])))
[a b c]
predictions]))


(defn improve-guess [a b c delta]
(let [candidates (list [a b c]
[(+ a delta) b c]
[(- a delta) b c]
[a (+ b delta) c]
[a (- b delta) c]
[a b (+ c delta)]
[a b (- c delta)])
scores (sort (map fit candidates))
]
(first scores)))




(improve-guess 1 1 1 4) ;-> [639 [1 5 1] (7 15 25 37 51 67 85)]
(improve-guess 1 5 1 4) ;-> [407 [-3 5 1] (3 11 21 33 47 63 81)]
(improve-guess -3 5 1 4) ;-> [399 [-7 5 1] (-1 7 17 29 43 59 77)]
(improve-guess -7 5 1 4) ;-> [399 [-7 5 1] (-1 7 17 29 43 59 77)]
(improve-guess -7 5 1 2) ;-> [375 [-5 5 1] (1 9 19 31 45 61 79)]
(improve-guess -5 5 1 2) ;-> [375 [-5 5 1] (1 9 19 31 45 61 79)]
(improve-guess -5 5 1 1) ;-> [375 [-5 5 1] (1 9 19 31 45 61 79)]
(improve-guess -5 5 1 0.5) ;-> [362.0 [-5 5.5 1] (1.5 10.0 20.5 33.0 47.5 64.0 82.5)]


(defn refine [a b c delta]
(let [[score [na nb nc ] predictions ] (improve-guess a b c delta)]
(if (= [na nb nc] [a b c])
[[score [na nb nc ] predictions ]]
(refine na nb nc delta))))

;; [2 6 14 26 40 60 96]

(refine -5 5 1 0.1) ;-> [[203.7099999999998 [-7.89999999999999 4.200000000000003 1.3000000000000003] (-2.3999999999999866 5.700000000000017 16.40000000000002 29.700000000000024 45.60000000000003 64.10000000000004 85.20000000000005)]]
(refine 1 1 1 0.1) ;-> [[125.31000000000004 [-3.1000000000000014 1.0 1.7000000000000006] (-0.4000000000000008 5.700000000000001 15.200000000000003 28.10000000000001 44.40000000000001 64.10000000000002 87.20000000000002)]]
(refine 4.41 -4.48 2.41 0.01) ;-> [[50.068 [4.42 -4.49 2.42] (2.3499999999999996 5.119999999999999 12.73 25.18 42.47 64.6 91.57)]]
(refine 4.42 -4.49 2.42 0.005)





(refine 1 1 1 4) ;-> [[399 [-7 5 1] (-1 7 17 29 43 59 77)] [399 [-7 5 1] (-1 7 17 29 43 59 77)]]
(refine -7 5 1 2) ;-> [[375 [-5 5 1] (1 9 19 31 45 61 79)] [375 [-5 5 1] (1 9 19 31 45 61 79)]]
(refine -5 5 1 1) ;-> [[375 [-5 5 1] (1 9 19 31 45 61 79)] [375 [-5 5 1] (1 9 19 31 45 61 79)]]
(refine -5 5 1 0.5) ;-> [[330.0 [-7.0 5.5 1] (-0.5 8.0 18.5 31.0 45.5 62.0 80.5)] [330.0 [-7.0 5.5 1] (-0.5 8.0 18.5 31.0 45.5 62.0 80.5)]]
(refine -7 5.5 1 0.1) ;-> [[227.50999999999982 [-9.099999999999993 5.000000000000002 1.2000000000000002] (-2.8999999999999906 5.700000000000012 16.700000000000014 30.100000000000016 45.90000000000002 64.10000000000002 84.70000000000003)] [227.50999999999982 [-9.099999999999993 5.000000000000002 1.2000000000000002] (-2.8999999999999906 5.700000000000012 16.700000000000014 30.100000000000016 45.90000000000002 64.10000000000002 84.70000000000003)]]

(refine 1 1 1 4)







0 comments on commit 1e69e11

Please sign in to comment.