Matlab has a built-in function to simulate from copulas: copularnd
I need to have a conditional Gaussian Copula.
I had a suggestion for Clayton Copula by another user:
Clayton Copula Sampling
for which the code is :
Can anybody by an example show, how the coding can be done, using GAUSSIAN Copula?
%% Simulations of Clayton copulas using conditional cdf
%Example for theta=4
n=3000;
theta=5;
u=rand(1,n);
y=rand(1,n);
v=((y.^(1/(1+theta)).*u).^(-theta)+1-u.^(-theta)).^(-1/theta);
x1=norminv(u);
x2=norminv(v);
plot(x1,x2,'.')
I just found this code:
%%Simulations of bivariate Gaussian copulas
%Example for rho=0.5
n=30000;
rho=0.5;
x1=norminv(rand(1,n));
x2=norminv(rand(1,n));
X = [x1; x2];
C = [1, rho; rho,1]; %2x2 Correlation matrix
cholesky = chol(C,'lower'); %lower triangular matrix of C using Cholesky decomposition
Copsims = cholesky*X;
c1 = Copsims(1,:);
c2 = Copsims(2,:);
plot(c1,c2,'.')
corrcoef(c1,c2) %check for empirical rho, not on point the initial rho because of sampling error
Related
I have a matrix M(mx2). The first column is my bins and the second one is the frequency associated with each bin. I want to fit a smooth curve to this histogram in matlab, but most of what I have tried (like kdensity) needs the real distribution of data, which I don't have them.
Is there any functions that can take the bins and their frequency and give me a smooth curve of bin-freq. ?
Here's a hack that should work for you: generate a sample from your histogram, then run ksdensity on the sample.
rng(42) % seed RNG to make reproducible
% make example histogram
N = 1e3;
bins = -5:5;
counts = round(rand(size(bins))*N);
M = [bins' counts'];
figure
hold on
bar(M(:,1), M(:,2));
% draw a sample from it
sampleCell = arrayfun( #(i) repmat(M(i,1), M(i,2), 1), 1:size(M,1), 'uniformoutput', false )';
sample = cat(1, sampleCell{:});
[f, x] = ksdensity(sample);
plot(x, f*sum(M(:,2)));
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));
Given a M x M desired covariance, R, and a desired number of sample vectors, N calculate a N x M Gaussian random vector, X in vanilla MATLAB (i.e. can't use r = mvnrnd(MU,SIGMA,cases)).
Not really sure how to tackle this, usually you need a covariance AND mean to generate a Gaussian random variable. I think sqrtm and chol could be useful.
If you have access to the MATLAB statistics toolbox you can type edit mvnrnd in MATLAB to see their solution.
[T p] = chol(sigma);
if m1 == c
mu = mu';
end
mu = mu(ones(cases,1),:);
r = randn(cases,c) * T + mu;
It feels almost like cheating to point this out, but editing MATLAB's source is very useful to understand things in general. You can also search for mvnrnd.m on google if you don't have the toolbox.
Example:
% Gaussian mean and covariance
d = 2; % number of dimensions
mu = rand(1,d);
sigma = rand(d,d); sigma = sigma*sigma';
% generate 100 samples from above distribution
num = 100;
X = mvnrnd(mu, sigma, num);
% plot samples (only for 2D case)
scatter(X(:,1), X(:,2), 'filled'), hold on
ezcontour(#(x,y) mvnpdf([x y], mu, sigma), xlim(), ylim())
title('X~N(\mu,\sigma)')
xlabel('X_1'), ylabel('X_2')
The above code uses functions from the Statistics toolbox (mvnrnd and mvnpdf). If you don't have access to it, consider these replacements (using the same concepts mentioned by others):
mvnrnd = #(mu,S,num) bsxfun(#plus, randn(num,numel(mu))*cholcov(S), mu);
mvnpdf = #(x,mu,S) exp(-0.5*(x-mu)*(S\(x-mu)')) / sqrt((2*pi)^d*det(S));
I am new to statistics. I have a discriminant function:
g(x) = ln p(x| w)+ lnP(w)
I know it has a normal distribution. I know mü and sigma variables. How can I plot pdf function of it at Matlab?
Here is a conversation: How to draw probability density function in MatLab? however I don't want to use any toolbax of Matlab.
Use normpdf, or mvnpdf for a multivariate Normal distribution:
mu = 0;
sigma = 1;
xs = [-5:.1:5];
ys = normpdf(xs, mu, sigma);
clf;
plot(xs, ys);
MATLAB plots vectors of data, so you'll need to make an X-vector, and a Y-vector.
If I had a function, say, x^2, i might do:
x = -1:.01:1; %make the x-vector
y = x.^2; %square x
plot(x,y);
You know the function of the PDF (y = exp(-x.^2./sigma^2).*1/sqrt(2*pi*sigma^2) ), so all you have to do is make the x-vector, and plot away!
Based upon a comment from #kamaci, this question gives a complete answer using #Pete's answer.
To avoid using a MATLAB toolbox, just plot the Normal probability density function (PDF) directly.
mu = 12.5; % mean
sigma = 3.75; % standard deviation
fh=#(x) exp(-((x-mu).^2)./(2*(sigma^2)))*(1/sqrt(2*pi*(sigma^2))); % PDF function
X = 0:.01:25;
p = plot(X,fh(X),'b-')
I have this matlab code for regression with one indepenpent variable, but what if I have two independent variables(x1 and x2)? How should I modify this code of polynomial regression?
x = linspace(0,10,200)'; % independent variable
y = x + 1.5*sin(x) + randn(size(x,1),1); % dependent variable
A = [x.^0, x]; % construct a matrix of permutations
w = (A'*A)\(A'*y); % solve the normal equation
y2 = A*w; % restore the dependent variable
r = y-y1; % find the vector of regression residual
plot(x, [y y2]);
Matlab has facilities for polynomial regression function polyfit. Have you tried that?
http://www.mathworks.com/help/techdoc/data_analysis/f1-8450.html
http://www.mathworks.com/help/toolbox/stats/bq_676m-2.html#bq_676m-3
But if you want to workout your own formulation,you should probably look at textbook or some online resources on regression e.g.
http://www.edwardtufte.com/tufte/dapp/DAPP3a.pdf