# ClojureDocs(beta)

• incanter

1.2.3-SNAPSHOT

## incanter.optimize

• (gradient f start & options)

Returns a function that calculates a 5-point approximation to
the gradient of the given function. The vector of start values are
used to determine the number of parameters required by the function, and
to scale the step-size. The generated function accepts a vector of
parameter values and a vector of x data points and returns a matrix,
where each row is the gradient evaluated at the corresponding x value.

Examples:

(use '(incanter core optimize datasets charts))
(defn f [theta x]
(+ (nth theta 0)
(div (* x (- (nth theta 1) (nth theta 0)))
(+ (nth theta 2) x))))

(def start [20 200 100])
(def data (to-matrix (get-dataset :thurstone)))
(def x (sel data :cols 1))
(def y (sel data :cols 0))
;; view the data
(view (scatter-plot x y))

### Source incanter/optimize.clj:250 top

```(defn gradient
"
Returns a function that calculates a 5-point approximation to
the gradient of the given function. The vector of start values are
used to determine the number of parameters required by the function, and
to scale the step-size. The generated function accepts a vector of
parameter values and a vector of x data points and returns a matrix,
where each row is the gradient evaluated at the corresponding x value.

Examples:

(use '(incanter core optimize datasets charts))
(defn f [theta x]
(+ (nth theta 0)
(div (* x (- (nth theta 1) (nth theta 0)))
(+ (nth theta 2) x))))

(def start [20 200 100])
(def data (to-matrix (get-dataset :thurstone)))
(def x (sel data :cols 1))
(def y (sel data :cols 0))
;; view the data
(view (scatter-plot x y))

"
([f start & options]
(let [opts (when options (apply assoc {} options))
tol (or (:tol opts) 1E-4)
dx (or (:dx opts) (mult start tol))
p (count start)
e (to-list (identity-matrix p))]
(fn [theta x]
(reduce bind-columns
(for [i (range p)]
(let [h (mult (nth e i) dx)]
(div
(map + (map (partial f (minus theta (mult 2 h))) x)
(map - (mult 8 (map (partial f (minus theta h)) x)))
(mult 8 (map (partial f (plus theta h)) x))
(map - (map (partial f (plus theta (mult 2 h))) x)))
(* 12 (nth dx i))))))))))```
Vars in incanter.optimize/gradient: * + - defn fn for let map nth partial reduce
Used in 0 other vars