local d = require "distribution"

d6 = d.uniform(6)
d20 = d.uniform(20)

foo = 2*d6 + d20

-- The basic summary method gives a sorted list of all the outcomes
-- and a summary of statistical values

print("2d6 + d20")
print(foo:summary())

-- Note that tostring(foo) and print(foo) will automatically call summary

-- To check the content of the distribution you can use
-- the outcomes member, which is a table of probabilities

print("Outcomes")

for outcome,probability in pairs(foo.outcomes) do
	print(outcome .. ": " .. probability * 100 .. "%")
end

print()

-- The method sorted_outcomes return a sorted list of pairs {outcome, probability}
-- If the outcomes' type is not number, the list is not sorted

print("Outcomes again")

for _,v in ipairs(foo:sorted_outcomes()) do
	print(v[1] .. ": " .. v[2])
end

print()

-- The Cumulative Distribution Function of distribution D is the probability that D <= x

print("CDF")

for _,v in ipairs(foo:cdf()) do
	print("foo <= " .. v[1] .. ": " .. v[2])
end

print()

-- You can also use the shortcut print_cdf

print("CDF again")

foo:print_cdf()

print()

-- Finally, individual statistical methods

print("Average", foo:average())
print("Standard deviation", foo:deviation())
print("Median", foo:median())
print("Median absolute deviation", foo:median_absolute_deviation())

-- And percentiles/quartiles/deciles/etc. via the nth_iles method

print("Thirds", table.concat(foo:nth_iles(3), ", "))
print("Quartiles", table.concat(foo:nth_iles(4), ", "))
print("Deciles", table.concat(foo:nth_iles(10), ", "))
print("Percentiles", table.concat(foo:nth_iles(100), ", "))