• incanter

1.2.3-SNAPSHOT

# hessian

## incanter.optimize

• (hessian f start & options)
Returns a function that calculates an approximation to the Hessian matrix
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 with p*(p+1)/2 columns, one for each unique entry in
the Hessian 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))

(time (def hess (hessian f start)))
(time (doall (hess start x)))

### 0 Examples top

Log in to add / edit an example.

### See Also top

Log in to add a see also.

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

```(defn hessian
" Returns a function that calculates an approximation to the Hessian matrix
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 with p*(p+1)/2 columns, one for each unique entry in
the Hessian 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))

(time (def hess (hessian f start)))
(time (doall (hess start x)))

"
([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) j (range p) :when (<= i j)]
(let [hi (mult (nth e i) dx)
hj (mult (nth e j) dx)
hij (mult (plus (nth e i) (nth e j)) dx)]
(div
(map +
(map - (map (partial f theta) x)
(map (partial f (plus theta hi)) x)
(map (partial f (plus theta hj)) x))
(map (partial f (plus theta hij)) x))
(* (nth dx i) (nth dx j))))))))))```
Vars in incanter.optimize/hessian: * + - defn fn for let map nth partial reduce
Used in 0 other vars