Matlab: Latin Hypercube - matlab

Is there a way to create a Latin Hypercube from a particular set of data? I have d(1,:) = 3*t +0.00167*randn(1,1000);. Is there a way for me to create a Latin Hypercube from the elements in d(1,:)?
Thanks a lot

An edit of the lhsnorm function can probably answer your question.
In matlab : edit lhsnorm :
function [X,z] = lhsnorm(mu,sigma,n,dosmooth)
%LHSNORM Generate a latin hypercube sample with a normal distribution
% X=LHSNORM(MU,SIGMA,N) generates a latin hypercube sample X of size
% N from the multivariate normal distribution with mean vector MU
% and covariance matrix SIGMA. X is similar to a random sample from
% the multivariate normal distribution, but the marginal distribution
% of each column is adjusted so that its sample marginal distribution
% is close to its theoretical normal distribution.
%
% X=LHSNORM(MU,SIGMA,N,'ONOFF') controls the amount of smoothing in the
% sample. If 'ONOFF' is 'off', each column has points equally spaced
% on the probability scale. In other words, each column is a permutation
% of the values G(.5/N), G(1.5/N), ..., G(1-.5/N) where G is the inverse
% normal cumulative distribution for that column''s marginal distribution.
% If 'ONOFF' is 'on' (the default), each column has points uniformly
% distributed on the probability scale. For example, in place of
% 0.5/N we use a value having a uniform distribution on the
% interval (0/N,1/N).
%
% [X,Z]=LHSNORM(...) also returns Z, the original multivariate normal
% sample before the marginals are adjusted to obtain X.
%
% See also LHSDESIGN, MVNRND.
% Reference: Stein, M. L. (1987). Large sample properties of simulations
% using Latin hypercube sampling. Technometrics, 29, 143-151. Correction,
% 32, 367.
% Copyright 1993-2010 The MathWorks, Inc.
% $Revision: 1.1.8.1 $ $Date: 2010/03/16 00:15:10 $
% 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) from the
% original random sample
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(:);
In your case you already have your distribution z in the code above and you also have mu, sigma and 'n' (the size of your distribution), just replace them and you should be able to create your Latin Hypercube.

There is a function in matlab for the creation of latin hypercube samples: lhsdesign(which lets you specifiy your hypercube) or lhsnorm(which uses a normal distributed one) . both are found in the statistics toolbox.

Related

Demeaned Returns for Covariance (Matlab)

