Simulating stock returns as normally distributed to run through a copula
4 views (last 30 days)
Show older comments
I am trying to model stock index returns for 4 different indices as normally distributed, to then run these distributions through a copula to obtain their correlation structure. I keep encountering the error ,
Error using copulafit (line 75)
U must contain values strictly between 0 and 1.
I know this is because the normal cdf of each set of returns varies between [0,1] , but when I model the returns as having a kernel distribution, the code runs fine, so how do I get a normal distribution cdf estimate for each set of returns that is between (0,1), so that the returns are able to be inputted to copulafit.m ?
Here's the code for a normal distribution estimate, that doesn't work.
% returns_N(:,1234) = returns for each of the 4 indices
u= normcdf(returns_N(:,1),mean(returns_N(:,1)),std(returns_N(:,1)));
v= normcdf(returns_N(:,2),mean(returns_N(:,2)),std(returns_N(:,2)));
w= normcdf(returns_N(:,3),mean(returns_N(:,3)),std(returns_N(:,3)));
x= normcdf(returns_N(:,4),mean(returns_N(:,4)),std(returns_N(:,4)));
correlation_gaussian_normal = copulafit('Gaussian',[u v w x]);
%Error using copulafit (line 75)
%U must contain values strictly between 0 and 1.
Heres the code for a kernel distribution estimate, that works fine
u = ksdensity(returns_N(:,1),returns_N(:,1),'function','cdf');
v = ksdensity(returns_N(:,2),returns_N(:,2),'function','cdf');
w = ksdensity(returns_N(:,3),returns_N(:,3),'function','cdf');
x = ksdensity(returns_N(:,4),returns_N(:,4),'function','cdf');
correlation_gaussian_kernel = copulafit('Gaussian',[u v w x]);
3 Comments
Phillip McCart
on 3 Jun 2022
Did you ever find the answer to this?
I'm having a the same problem but I'm using ecdf instead of normcdf
[fx,~]=ecdf(x);
[fy,~]=ecdf(y);
u = [x,y];
[rhohat,nuhat,nuci]=copulafit(u)
Error using copulafit (line 75)
U must contain values strictly between 0 and 1.
I've verified that the values of fx and fy lie between 0 and 1 or are 0 and 1. I've tried deleting the zeroes and the beginning of fx, fy and same goes for the 1s and the end.
I'm so confused! U doesn't contain any values greater than 1 or less than zero!!
Kausthub
on 31 Jan 2024
Hi Phillip, shouldn't it be u = [fx,fy] instead of u = [x,y] since you have verified the values of fx and fy lie between (0,1)?
Answers (1)
Kausthub
on 31 Jan 2024
Edited: Kausthub
on 31 Jan 2024
Hi Pete and Phillip,
I understand that you are facing issues regarding the "normcdf" function. The range of "normcdf" and "ecdf" is [0,1] which is inclusive of both 0 and 1, whereas "copulafit" requires values strictly between (0,1) i.e., exclusive of both 0 and 1. Since, there is possiblity of "normcdf" and "ecdf" returning 0 or 1, the "copulafit" function errors when it recieves an input containing either of 0 or 1. For example:
x = [-2,-1,0,1,11];
mu = 2;
sigma = 1;
u = normcdf(x,mu,sigma)
correlation_gaussian_kernel = copulafit('Gaussian', [u]);
To solve to issue, as rightly suggested by Jeff you could try to subtract or add a small value (epsilon) to make 0 to 0+ and 1 to 1-.
Hope this helps!
0 Comments
See Also
Categories
Find more on Probability Distributions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!