Mòdul:SimpleStats
Aparença

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]
Simple Statistical functions.
- stats.Mean (t)
- stats.WinsorizedMean (t), modified for include values between 5 and 9.
- stats.Mode (t)
- stats.Median (t)
- stats.StandardDeviation (t)
- stats.MaxMin (t)
Where t is a table.
Funcions estadístiques descriptives
[modifica]Funció | Correspondència |
---|---|
stats.Mean (t) | Mitjana |
stats.WinsorizedMean (t) | Mitjana winsoritzada |
stats.Mode (t) | Moda |
stats.Median (t) | Mediana |
stats.StandardDeviation (t) | Desviació estàndard o tipus |
stats.MaxMin (t) | Màxim i mínim |
On t és una taula.
Use
[modifica]local SS = require "Module:SimpleStats" function p.YourFunction() tab = {2,3,4} return stats.mean (tab) end
-- Small stats library --
----------------------------------------------
stats={}
-- Get the Mean value of a table
function stats.Mean(t)
local sum = 0
local count = 0
for k,v in pairs(t) do
if type(v) == 'number' then
sum = sum + v
count = count + 1
end
end
return (sum / count)
end --Mean
-- Get the Mode of a table. Returns a table of values.
-- Works on anything (not just numbers).
function stats.Mode(t)
local counts={}
for k, v in pairs(t) do
if counts[v] == nil then
counts[v] = 1
else
counts[v] = counts[v] + 1
end
end
local biggestCount = 0
for k, v in pairs(counts) do
if v > biggestCount then
biggestCount = v
end
end
local temp = {}
for k,v in pairs(counts) do
if v == biggestCount then
table.insert(temp, k)
end
end
return temp
end --Mode
local function CheckAndSort(t)
local temp = {}
-- deep copy table so that when we sort it, the original is unchanged
-- also weed out any non numbers
for k,v in pairs(t) do
if type(v) == 'number' then
table.insert(temp, v)
end
end
table.sort(temp)
return temp
end --CheckAndSort
-- Get the Median of a table.
function stats.Median(t)
local temp = CheckAndSort(t)
-- If we have an even number of table elements or odd.
if math.fmod(#temp,2) == 0 then
-- return Mean value of middle two elements
return (temp[#temp/2] + temp[(#temp/2)+1]) / 2
else
-- return middle element
return temp[math.ceil(#temp/2)]
end
end --Median
-- Get the winsorized Mean of a table.
function stats.WinsorizedMean(t)
local temp = CheckAndSort(t)
N = #temp
if N >= 10 then
local EPS = math.floor(N/10)
for I = 1, EPS do
temp[I] = temp[EPS+1]
end
for I = N, N-EPS, -1 do
temp[I] = temp[N-EPS]
end
elseif N >= 5 then
temp[1] = (temp[1]+temp[2])/2
temp[N] = (temp[N-1]+temp[N])/2
end
return stats.Mean(temp)
end --
-- Get the standard deviation of a table
function stats.StandardDeviation(t)
local m
local vm
local sum = 0
local count = 0
local result
m = stats.Mean(t)
for k,v in pairs(t) do
if type(v) == 'number' then
vm = v - m
sum = sum + (vm * vm)
count = count + 1
end
end
result = math.sqrt(sum / (count-1))
return result
end --StandardDeviation
-- Get the max and min for a table
function stats.MaxMin(t)
local max = -math.huge
local min = math.huge
for k,v in pairs(t) do
if type(v) == 'number' then
max = math.max(max, v)
min = math.min(min, v)
end
end
return max, min
end --MaxMin
return stats