forked from CPernet/Robust-Correlations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional.m
30 lines (25 loc) · 834 Bytes
/
conditional.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function [values,variance]=conditional(X,Y)
% Returns the conditional values and variances of X given Y and Y given X.
% The calculation is based on Pearson correlation values because
% if the X & Y are jointly normal and r = 0, then X & Y are independent.
%
% FORMAT: [values,variance]=conditional(X,Y)
%
% INPUTS: X and Y are two vectors of the same length
%
% OUTPUTS: values are the conditioned variables X and Y
% variances are the conditional variances
%
% Cyril Pernet v1 21/05/2012
% ---------------------------------
% Copyright (C) Corr_toolbox 2012
if size(X)~=size(Y)
error('X and Y must have the same size')
end
r = corr(X,Y);
Xhat = r*std(X)*Y / std(Y);
Yhat = r*std(Y)*X / std(X);
Cond_stdX = (1-r^2)*std(X);
Cond_stdY = (1-r^2)*std(Y);
values = [Xhat Yhat];
variance = [Cond_stdX^2 Cond_stdY^2];