Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to edit the lhsnorm function so that I can obtain Latin Hypercube sample from a Normally distributed set of data. The lhsnorm function is as follows:

function [X,z] = lhsnorm(mu,sigma,n,dosmooth)
%LHSNORM Generate a latin hypercube sample with a normal distribution

% Generate a random sample with a specified distribution and
% correlation structure -- in this case multivariate normal
z = mvnrnd(mu,sigma,n);

% Find the ranks of each column
p = length(mu);
x = zeros(size(z),class(z));
for i=1:p
   x(:,i) = rank(z(:,i));
end

% Get gridded or smoothed-out values on the unit interval
if (nargin<4) || isequal(dosmooth,'on')
   x = x - rand(size(x));
else
   x = x - 0.5;
end
x = x / n;

% Transform each column back to the desired marginal distribution,
% maintaining the ranks (and therefore rank correlations) 
for i=1:p
   x(:,i) = norminv(x(:,i),mu(i), sqrt(sigma(i,i)));
end
X = x;

function r=rank(x)

% Similar to tiedrank, but no adjustment for ties here
[sx, rowidx] = sort(x);
r(rowidx) = 1:length(x);
r = r(:);
I am editing the code as is shown below:

function [X] = lhsnorm_sid(z,dosmooth)

[muhat,sigmahat] = normfit(z);

z = z;
p = length(muhat);
x = zeros(size(z),class(z));
s = size(z);
n = s(1,1);

for i=1:p
   x(:,i) = rank(z(:,i));
end

if (nargin<4) || isequal(dosmooth,'on')
   x = x - rand(size(x));
else
   x = x - 0.5;
end
x = x / n;

for i=1:p
   x(:,i) = norminv(x(:,i),muhat(i), sqrt(sigmahat(i,i)));
end
X = x;

function r=rank(x)

[sx, rowidx] = sort(x);
r(rowidx) = 1:length(x);
r = r(:);


However, I end up with Latin Hypercube values which are way off what is expected. I also tried directly substituting z with d(1,: ) which contains a Normally distributed set of data, however the Latin Hypercube I obtained did not contain any of the values in d(1,: )
Thanks
Posted
Updated 6-Mar-14 9:43am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900