11 個のキーワードで学ぶ統計学入門 6 / 相関 (correlation)

  • Calculator の場合:

構文: corrMat(リスト, リスト)
実行結果:
f:id:ti-nspire:20180509101156p:plain:w400
 

  • Nspired Lua で実行する場合:
function corrMat(list1,list2)
   local temp1 = "{"..table.concat(list1, ",").."}"
   local temp2 = "{"..table.concat(list2, ",").."}"
   --return math.eval("corrMat("..temp1..","..temp2..")")
   return math.eval(string.format("corrMat(%s, %s)", temp1, temp2))
end

s1 = {1,2,3,4,5,6,7,8,9,10}
s2 = {10,9,8,7,6,5,4,3,2,1}
s3 = {1,-3,1,-1,4,0,3,0,-5,2}
function on.resize()
   print(corrMat(s1, s2)[1][2])
   print(corrMat(s1, s1)[1][2])
   print(corrMat(s1, s3)[1][2])
end

実行結果:
f:id:ti-nspire:20180509101720p:plain
 

  • Lua だけで組む場合:
function sum(list)
   local temp = 0
   for i = 1, #list do
      temp = temp + list[i]
   end
   return temp
end
function mean(list)
   return sum(list) / #list
end
function varPop(list)
   local num  = #list
   local mean = sum(list) / num
   local sum2 = 0
   for i = 1, num do
      sum2 = sum2 + (list[i] - mean)^2
   end
   return sum2 / num
end
function stDevPop(list)
   return math.sqrt(varPop(list))
end
function covPop(list1, list2)
   local num = #list1
   local mean1 = mean(list1)
   local mean2 = mean(list2)
   local temp = 0
   for i = 1, num do
      temp = temp + (list1[i] - mean1) * (list2[i] - mean2)
   end
   return temp / num   
end
function correlation(list1, list2)
   return covPop(list1, list2) / (stDevPop(list1) * stDevPop(list2))
end

s1 = {1,2,3,4,5,6,7,8,9,10}
s2 = {10,9,8,7,6,5,4,3,2,1}
s3 = {1,-3,1,-1,4,0,3,0,-5,2}
function on.resize()
   print(correlation(s1, s2))
   print(correlation(s1, s1))
   print(correlation(s1, s3))
end

実行結果:
f:id:ti-nspire:20180509102409p:plain
―――――――――――――――――――――――――――――――――
Wolfram の場合:
構文: Correlation[リスト, リスト]
実行結果:
f:id:ti-nspire:20180509102745p:plain:w400
―――――――――――――――――――――――――――――――――
Excel の場合:
構文: CORREL(範囲, 範囲)
実行結果:
f:id:ti-nspire:20180509103227p:plain:w400