d
I changed the r variable right before inserting it. The randnum variable should be r.
How would I create a running mean?
data pieces;
call streaminit(12345);
do i = 1 to 50000;
r = rand('uniform');
short = min(r, 1-r);
long = max(r, 1-r);
output;
short_long = short / long;
long_short = long / short;
end;
run;
proc means data=pieces;
var short_long long_short;
run;
@JamesHawthorne wrote:
I changed the r variable right before inserting it. The randnum variable should be r.
How would I create a running mean?
data pieces;
call streaminit(12345);
do i = 1 to 50000;
r = rand('uniform');
short = min(r, 1-r);
long = max(r, 1-r);
output;
short_long = short / long;
long_short = long / short;
end;
run;
proc means data=pieces;
var short_long long_short;
run;
data status;
set pieces;
row = _n_;
SL_Running + short_long;
LS_Running + long_short;
SL_Mean = SL_Running / _n_;
LS_Mean = LS_Running / _n_;
run;
proc sgplot data=status;
series x=row y=SL_mean;
series x=row y=LS_Mean;
run;
So by looking at the graphs, the expected value of short divided by long converges because the values converge towards a single value. While the expected value of long divide by short diverges because it doesn't converge towards a single value?
Expected Value of Long Divide by Short
Expected Value of Short Divide by Long
Long/short is the tough one... Look at this:
data test;
call streaminit(12345);
do e = 2 to 7;
n = 10**e;
do m = 1 to 9-e;
do i = 1 to n;
x = rand("uniform");
y = max(x, 1-x) / min(x, 1-x);
output;
end;
end;
end;
keep n m y;
run;
proc sql;
create table outtest as
select
n, m,
mean(y) as _mean_ "Mean(long/short)"
from test
group by n, m;
quit;
proc sgplot data=outtest;
scatter x=n y=_mean_ / jitter;
xaxis type=log logbase=10;
run;
I'm relatively new to SAS programming so I'm having trouble understanding some of your code. I'm having trouble understanding the purpose of the first 2 do loops. Why is the variable 'n' 10^e? Also, why does the second do loop go from 1 to 9-e. Does the code below do the same thing but in a simpler way?
data pieces;
call streaminit(23456);
do i= 1 to 100000;
r= rand('uniform');
short = min(r,1-r);
long = max(r,1-r);
output;
short_long = short/long;
long_short = long/short;
end;
run;
data status;
set pieces;
row = _n_;
SL_Running + short_long;
LS_Running + long_short;
SL_Mean = SL_Running / _n_;
LS_Mean = LS_Running / _n_;
run;
The two loops generate
7 independent samples of 100 values,
6 independent samples of 1000 values,
...
and 2 independent samples of 10000000 values.
I wanted to explore the variability of estimates as a function of sample size while keeping the total size of the simulation within reasonable bounds.
This is not quite equivalent to @Reeza's proposition as those consecutive estimates are not independent. But both approaches are instructive.
As for the original question. In numerical analysis, we say that an iterative procedure diverges when the results start moving farther away from the objective, at some point. So, technically, the long/short mean simulation is NOT diverging, as it keeps (very slowly) moving closer to the true value, which is infinity.
What this exercise demonstrates imho is that random simulation can be a very poor estimator sometimes.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: