Obsidian | Level 7

## plot the correlation between 2 functions

Hello,

My problem is the following one:

I have 2 function A and B. The parameters m is in [0.001,30]. We have tau_1=1 and tau_2=1.

I wonder if there is a way plot the corrélation between my function A and B on SAS?

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: plot the correlation between 2 functions

The distribution of M determines the correlation between A(m) and B(m). If you choose M ~ lognormal you will get a different answer from if you choose M ~ gamma or M ~ exponential.

Since you didn't specify the distribution of M, the following DATA step chooses equally spaced values in [1,30] along with some small values. This should give the general idea of how to do the computation:

``````/* evaluate A(m) and B(m) for range of m
THE LOCATIONS OF m THAT YOU CHOOSE AFFECT THE CORRELATION */
data AB;
tau2 = 2;
do tau1 = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
/* probably want a DISTRIBUTION for m */
do m = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
c1 = -m/tau1;
c2 = -m/tau2;
A = (1 - exp(c1)) / c1 - exp(c1);
B = c2**2 * exp(c2);
output;
end;
end;
drop c1 c2;
run;

/* compute correlation matrices for each tau1 value */
proc corr data=AB noprint
outp=OutCorr(where=(_TYPE_="CORR") rename=(B=Corr));
by tau1;
var A B;
run;

/* we only want the correlation coefficient; extract the R[1,2] cell */
data Corr;
set OutCorr;
if mod(_N_,2)=1;   /* Keep odd-numbered rows; delete even rows */
drop _TYPE_ _NAME_ A;
run;

title "Corr(A,B), tau2=2";
proc sgplot data=Corr;
series x=tau1 y=Corr;
run;``````

If you choose a distribution for M, then the inner loop will be

do i = 1 to 10000;

m = rand("disrib_name", params);

c1= ..; c2=..; etc;

end;

10 REPLIES 10
Diamond | Level 26

## Re: plot the correlation between 2 functions

This might help:

http://support.sas.com/documentation/cdl/en/procstat/66703/HTML/default/viewer.htm#procstat_corr_syn...

I will move the post to stats section of forum also.

Obsidian | Level 7

## Re: plot the correlation between 2 functions

I have a technical question about the correlation matrix:

Do you know how to keep only the value of the corr between C_0 and P_0, C_1 and P_1, C_2 and P_2... in a table?

Diamond | Level 26

## Re: plot the correlation between 2 functions

Am afraid I can't help you, hopefully in this forum area a Stato will be able to help.  I am merely a programmer

Super User

## Re: plot the correlation between 2 functions

I have a technical question about the correlation matrix:

Do you know how to keep only the value of the corr between C_0 and P_0, C_1 and P_1, C_2 and P_2... in a table?

It wouldn't hurt to show the code you used to generate that.

Assuming you used Proc Corr and want Pearson correlation statistics then the option OUTP=datassetname will place the statistics into a dataset named  on the option.

I am not sure exactly what you mean by "between C_0 and P_0, C_1 and P_1, C_2 and P_2" but you should be able to transform the resulting dataset to have the contents and structure you want.

Opal | Level 21

## Re: plot the correlation between 2 functions

Why not show the relation between the two functions as:

``````proc fcmp outlib=sasuser.fcmp.math;
function A(m);
return ((1-exp(-m))/m - exp(-m));
endsub;
function B(m);
return (m**2 * exp(-m));
endsub;
run;

options cmplib=sasuser.fcmp;

data test;
do lm = log(.001) to log(30) by log(30/0.001)/100;
m = exp(lm);
A = A(m);
B = B(m);
output;
end;
run;

proc sgplot data=test;
series x=a y=b / markers
markerattrs=(symbol=circlefilled size=5)
lineattrs=(thickness=2);
run;
``````

PG
SAS Super FREQ

## Re: plot the correlation between 2 functions

Could you clarify your statement? There are many kinds of correlations. Are you looking for cross-correlation of time series?

Opal | Level 21

## Re: plot the correlation between 2 functions

Interesting Relationship...

PG
Obsidian | Level 7

## Re: plot the correlation between 2 functions

For the correlation I meant the cov(X,Y) by std_X and std_y.

I would like to plot the correlation of the function A(m) and B(m) for m in [0.000001,30], tau1 fixed equal to 2 and tau2 in [0.0001,30].

the x axis will be tau2 and the y axis will be corr(A,B)

SAS Super FREQ

## Re: plot the correlation between 2 functions

So there are no random variables in your problem? The variable M is not random with a distribution?

Do you have SAS/IML software or do you require the solution to use Base SAS?

SAS Super FREQ

## Re: plot the correlation between 2 functions

The distribution of M determines the correlation between A(m) and B(m). If you choose M ~ lognormal you will get a different answer from if you choose M ~ gamma or M ~ exponential.

Since you didn't specify the distribution of M, the following DATA step chooses equally spaced values in [1,30] along with some small values. This should give the general idea of how to do the computation:

``````/* evaluate A(m) and B(m) for range of m
THE LOCATIONS OF m THAT YOU CHOOSE AFFECT THE CORRELATION */
data AB;
tau2 = 2;
do tau1 = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
/* probably want a DISTRIBUTION for m */
do m = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
c1 = -m/tau1;
c2 = -m/tau2;
A = (1 - exp(c1)) / c1 - exp(c1);
B = c2**2 * exp(c2);
output;
end;
end;
drop c1 c2;
run;

/* compute correlation matrices for each tau1 value */
proc corr data=AB noprint
outp=OutCorr(where=(_TYPE_="CORR") rename=(B=Corr));
by tau1;
var A B;
run;

/* we only want the correlation coefficient; extract the R[1,2] cell */
data Corr;
set OutCorr;
if mod(_N_,2)=1;   /* Keep odd-numbered rows; delete even rows */
drop _TYPE_ _NAME_ A;
run;

title "Corr(A,B), tau2=2";
proc sgplot data=Corr;
series x=tau1 y=Corr;
run;``````

If you choose a distribution for M, then the inner loop will be

do i = 1 to 10000;

m = rand("disrib_name", params);

c1= ..; c2=..; etc;

end;

Discussion stats
• 10 replies
• 1783 views
• 5 likes
• 5 in conversation