Programming the statistical procedures from SAS

Comparison CDF curves

Reply
Contributor
Posts: 20

Comparison CDF curves

Goodevening everyone, 

 

I have two variables (bootstrapreturn and actualreturn) that I have plotted one CDF graphs to be able to compare them by using the following SAS code: 

 

ods graphics on;
data Comparison;
set WORK.FAMA3CDF;
length varName $20;
ObsNum = _N_;
varName = "BootstrapReturn"; Value = BootstrapReturn; output; /* put VAR1 on this line */
varName = "ActualReturn"; Value = ActualReturn; output; /* put VAR2 on this line */
run;

proc univariate data=Comparison;
class varName;
var Value;
cdfplot Value/ overlay;
run;

 

That being said, I would like to be able, for each percentile, to know the proportion of observations of bootstrap return higher/lower than actual return. For example, I would like to know that, at the 95th percentile, 87% of the observations of bootstrap return are lower than actual return.

Any suggestion? I really have no idea how I could do that...

 

Thanks a lot in advance for your help!

 

PROC Star
Posts: 1,316

Re: Comparison CDF curves

An interesting problem. I must admit, I don't have a clue about how to do it either, but SAS gives us a great set of meccano pieces to stitch something together.

 

First question: are you expecting to see something graphical, or something tabular? In either case, could you sketch out what your desired result would look like? That will give us a target to aim at.

 

Tom

Contributor
Posts: 20

Comparison CDF curves

Hi,

thanks a lot for your answer. I expect to have a table with the following structure:

Percentile ActualReturn BootstrapReturn % > ActualReturn
10 -4.25 -3.18 8.12%
20 -3.5 -2.8 23%
30 -2 -1.9 61%
40
...



PROC Star
Posts: 1,316

Re: Comparison CDF curves

This will give you the side by side percentiles. The percentage column needs some more thinking.

 

Tom

 

proc univariate data=FAMA3CDF;
	var ActualReturn BootstrapReturn;
	output out=Pctls pctlpre=AR BR pctlpts=10 to 100 by 10;
run;

proc transpose data=Pctls out=TrnsPctls;
run;

data TrnsPctls;
	set TrnsPctls(rename=(Col1=PctlValue));
	Category = substr(_name_, 1, 2);
	Percentile = input(subpad(_name_, 3), best3.);
	drop _name_ _label_;
run;

proc transpose data=TrnsPctls out=ReTrnsPctls(drop=_name_);
	var PctlValue;
	by Percentile;
	ID Category;
run;
Contributor
Posts: 20

Comparison CDF curves

Yes that's it! Let me know if you have a solution for the last part Smiley Happy
Thanks a lot
PROC Star
Posts: 1,316

Re: Comparison CDF curves

Here's what I hope is the whole thing. I don't have the time to verify the results using my test data in detail, so I suggest that you evaluate it very carefully. I've created different temporary datasets all the way through so that you can see the partial results as they are created.

 

Let me know!

   Tom

 

proc univariate data=FAMA3CDF;
	var ActualReturn BootstrapReturn;
	output out=Pctls pctlpre=AR BR pctlpts=10 to 100 by 10;
run;

proc transpose data=Pctls out=TrnsPctls;
run;

data TrnsPctls;
	set TrnsPctls(rename=(Col1=PctlValue));
	Category = substr(_name_, 1, 2);
	Percentile = input(subpad(_name_, 3), best3.);
	drop _name_ _label_;
run;

proc transpose data=TrnsPctls out=ReTrnsPctls(drop=_name_);
	var PctlValue;
	by Percentile;
	ID Category;
run;

proc sql noprint;
	select count(*) into: RecordCount from work.FAMA3CDF;
quit;

proc sort data=WORK.FAMA3CDF out=FAMA3CDFSort;
	by BootstrapReturn;
run;

proc sql noprint;
	create table AllVars as
		select * from FAMA3CDFSort cross join Pctls;
quit;

