基本的にそれはプログラミングの問題よりも統計であるが、それは次の方法で行うことができますので、あなたは、どのようにcreate 2 vectors with a specified correlationに尋ねる:
ステップ1 - 希望相関
r = 0.75; % r is the desired correlation
M = rand(10000,2); % two vectors from uniform distribution between 0 to 1
R = [1 r; r 1];
L = chol(R); % this is Cholesky decomposition of R
M = M*L; % when multiplied by M it gives the wanted correlation
M = (M+abs(min(M(:)))); % shift the vector to only positive values
M = M./max(M(:)); % normalize the vector...
M = round(40*M)+10; % ...to values between 10 to 50
disp([min(M(:)) max(M(:))])
first_r = corr(M(:,1), M(:,2)); % and check the resulted correlation
を持つ2つのベクトルを作成します
rand
関数は、randi
またはrandn
のような任意の生成された数値関数に変更できます。特定の分布が望ましい場合は、using the it's cdfを取得できます。
ステップ2 - サンプルの2つのセット、Xはいずれかのこれらのベクターのサンプリング> YおよびY> X
x = M(:,1);
y = M(:,2);
Xy = x>y; % logical index for all x > y
Yx = y>x; % logical index for all y > x
xy1 = datasample([x(Xy) y(Xy)],150,'Replace',false); % make a 1/2 sample like Xy
xy2 = datasample([x(Yx) y(Yx)],150,'Replace',false); % make a 1/2 sample like Yx
x = [xy1(:,1);xy2(:,1)]; % concat the smaples back to x
y = [xy1(:,2);xy2(:,2)]; % concat the smaples back to y
checkx = sum(x>y) % how many times x is bigger than y
checky = sum(y>x) % how many times y is bigger than x
final_r = corr(x,y) % and check the new correlation
ステップ3有するもの - あなたのように相関
を補正しますfinal_r
が望みのようではないことが分かります。r
ですので、最初にr
をfinal_r
から移動する必要があります。ここでは例です - 最初の出力時にr = 0.75
:
10 50
checkx =
150
checky =
150
final_r =
0.67511
我々はfinal_r
は0.074886でシフトダウンされていることがわかり、私たちは私たちのfinal_r
正しいを取得するには、この値によって、元のr
をシフトアップしたいと思います。我々はr = 0.75+0.074886
で再びそれを実行するのであれば、我々が得る:希望r
にかなり近いです
10 50
checkx =
150
checky =
150
final_r =
0.76379
を。私は、例えば、1000回のプロセスでループを実行して、最も近いものを見つけてr
を見つけたり、または単にfinal_r
が十分に近くなるまで検索を続けるしきい値を設定します。
出典
2016-07-26 20:24:34
EBH
_red_と_blue_ dotsで始まり、突然_green_になりますか? – EBH
良い点、申し訳ありません。 – user1363251
コピュラを使用しますか? [this](http://stackoverflow.com/a/37515473/5540279)はあなたの質問に答えていますか? –