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