필터 지우기
필터 지우기

How to find the correlation between two random numbers

조회 수: 10 (최근 30일)
Trond Oesten
Trond Oesten 2015년 2월 24일
댓글: Greig 2015년 2월 25일
Hi,
I want to give a specific correlation between two random numbers. I am using them to find the inverse lognormal distribution, so they have to remain between 0 and 1. How can I do this?
script:
N = 10;
ro = 0.75; %correlation between random numbers
f = rand(N,2);
mu7 = 0.84208;
su7 = 0.3852;
my7 = 0.688;
sy7 = 0.09975;
j = f(:,1);
g = f(:,2);
epy = logninv(j,my7,sy7);
epu_y = logninv(g,mu7,su7);
epu = epu_y + epy;
Thanks!

채택된 답변

Greig
Greig 2015년 2월 24일
편집: Greig 2015년 2월 25일
If you have two uncorrelated normally distributed random numbers, given by x, you can use the following to determine Y, which will be correlated with x(:,1)....
x=randn(1e3,2);
R0 = 0.75;
corr(x(:,1), x(:,2))
Y=R0*x(:,1)+(sqrt(1-R0)*x(:,2));
corr(x(:,1), Y)
It also appears to work well if x is from a uniform distribution, but the returned results are no longer uniformly distributed.
x=rand(1e3,2);
R0 = 0.75;
corr(x(:,1), x(:,2))
Y=R0*x(:,1)+(sqrt(1-R0)*x(:,2));
corr(x(:,1), Y)
It should be noted that the real correlation between x(:,1) and Y can vary significantly from R0 if N is small, also it works less well if x(:,1) and x(:,2) are coincidently correlated.
There are ways of doing this with Cholesky and eignevector decomposition, but I can't remember them off the top of my head.
UPDATED:
As John pointed out, the about solution does not maintain the uniformity of the random numbers after adjusted for correlations. I have attached a function that I sometimes use for this purpose, which does return uniform results
This is based on an example script provided here http://comisef.wikidot.com/tutorial:correlateduniformvariates
  댓글 수: 3
Greig
Greig 2015년 2월 24일
That is true, so the approach is only valid for random samples from unbounded distributions. Given that the problem is dealing with logninv, this might throw up some errors.
I have some scripts for this sort of thing that I can check in when I'm back at work in the morning.
Greig
Greig 2015년 2월 25일
The answer has been updated with a script that returns uniformly distributed results

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by