I've got this code:
function [sigma,shrinkage]=covMarket(x,shrink)
% function sigma=covmarket(x)
% x (t*n): t iid observations on n random variables
% sigma (n*n): invertible covariance matrix estimator
%
% This estimator is a weighted average of the sample
% covariance matrix and a "prior" or "shrinkage target".
% Here, the prior is given by a one-factor model.
% The factor is equal to the cross-sectional average
% of all the random variables.
% The notation follows Ledoit and Wolf (2003)
% This version: 04/2014
% de-mean returns
t=size(x,1);
n=size(x,2);
meanx=mean(x);
x=x-meanx(ones(t,1),:);
xmkt=mean(x')';
sample=cov([x xmkt])*(t-1)/t;
covmkt=sample(1:n,n+1);
varmkt=sample(n+1,n+1);
sample(:,n+1)=[];
sample(n+1,:)=[];
prior=covmkt*covmkt'./varmkt;
prior(logical(eye(n)))=diag(sample);
if (nargin < 2 | shrink == -1) % compute shrinkage parameters
c=norm(sample-prior,'fro')^2;
y=x.^2;
p=1/t*sum(sum(y'*y))-sum(sum(sample.^2));
% r is divided into diagonal
% and off-diagonal terms, and the off-diagonal term
% is itself divided into smaller terms
rdiag=1/t*sum(sum(y.^2))-sum(diag(sample).^2);
z=x.*xmkt(:,ones(1,n));
v1=1/t*y'*z-covmkt(:,ones(1,n)).*sample;
roff1=sum(sum(v1.*covmkt(:,ones(1,n))'))/varmkt...
-sum(diag(v1).*covmkt)/varmkt;
v3=1/t*z'*z-varmkt*sample;
roff3=sum(sum(v3.*(covmkt*covmkt')))/varmkt^2 ...
-sum(diag(v3).*covmkt.^2)/varmkt^2;
roff=2*roff1-roff3;
r=rdiag+roff;
% compute shrinkage constant
k=(p-r)/c;
shrinkage=max(0,min(1,k/t))
else % use specified number
shrinkage = shrink;
end
% compute the estimator
sigma=shrinkage*prior+(1-shrinkage)*sample;
end
It's a Part of the Matlab code from Ledoit/Wolf (2003). I don't understand why the demeaning the returns before calculating the covariance? Is this Matlab specific? In my opinion, there is no need for demeaning returns before calculating with the cov-function. (The function does it on its own)
Thanks for help in advance!

linear regression with feature normalization matlab code

I did it two ways, why is the first way(starting on line with mu=mean(X) not working? what's the difference?
function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X
% FEATURENORMALIZE(X) returns a normalized version of X where
% the mean value of each feature is 0 and the standard deviation
% is 1. This is often a good preprocessing step to do when
% working with learning algorithms.
% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
% of the feature and subtract it from the dataset,
% storing the mean value in mu. Next, compute the
% standard deviation of each feature and divide
% each feature by it's standard deviation, storing
% the standard deviation in sigma.
%
% Note that X is a matrix where each column is a
% feature and each row is an example. You need
% to perform the normalization separately for
% each feature.
%
% Hint: You might find the 'mean' and 'std' functions useful.
%
%mu=mean(X)
%X_norm=X-mu;
%sigma=std(X_norm)
%X_norm(1)=X_norm(1)/sigma(1)
%X_norm(2)=X_norm(2)/sigma(2)
% Calculates mean and std dev for each feature
for i=1:size(mu,2)
mu(1,i) = mean(X(:,i));
sigma(1,i) = std(X(:,i));
X_norm(:,i) = (X(:,i)-mu(1,i))/sigma(1,i);
end
% ============================================================
end
The reason is because you try to subtract a vector from a matrix. mean(X) gives you a vector with the mean in the columns of X, dimension [1xC], and the X is dimension [RxC]. A way to solve this in a oneliner is
X = (X-repmat(mean(X,1),size(X,1),1))./repmat(std(X,0,1),size(X,1),1)
You need to loop through X. You can further verify the output of above code with normalize(X)
for i = 1: size(X, 2)
mu = mean(X(:, i));
sigma = std(X(:, i));
X_norm(:, i) = (X(:, i) - mu) ./ sigma
end

random number with p(x)= x^(-a) distribution

How can I generate integer random number within [a,b] with below distribution in MATLAB:
p(x)= x^(-a)
I want the distribution to be normalized.
For continuous distributions: Generate random values given a PDF
For discrete distributions, as later it was specified in the OP:
The same rationale can be used as for continuous distributions: inverse transform sampling.
So from mathematical point of view there is no difference, the Matlab implementation however is different. Here is a simple solution with your distribution function:
% for reproducibility
rng(333)
% OPTIONS
% interval endpoints
a = 4;
b = 20;
% number of required random draws
n = 1e4;
% CALCULATION
x = a:b;
% normalization constant
nc = sum(x.^(-a));
% if a and b are finite it is more convinient to have the pdf and cdf as vectors
pmf = 1/nc*x.^(-a);
% create cdf
cdf = cumsum(pmf);
% generate uniformly distributed random numbers from [0,1]
r = rand(n,1);
% use the cdf to get the x value to rs
R = nan(n,1);
for ii = 1:n
rr = r(ii);
if rr == 1
R(ii) = b;
else
idx = sum(cdf < rr) + 1;
R(ii) = x(idx);
end
end
%PLOT
% verfication plot
f = hist(R,x);
bar(x,f/sum(f))
hold on
plot(x, pmf, 'xr', 'Linewidth', 1.2)
xlabel('x')
ylabel('Probability mass')
legend('histogram of random values', 'analytical pdf')
Notes:
the code is general, just replace the pmf with your function;
it is strange that the same parameter a appears in the distribution function and in the interval too.

How can i generate gaussian random process using matlab?

How can i generate Gaussian random process using Matlab with zero mean and unit variance ?
Gaussian random variable can be implemented by
w=(1/sqrt(2*pi))*exp(-(t.^2)/2);
but what about Gaussian random process ?
If the Gaussian process is white (no correlation between samples at different instants), just use
w = randn(1,n);
where n is the desired number of samples.
If you need to introduce correlation between samples (that is, the values at different instants are correlated), the usual approach is to generate a white Gaussian process and then apply a low-pass filter (using conv or filter). The autocorrelation of the process is determined by the filter shape.
For example,
w = randn(1,500);
y = conv(w,ones(1,100)/10,'same'); %// apply a simple low-pass filter
plot(w)
hold on
plot(y,'r')
You can see that the filtered signal (red) has smoother time variations, because of the (auto)correlation introduced by the filter.
A random Gaussian process with specified correlation length(cl) and RMSE -height(hRMSE) can be generated by passing a white noise with mean 0 and standard deviation hRMSE through a Gaussian filter g=exp(-(x.^2)/(cl^2/2)).
Furthermore, you can find the Matlab code under the below link: http://www.mysimlabs.com/matlab/surfgen/rsgeng1D.m
Which has been transcribed below:
function [f,x] = rsgeng1D(N,rL,h,cl)
%
% [f,x] = rsgeng1D(N,rL,h,cl)
%
% generates a 1-dimensional random rough surface f(x) with N surface points.
% The surface has a Gaussian height distribution function and a Gaussian
% autocovariance function, where rL is the length of the surface, h is the
% RMS height and cl is the correlation length.
%
% Input: N - number of surface points
% rL - length of surface
% h - rms height
% cl - correlation length
%
% Output: f - surface heights
% x - surface points
%
% Last updated: 2010-07-26 (David Bergström).
%
format long;
x = linspace(-rL/2,rL/2,N);
Z = h.*randn(1,N); % uncorrelated Gaussian random rough surface distribution
% with mean 0 and standard deviation h
% Gaussian filter
F = exp(-x.^2/(cl^2/2));
% correlation of surface using convolution (faltung), inverse
% Fourier transform and normalizing prefactors
f = sqrt(2/sqrt(pi))*sqrt(rL/N/cl)*ifft(fft(Z).*fft(F));

plot 3D line, matlab

My question is pretty standard but can't find a solution of that.
I have points=[x,y,z] and want to plot best fit line.
I am using function given below (and Thanx Smith)
% LS3DLINE.M Least-squares line in 3 dimensions.
%
% Version 1.0
% Last amended I M Smith 27 May 2002.
% Created I M Smith 08 Mar 2002
% ---------------------------------------------------------------------
% Input
% X Array [x y z] where x = vector of x-coordinates,
% y = vector of y-coordinates and z = vector of
% z-coordinates.
% Dimension: m x 3.
%
% Output
% x0 Centroid of the data = point on the best-fit line.
% Dimension: 3 x 1.
%
% a Direction cosines of the best-fit line.
% Dimension: 3 x 1.
%
% <Optional...
% d Residuals.
% Dimension: m x 1.
%
% normd Norm of residual errors.
% Dimension: 1 x 1.
% ...>
%
% [x0, a <, d, normd >] = ls3dline(X)
I have a.
So equation may be
points*a+dist=0
where dist is min. distance from origon.
Now my question is how to plot best filt line in 3D.
It helps to actually read the content of the function, which uses Singular Value Decomposition.
% calculate centroid
x0 = mean(X)';
% form matrix A of translated points
A = [(X(:, 1) - x0(1)) (X(:, 2) - x0(2)) (X(:, 3) - x0(3))];
% calculate the SVD of A
[U, S, V] = svd(A, 0);
% find the largest singular value in S and extract from V the
% corresponding right singular vector
[s, i] = max(diag(S));
a = V(:, i);
The best orthogonal fitting line is
P = x0 + a.*t
as the parameter t varies. This is the direction of maximum variation which means that variation in the orthogonal direction is minimum. The sum of the squares of the points' orthogonal distances to this line is minimized.
This is distinct from linear regression which minimizes the y variation from the line of regression. That regression assumes that all errors are in the y coordinates, whereas orthogonal fitting assumes the errors in both the x and y coordinates are of equal expected magnitudes.
[Credit: Roger Stafford , http://www.mathworks.com/matlabcentral/newsreader/view_thread/294030]
Then you only need to create some t and plot it:
for t=0:100,
P(t,:) = x0 + a.*t;
end
scatter3(P(:,1),P(:,2),P(:,3));
You may want to use plot3() instead, in which case you need only a pair of points. Since a line is infinite by definition, it is up to you to determine where it should begin and end (depends on application).