   "returns the Hansen (1992) test statistic; number of first-order
   conditions `num-foc`accounts for the demeaned residuals, intercept,
   and time-step introduced by `first-order-conditions`"
-  [y & x]
+  [y]
   1)
 
 (defn lengthening-ts

;; after: (time (dotimes [_ 100] (dorun (expt-residuals y X 2))))
;; "Elapsed time: 320.351 msecs"

(defn first-order-conditions
  "returns a matrix with residual weighted cofactors (incl. constant
  vector) and deviations from mean; corresponds to first-order
  conditions $f_t$ in the following reference: Hansen, B. (1992)
  Testing for Parameter Instability in Linear Models, Journal for
  Policy Modeling, 14(4), pp. 517-533"
  [y]
  (let [ones (i/matrix (repeat (count y) 1))
        X (i/matrix (utils/idx y))
        resid (linear-residuals y X)
        sq-resid (i/matrix (expt-residuals 2 resid))
        mu (s/mean sq-resid)]
    (i/bind-columns (i/mult resid X)
                    (i/mult resid ones)
                    (i/minus sq-resid mu))))

    ;; (vector (map * resid X)
            ;; (map * resid (repeat (count y) 1))
            ;; (map #(- % (utils/average sq-resid)) sq-resid))

;; (count (last (first-order-conditions ndvi))) => 271
;; (count (first (first-order-conditions ndvi))) => 271
;; (first (last (first-order-conditions ndvi))) => -1352787.304306197
;; (time (dotimes [_ 100] (dorun (first-order-conditions y))))
;; => "Elapsed time: 639.449 msecs"
;; after: (time (dotimes [_ 100] (dorun (first-order-conditions y))))
;; => "Elapsed time: 253.278 msecs"


;; (defn outer-product2 [coll] (i/matrix (flatten (map #(scale % coll) coll))))
(defn hansen-mats
  "returns the matrices of element-wise sums of (1) the first-order
  conditions, and (2) the cumulative first-order conditions.  This is
  only an intermediate step in the calculation of the Hansen (1992)
  test for parameter instability in linear models."
  [y & x]
  (let [foc (apply first-order-conditions y x)
        foccum (map i/cumulative-sum foc)]
    [(element-sum (map outer-product (transpose foc)))
     (element-sum (map outer-product (transpose foccum)))]))

;; (defn hansen-stat
;;   "returns the Hansen (1992) test statistic; number of first-order

        [cascalog.api]
        [clojure.math.numeric-tower :only (sqrt floor abs expt)]
        [forma.trends.filter])
  (:import [org.jblas FloatMatrix MatrixFunctions Solve DoubleMatrix])
  (:require [forma.utils :as utils]
            [incanter.core :as i]
            [incanter.stats :as s]
