Giter Club home page Giter Club logo

lhsmdu's Introduction

LHS-MDU

Basics

This is a package for generating latin hypercube samples with multi-dimensional uniformity.

To use, simply do:

>>> import lhsmdu
>>> k = lhsmdu.sample(2, 20) # Latin Hypercube Sampling with multi-dimensional uniformity

This will generate a nested list with 2 variables, with 20 samples each.

To plot and see the difference between Monte Carlo and LHS-MDU sampling for a 2 dimensional system:

>>> l = lhsmdu.createRandomStandardUniformMatrix(2, 20) # Monte Carlo sampling
>>> import matplotlib.pyplot as plt
>>> 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], col="g", label="LHS-MDU")
>>> plt.scatter(l[0], l[1], col="r", label="MC")
>>> plt.grid()
>>> plt.show()

You can use the strata generated by the algorithm to sample again, if you so desire. For this, you can do:

>>> m = lhsmdu.resample()
>>> n = lhsmdu.resample()
>>> o = lhsmdu.resample()

This will again generate the same number of samples as before, a nested list with 2 variables, with 20 samples each.

You can plot these together and see the sampling from the strata:

>>> 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.title("LHS-MDU")
>>> plt.scatter(k[0], k[1], c="g", label="sample 1")
>>> plt.scatter(m[0], m[1], c="r", label="resample 2")
>>> plt.scatter(n[0], n[1], c="b", label="resample 3")
>>> plt.scatter(o[0], o[1], c="y", label="resample 4")
>>> plt.grid()
>>> plt.show()

Alternatively, you can choose to get new strata each time, and see the sampling hence:

>>> p = lhsmdu.sample(2, 20) # Latin Hypercube Sampling with multi-dimensional uniformity
>>> q = lhsmdu.sample(2, 20) # Latin Hypercube Sampling with multi-dimensional uniformity
>>> r = lhsmdu.sample(2, 20) # Latin Hypercube Sampling with multi-dimensional uniformity
>>> 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.title("LHS-MDU")
>>> plt.scatter(k[0], k[1], c="g", label="sample 1")
>>> plt.scatter(p[0], p[1], c="r", label="sample 2")
>>> plt.scatter(q[0], q[1], c="b", label="sample 3")
>>> plt.scatter(r[0], r[1], c="y", label="sample 4")
>>> plt.grid()
>>> plt.show()

Sampling from arbitrary CDFs

After uniformly distributed samples have been generated from LHSMDU, you can convert these to samples from arbitrary distributions using inverse tranform sampling. In this, the CDF [0,1] of the distribution of interest is inverted, and then data points corresponding to the uniformly sampled points are picked up. To do this, you must have a rv_contiuous or rv_discrete distribution instance taken from scipy.stats. You can also use frozen distributions (after setting loc and scale parameters). Following is an example for normal distribution.:

>>> import scipy.stats.distributions as ssd
>>> p = ssd.norm
>>> new_samples = lhsmdu.inverseTransformSample(p, k[0])
>>> plt.hist(lhsmdu.inverseTransformSample(p, k[0]))
>>> plt.show()

lhsmdu's People

Contributors

sahilm89 avatar

Stargazers

Zhihao Liu avatar

Watchers

James Cloos avatar John Lawson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.