- Calculator の場合 (組込函数が見つからないため自作する):
Define covpop(seq1,seq2)= Func :Local mean1,mean2,num :num:=dim(seq1) :mean1:=mean(seq1) :mean2:=mean(seq2) :Return ((sum((seq1-mean1)*(seq2-mean2)))/(num)) :EndFunc
実行結果:
- Nspired Lua で実行する場合:
function covPop(list1,list2) local temp1 = "{"..table.concat(list1, ",").."}" local temp2 = "{"..table.concat(list2, ",").."}" --return math.eval("covpop("..temp1..","..temp2..")") return math.eval(string.format("covpop(%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} function on.resize() print(covPop(s1, s2)) end
実行結果:
- 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 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 s1 = {1,2,3,4,5,6,7,8,9,10} s2 = {10,9,8,7,6,5,4,3,2,1} print(covPop(s1,s2))
実行結果:
―――――――――――――――――――――――――――――――
Wolfram の場合 (母共分散函数は有無不明のため自作):
s1={1,2,3,4,5,6,7,8,9,10}; s2={10,9,8,7,6,5,4,3,2,1}; meanS1=Mean[s1]; meanS2=Mean[s2]; N[Total[(s1-meanS1) (s2-meanS2)]/Length[s1]] N[Covariance[s1,s2]]
実行結果:
(Covariance[] は不偏標本共分散)
―――――――――――――――――――――――――――――――
Excel の場合:
実行結果:
(=COVARIANCE.S() は不偏標本共分散)