%macro CalcStats;

	data Stats;
		set AllVars end=LastRec;

		%do i = 10 %to 100 %by 10; /* Set up an accumulator variable for each percentile */
			retain ARUnder&i 0;

			if BootstrapReturn < AR&i then
				ARUnder&i = ARUnder&i + 1;
		%end;

		if LastRec
			then do;

			%do i = 10 %to 100 %by 10; /* Set up an accumulator variable for each percentile */
				Percentile = &i;
				ARPctg&i = ARUnder&i / &RecordCount;
				keep ARPctg&i;
			%end;

			output;
		end;
	run;

%mend;

%CalcStats;

proc transpose data=Stats out=TrnsStats;
run;

data Percentages;
	set TrnsStats(rename=(Col1=Percentage));
	Percentile = input(subpad(_name_,7), best3.);
	drop _name_;
run;

proc sql noprint;
	create table Want as
		select r.Percentile, r.AR, r.BR, p.Percentage
			from ReTrnsPctls r inner join Percentages p
				on r.Percentile = p.Percentile;
quit;
Esteemed Advisor
Posts: 5,535

Re: Comparison CDF curves

[ Edited ]

You could do that like this:

 

data test;
call streaminit(896868);
do i = 1 to 200;
    x = rand("normal");
    y = rand("normal");
    output;
    end;
run;

proc sort data=test(keep=x) out=test_x(rename=x=u); by x; run;
proc sort data=test(keep=y) out=test_y(rename=y=u); by y; run;
proc rank data=test_y out=test_y fraction; var u; ranks ru; run;

data test_xy;
merge test_x(in=inx) test_y(in=iny);
by u;
retain cdf_y 0;
if iny then cdf_y = ru;
if inx;
drop ru;
rename u=x;
run;

proc sgplot data=test_xy;
step x=x y=cdf_y;
run;
PG
Contributor
Posts: 20

Re: Comparison CDF curves

Hi PG, 

 

I applied it to my code so I obtain 

 

proc sort data=fama3cdf(keep=ActualReturn) out=test_x(rename=ActualReturn=u); by ActualReturn; run;
proc sort data=fama3cdf(keep=BootstrapReturn) out=test_y(rename=BootstrapReturn=u); by BootstrapReturn; run;
proc rank data=test_y out=test_y fraction; var u; ranks ru; run;

data test_xy;
merge test_x(in=inx) test_y(in=iny);
by u;
retain cdf_y;
if iny then cdf_y = ru;
if inx;
drop ru;
rename u=x;
run;

proc sgplot data=test_xy;
series x=x y=cdf_y;
run;

 

I guess I have to skip the first part as I have my dataset. But I dont understand the result I obtain... What is the meaning of the third table?

 

Thanks a lot for your advice

Esteemed Advisor
Posts: 5,535

Re: Comparison CDF curves

[ Edited ]

The data_xy dataset contains for each value of x (ActualReturn) the proportion of y values (BootstrapReturn) which are inferior or equal to x in the variable cdf_y.

PG
SAS Super FREQ
Posts: 4,242

Re: Comparison CDF curves

I think PGStats's program gives you the proportion for each value of X. For large data sets, you might want to compute the proportions only for the percentiles of X. For example, the following SAS/IML program uses PGStats's idea, but only results in 100 computations, regardless of the size of X:

 

data test;
call streaminit(896868);
do i = 1 to 200;
    x = rand("normal", 0);
    y = rand("normal", 0.1);
    output;
    end;
run;

proc iml;
use test;  read all var {x y};  close;
call sort(x);
call sort(y);

/* for each percentile of x, what is the proportion of observations 
   in y that are less than that percentile? */
pctls = do(0.0, 1, 0.01);
call qntl(q, x, pctls);   /* convention: 0th pctl=min; 100th pctl=max */
prop = j(nrow(q), 1);
do i = 1 to nrow(q);
   prop[i] = mean(y <= q[i]);   /* or use >= */
end;

title "Proportion of Y that is less than or equal to quantile of X";
call scatter(q, prop) label={"Quantiles of X" "Proportion of Y That Is Less Than"};
Ask a Question
Discussion stats
  • 9 replies
  • 289 views
  • 4 likes
  • 4 in conversation