Search code examples
luastatisticscorrelationtorch

Torch / Lua, how to pair together to arrays into a table?


I need to use the Pearson correlation coefficient in my Torch / Lua program. This is the function:

function math.pearson(a)
  -- compute the mean
  local x1, y1 = 0, 0
  for _, v in pairs(a) do
  x1, y1 = x1 + v[1], y1 + v[2]
  print('x1 '..x1);
  print('y1 '..y1);
  end
  -- compute the coefficient
  x1, y1 = x1 / #a, y1 / #a
  local x2, y2, xy = 0, 0, 0
  for _, v in pairs(a) do
    local tx, ty = v[1] - x1, v[2] - y1
    xy, x2, y2 = xy + tx * ty, x2 + tx * tx, y2 + ty * ty
  end

  return xy / math.sqrt(x2) / math.sqrt(y2)
end

This function wants an input couple table that can be used in the pairs() function. I tried to submit to it the right input table but I was not able to get anything working.

I tried with:

z = {}
a = {27, 29, 45, 98, 1293, 0.1}
b = {0.0001, 0.001, 0.32132, 0.0001, 0.0009}
z = {a,b}

But unfortunately it does not work. It will compute pairs between the first elements of b, while I want it to compute the correlation between a and b. How could I do?

Can you provide a good object that may work as input to the math.pearson() function?


Solution

  • If you are using this implementation, then I think it expects a different parameter structure. You should be passing a table of two-value tables, as in:

    local z = {
      {27, 0.0001}, {29, 0.001}, {45, 0.32132}, {98, 0.0001}, {1293, 0.0009}
    }
    print(math.pearson(z))
    

    This prints -0.25304101592759 for me.