Mapytex/mapytex/stat/random_generator.py

92 lines
2.9 KiB
Python
Raw Normal View History

2019-10-13 19:01:31 +00:00
# /usr/bin/env python
2016-01-09 15:51:20 +00:00
# -*- coding:Utf-8 -*-
2016-01-12 08:14:44 +00:00
from random import randint, uniform, gauss, choice
2016-01-09 15:51:20 +00:00
2019-10-13 19:01:31 +00:00
def random_generator(
length,
distrib=gauss,
rd_args=(0, 1),
nbr_format=lambda x: round(x, 2),
v_min=None,
v_max=None,
exact_mean=None,
):
2016-01-09 15:51:20 +00:00
""" Generate a random list of value
:param length: length of the dataset
2016-01-12 08:14:44 +00:00
:param distrib: Distribution of the data set. It can be a function or string from ["randint", "uniform", "gauss", "choice"]
2016-01-09 15:51:20 +00:00
:param rd_args: arguments to pass to distrib
:param nbr_format: function which format value
:param v_min: minimum accepted value
:param v_max: maximum accepted value
:param exact_mean: if set, the last generated number will be create in order that the computed mean is exacly equal to "exact_mean"
2020-08-20 14:56:34 +00:00
>>> random_generator(10) # doctest: +SKIP
[-0.76, 0.46, 0.19, 0.08, -1.13, -0.5, 0.47, -2.11, 0.16, -1.05]
>>> random_generator(10, distrib = uniform, rd_args = (5, 10)) # doctest: +SKIP
[9.01, 5.32, 5.59, 8.8, 7.36, 6.9, 6.05, 7.44, 9.47, 6.95]
>>> random_generator(10, distrib = "uniform", rd_args = (5, 10)) # doctest: +SKIP
[7.85, 9.01, 5.32, 5.59, 8.8, 7.36, 6.9, 6.05, 7.44, 9.47]
>>> random_generator(10, v_min = 0) # doctest: +SKIP
[0.46, 0.19, 0.08, 0.47, 0.16, 0.87, 0.17, 1.79, 0.19, 1.12]
>>> random_generator(10, exact_mean = 0) # doctest: +SKIP
[-0.76, 0.46, 0.19, 0.08, -1.13, -0.5, 0.47, -2.11, 0.16, 3.14]
>>> random_generator(10, distrib = gauss, rd_args = (50,20), nbr_format = int) # doctest: +SKIP
[34, 59, 53, 51, 27, 40, 59, 7, 53, 28]
2016-01-09 15:51:20 +00:00
"""
# if exact_mean is set, we create automaticaly only length-1 value
2016-02-13 04:04:08 +00:00
if exact_mean is not None:
2016-01-09 15:51:20 +00:00
length = length - 1
# build function to test created values
2016-02-13 04:04:08 +00:00
if v_min is None:
2016-01-09 15:51:20 +00:00
v1 = lambda x: True
else:
v1 = lambda x: x >= v_min
2016-02-13 04:04:08 +00:00
if v_max is None:
2016-01-09 15:51:20 +00:00
v2 = lambda x: True
else:
v2 = lambda x: x <= v_max
2016-02-13 04:04:08 +00:00
validate = lambda x: v1(x) and v2(x)
2016-01-09 15:51:20 +00:00
# get distrib function
2016-02-13 04:04:08 +00:00
distribs = {
"gauss": gauss,
"uniform": uniform,
"randint": randint,
2019-10-13 19:01:31 +00:00
"choice": choice,
}
2016-01-09 15:51:20 +00:00
try:
distrib(*rd_args)
except TypeError:
distrib = distribs[distrib]
# building values
data = []
for _ in range(length):
valid = False
while not valid:
v = nbr_format(distrib(*rd_args))
valid = validate(v)
data.append(v)
# Build last value
2016-02-13 04:04:08 +00:00
if exact_mean is not None:
last_v = nbr_format((length + 1) * exact_mean - sum(data))
2016-01-09 15:51:20 +00:00
if not validate(last_v):
2016-02-13 04:04:08 +00:00
raise ValueError(
2019-10-13 19:01:31 +00:00
"Can't build the last value. Conflict between v_min/v_max and exact_mean"
)
2016-01-09 15:51:20 +00:00
data.append(last_v)
return data
2019-10-13 19:01:31 +00:00
2016-01-09 15:51:20 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2016-02-13 03:29:26 +00:00
# cursor: 16 del