Question

New to Incanter, and was wondering what a vectorized solution to creating a matrix based on the results of the pair-wise product of two lists, would look like. To be clearer, I have two lists that I create with

(def x (pdf-poisson (range 4) :lambda 2.2))
(def y (pdf-poisson (range 4) :lambda 1.5)).

I would now like a 4x4 matrix M such that M(1,1) is the product of x(1) and y(1), M(1,2) is the product of x(1) and y(2) etc.

Taking the outer product in Octave is easy, so was hoping that Incanter supported this also.

I can easily hand code this by mapping a function across the vectors, but wanted an idiomatic or vectorized approach, if that is possible.

Thanks, JT

Was it helpful?

Solution

Just found the kronecker function in Incanter. The Kronecker product of the vectors is what I needed. So

(kronecker y-poisson x-poisson)

OTHER TIPS

Ended with this :-

(def x-poisson (pdf-poisson (range 4) :lambda 2.2))
(def y-poisson (pdf-poisson (range 4) :lambda 1.5))

(defn- poisson-mapper
  "Takes a list of values from the PDF, and returns a closure that will multiply each
  value in the list by p. Used to create the probability matrix"
  [p_list]
  (fn [p] (map #(* p %) p_list))) 

(def x-mapper (poisson-mapper x-poisson))

(def probabiliy-matix (matrix (map #(x-mapper %) y-poisson)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top