mardi 11 juin 2019

Why does python's lambda function not resample from a distribution?

Python's lambda function does not resample from a distribution when applied on an array; e.g. when using:

f1 = lambda x: -3 + 0.75*x + numpy.random.randn()

The proper way would be to use a map() and thereby 'map' the lambda function on the array.

However, one can easily apply a lambda function on an array. I'm asking how that's working but the function is not resampling.

a MVP:

import numpy
numpy.random.seed(1)

f1 = lambda x: -3 + 0.75*x + numpy.random.randn()  # dummy with random part
f2 = lambda x: -3 + 0.75*x  # dummy w/o random part 

# dummy data
x = numpy.arange(10)

f1(x) - f2(x)
>> array([1.62434536, 1.62434536, 1.62434536, 1.62434536, 1.62434536,
1.62434536, 1.62434536, 1.62434536, 1.62434536, 1.62434536])

numpy.var(f1(x) - f2(x))  # almost 0

So, I'd expect a variance greater close to 1. The example is an evidence that the lambda function is just sampling on the first element and then does not change the random part anymore.

Thanks for your explanation !!




Aucun commentaire:

Enregistrer un commentaire