jeudi 26 janvier 2017

Using SciPy or NumPy, how can sample experimental data be generated using a function?

Using ROOT, sample experimental data can be generated using a function in a way like the following:

histogram.FillRandom("gaus", 10000)

Here, 10,000 data points are generated using a Gaussian function and stored in a histogram.

How can something similar be done using SciPy or NumPy? Let's say I have a function defined and I want to use it to generate 10,000 sample experimental data points (obviously introducing noise) using it:

from __future__ import division

import numpy
import pyprel

def hardscaled_log_normal(
    x,
    mu      = 0.109695958342,
    sigma   = 1.32148811307,
    scale_x = 0.0137899935922,
    scale_y = 9.26895741647,
    ):

    return scale_y * 1 / (numpy.sqrt(2 * numpy.pi) * sigma * scale_x * x) *\
           numpy.exp(-abs(numpy.log(scale_x * x) - mu) ** 2/(2 * sigma ** 2))

data_x = [
    2.5,
    7.5,
    12.5,
    17.5,
    23.5,
    27.5,
    47.5,
    67.5,
    113.5,
    137.5,
    180,
    197.5
]

data_y = [hardscaled_log_normal(x) for x in data_x]

print(pyprel.Table(
    contents = [["x", "y"]] + [[x, y] for x, y in zip(data_x, data_y)]
))




Aucun commentaire:

Enregistrer un commentaire