Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latin hypercube sampling with python

I would like to sample a distribution defined by a function in multiple dimensions (2,3,4):

f(x, y, ...) = ...

The distributions might be ugly, non standard (like a 3D spline on data, sum of gaussians ect.). To this end I would like to uniformly sample the 2..4 dimensional space, and than with an additional random number accept or reject the given point of the space into my sample.

  1. Is there a ready to use python lib for this purpose?

  2. Is there python lib for generating the points in this 2..4 dimensional space with latin hypercube sampling, or with other uniform sampling method? Bruteforce sampling with independent random numbers usually results in more and less dense regimes of the space.

  3. if 1) and 2) doesn't exist, is there anybody who is kind enough to share his implementation for the same or similar problem.

I'll use it in a python code, but links to other solutions are also acknowledged.

like image 399
user2393987 Avatar asked Oct 01 '14 08:10

user2393987


4 Answers

Latin Hypercube sampling is now part of SciPy since version 1.7. See the doc.

from scipy.stats.qmc import LatinHypercube

engine = LatinHypercube(d=2)
sample = engine.random(n=100)
like image 168
tupui Avatar answered Nov 04 '22 15:11

tupui


Now the pyDOE library provides a tool to generate Latin-hypercube-based samples.

https://pythonhosted.org/pyDOE/randomized.html

to generate samples over n dimensions:

lhs(n, [samples, criterion, iterations])

where n is the number of dimensions, samples as the total number of the sample space.

like image 28
Qiangzini Avatar answered Nov 11 '22 11:11

Qiangzini


I guess this is a late answer, but this is also for future visitors. I have just put up an implementation of multi-dimensional uniform Latin Hypercube sampling on git. It is minimal, but very easy to use. You can generate uniform random variables sampled in n dimensions using Latin Hypercube Sampling, if your variables are independent. Below is an example plot comparing Monte Carlo and Latin Hypercube Sampling with Multi-dimensional Uniformity (LHS-MDU) in two dimensions with zero correlation.

import lhsmdu
import matplotlib.pyplot as plt
import numpy

l = lhsmdu.sample(2,10) # Latin Hypercube Sampling of two variables, and 10 samples each.
k = lhsmdu.createRandomStandardUniformMatrix(2,10) # Monte Carlo Sampling

fig = plt.figure()
ax = fig.gca()
ax.set_xticks(numpy.arange(0,1,0.1))
ax.set_yticks(numpy.arange(0,1,0.1))
plt.scatter(k[0], k[1], color="b", label="LHS-MDU")
plt.scatter(l[0], l[1], color="r", label="MC")
plt.grid()
plt.show()

MCS versus LHS

like image 10
Sahil M Avatar answered Nov 11 '22 09:11

Sahil M


Here is an update of Sahil M's answer for Python 3 (update from Python 2 to Python 3 and some minor code changes to match code and figure):

import lhsmdu
import matplotlib.pyplot as plt
import numpy

l = lhsmdu.sample(2,10) # Latin Hypercube Sampling of two variables, and 10 samples each.
k = lhsmdu.createRandomStandardUniformMatrix(2,10) # Monte Carlo Sampling

fig = plt.figure()
ax = fig.gca()
ax.set_xticks(numpy.arange(0,1,0.1))
ax.set_yticks(numpy.arange(0,1,0.1))
plt.scatter([k[0]], [k[1]], color="r", label="MC")
plt.scatter([l[0]], [l[1]], color="b", label="LHS-MDU")
plt.legend()
plt.grid()
plt.show()

code visual output

I once encountered a Python memory error running this script. Any suggestions why this could happen or how to changes the script so it doesn't happen anymore in the future?

like image 3
Matthi9000 Avatar answered Nov 11 '22 10:11

Matthi9000