javaclojureincanter

Getting the mean and covariance matrix from array in clojure using incanter


I am trying to get the array mean and covariance matrix from a data structure that is 1000x2 using incanter.

My test case looks as such

(ns test.mean-cov
  (:require clojure.string
            [incanter.core :as in-core]
            [incanter.stats :as in-stats])
  (:use clojure.java.io))

(def test-mat [[1 2] [2 2]])

(in-stats/mean test-mat)

which I am running in LighTable.

The error I get is this:

java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to java.lang.Number
        Numbers.java:1104 clojure.lang.Numbers.double_array
           stats.clj:1492 incanter.stats/mean

I cannot quite say that I understand this, and unfortunately my Googling is not helping. Could someone offer a simple explanation and solution?

Naturally one awful way of doing it would be

(def c1 (first (transpose test-mat)))
(def c2 (second (transpose test-mat)))

(def data-cov-mat [[(in-stats/covariance c1 c1) (in-stats/covariance c1 c2)] [(in-stats/covariance c2 c1) (in-stats/covariance c2 c2)]])

Full disclosure: I am new to clojure.

Thanks


Solution

  • For matrix covariance you need to use the following:

    (in-stats/covariance (in-core/matrix test-mat))
    

    and for the matrix mean vector:

    (mapv in-stats/mean (in-core/trans test-mat))