File Exchange

image thumbnail

lhsgeneral(pd,corre​lation,n)

version 1.2.0.0 (3.52 KB) by Iman
This function implements Latin hypercube sampling method for correlated random variables.

8 Downloads

Updated 07 Mar 2017

View License

This function is developed to generate different realizations of correlated random variables using Latin hypercube sampling method. To learn more about this function, please watch the following tutorial:
https://www.youtube.com/watch?v=pVgKwqN621s


Please watch the following short tutorials to quickly learn random sampling (Monte Carlo Vs. Latin hypercube sampling):
Random Sampling - Tutorial 1 - Introduction
https://www.youtube.com/watch?v=kKpqGcCUFF8&index=1&list=PLJ-OcUCIty7fJQiAdjMjBiCbBkT563iSY


Random Sampling - Tutorial 2 - Monte Carlo
https://www.youtube.com/watch?v=M9hjdX97Z3s&index=2&list=PLJ-OcUCIty7fJQiAdjMjBiCbBkT563iSY

Random Sampling - Tutorial 3 - Monte Carlo Example
https://www.youtube.com/watch?v=mATm9eCtJaQ&index=3&list=PLJ-OcUCIty7fJQiAdjMjBiCbBkT563iSY

Random Sampling - Tutorial 4 - Latin Hypercube Sampling
https://www.youtube.com/watch?v=H0RZ1uezuuw&index=4&list=PLJ-OcUCIty7fJQiAdjMjBiCbBkT563iSY

Random Sampling - Tutorial 5 - Monte Carlo Vs. Latin Hypercube Sampling
https://www.youtube.com/watch?v=2u4YlhrrDQE&index=5&list=PLJ-OcUCIty7fJQiAdjMjBiCbBkT563iSY

Please subscribe to my signal processing channel to see more tutorials:
https://www.youtube.com/channel/UCVkatNMgkEdpWLhH0kBqqLw

Acknowledgements:
This submission is developed for 2060 Project at the Institute for Integrated Energy Systems at the University of Victoria, Canada. The project has been funded by Pacific Institute for Climate Solutions, Natural Resources Canada and Natural Sciences and Engineering Research Council of Canada. This funding is gratefully acknowledged.

Comments and Ratings (26)

kian

Very useful

Iman

Thanks a lot, Amirreza

very useful

Congratulations Iman. I found it very helpful.

I have two questions though.

At line#71 you have: dependent_samples=independent_samples*(P*inv(Q)).';
Why do you multiply with inv(Q)?

The depedent_samples have a correlation pretty close to the defined one(-0.8 in your case), corr(dependent_samples(:,1),dependent_samples(:,2)) but it is different than the correlation of the results, actual_correlation.
Why is this the case

Really, it is good code for helping me.

Iman

Hey swelltime. Thank you very much for your feedback. I don't have a citable paper for this as it is common knowledge. You can cite this work as:

Moazzen, Iman (2016). lhsgeneral (https://www.mathworks.com/matlabcentral/fileexchange/56384), MATLAB Central File Exchange. Retrieved June 9, 2017.

swelltime

many thanks for this awesome tool! do you have a citable paper or is this common knowledge?

arnold

Hi Iman,
it would be even nicer if one could map the latin hypercube to an arbitrary distribution that was given by an input vector! For example, I quite often need one variable to be logarithmic... yet makedist can't provide this.

What's also often needed for my simulations are discrete steps in one dimension. I usually discretize each dimension then so that [n1...ni] is the number of steps in each dimension. I haven't found a proper way to optimise latin hypercube designs for these cases.

Iman

Thank you very much EAnane. You are awesome. I saw some people reporting the bug and wished to get it fixed asap but life has been hectic recently due to the new job and relocation. Anyway, thanks again for your help.

EAnane

Hi Iman,

thanks a lot for the code. I needed this badly.

@ arnold and I Summer:
I run into the same problem. I resolved it by specifying a 'mu' and 'sigma' for the icdf function. See matlab documentation for icdf
======
independent_samples(:,i) = icdf('Normal',prob,0,1);
======
By this you don't get NANs in the R vector.

arnold

same error trying to use either of your examples. Matlab version is 2016b
=============
Error using chol
Matrix must be positive definite.

Error in lhsgeneral (line 65)
Q=chol(R).';
% Cholesky decomposition of the actual
correlation matrix

I Summer

Hi Iman,

Thank you for creating this code.

I am getting this error:
Error using chol
Matrix must be positive definite.

Error in lhsgeneral (line 70)
Q=chol(R).' ; % Cholesky decomposition of the actual
correlation matrix

I see R is NAN, which depends on the independent samples. Could you help me with this?

Thank you!

Iman

Hey Malav. Sorry for the late response. I didn't get your message in my email box which is weird! Could you please send me your MATLAB code? I will take a look at it ASAP. Thank you.

Malav Shah

Iman, I was using your code to generate latin hypercube samples for generelised extreme value distribution. While using your code, it was showing me following error message:

Error using *
Inner matrix dimensions must agree.

Error in lhsgeneral (line 71)
dependent_samples=independent_samples*(P*inv(Q)).'; % transformation matrix which adds
dependency between normal variables

Please help to eliminate this error as soon as possible. Thank you.

Malav Shah

If you can show me for any random variable other than normal, then also that will work. Please reply asap

Malav Shah

Can you please help me in using this code only for one independent distribution? I have only one distribution since there is only one uncertain parameter. I think I don't need to use correlation in case of only one random variable but when I use your code with no correlation, it shows me error. If you can help me in case of using this code only for one distribution i.e. generalized extreme value distribution, then this will be a big help for me. Thank you in advance.

Taotao Zhou

Iman

Hi Mimo. Thank you for your positive feedback. The thread is a long page and I am not exactly sure what your question is. My MATLAB function is capable of generating 10 uncorrelated variables with different distribution. Please send me the distribution types, and I will get back to you ASAP. Cheers.

Mimo

Hi Iman,
Many thanks for providing this useful video tutorials. My issue is I wanna do a LHS sampling of 10 totally UNCORRELATED variables but the variables are all types of different distributions (uniform, normal and lognormal). I saw this example before yours :
http://au.mathworks.com/matlabcentral/newsreader/view_thread/279955
Can you advise if this enough for the sampling I aim for? I am just doubted if it is required (and how) to define the variables distribution before performing the sampling or not.

cheers

Updates

1.2.0.0

bug fix. Very special thanks to EAnane for finding and fixing the issue!

1.2.0.0

description update.

1.2.0.0

description update.

1.2.0.0

description update.

1.2.0.0

image update

1.2.0.0

adding youtube channel

1.2.0.0

description update.

1.2.0.0

description update.

1.2.0.0

Description update.

1.2.0.0

description update.

1.2.0.0

title update

1.2.0.0

link update.

1.2.0.0

Description Update.

1.2.0.0

Big update on the algorithm used.

1.1.0.0

Description Update.

1.0.0.0

Description update.

1.0.0.0

Description update.

1.0.0.0

Description update.

1.0.0.0

Description update.

1.0.0.0

Description update.

1.0.0.0

Videos added.

1.0.0.0

Title change.

1.0.0.0

Description Update.

1.0.0.0

File Update.

1.0.0.0

Description Update.

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux