11 個のキーワードで学ぶ統計学入門 10 / 正規乱数

  • Calculator の場合:

構文: randNorm(平均, 標準偏差 [, 試行回数])
実行結果:
f:id:ti-nspire:20180510082512p:plain:w400
f:id:ti-nspire:20180510082540p:plain:w400
 

  • Nspired Lua で実行する場合:
function normRand(mean, sigma, trials)
   local trials = trials or 1
   return math.eval(string.format("randNorm(%s, %s, %s)", mean, sigma, trials))
end

function on.resize()
   var.store("b", normRand(0,1,580))
end

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

  • Lua だけで組む場合:

(1) Box–Muller 法

function normRandBM(size)
   local rnd = {}
   for i = 1, size, 2 do
      local x = math.random()
      local y = math.random()
      local temp1 = math.sqrt(-2 * math.log(x))
      local temp2 = 2 * math.pi * y
      local z1 = temp1 * math.cos(temp2)
      local z2 = temp1 * math.sin(temp2)
      rnd[#rnd+1] = z1
      if i >= size then 
         break
      end
      rnd[#rnd+1] = z2
   end
   return rnd
end

print(table.concat(normRandBM(0),", "))
print(table.concat(normRandBM(1),", "))
print(table.concat(normRandBM(2),", "))
print(table.concat(normRandBM(3),", "))
print(table.concat(normRandBM(4),", "))

実行結果:
f:id:ti-nspire:20180510092745p:plain
 
(2) 中心極限定理

function normRandCL(size)
   local rnd = {}
   for i = 1, size do
      local temp = 0
      for n = 1, 12 do
         temp = temp + math.random()
      end
      rnd[#rnd+1] = temp - 6
   end
   return rnd
end

print(table.concat(normRandCL(0),", "))
print(table.concat(normRandCL(1),", "))
print(table.concat(normRandCL(2),", "))
print(table.concat(normRandCL(3),", "))
print(table.concat(normRandCL(4),", "))

実行結果:
f:id:ti-nspire:20180510094837p:plain
――――――――――――――――――――――――――――――――――
Wolfram の場合:
不明。多分 RandomReal[NormalDistribution[平均, 標準偏差]]

rnd:=RandomReal[NormalDistribution[0,1]] ;
Table[rnd,{x,10000}];
Histogram[%]

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

――――――――――――――――――――――――――――――――――
Excel の場合:
構文: NORM.INV(RAND(), 平均, 標準偏差)