BookmarkSubscribeRSS Feed
7 REPLIES 7
Reeza
Super User
Your log has a note that indicates where part of your issue is, did you fix that already?

I *think* that you're being asked to determine if the expected value converges. So as you simulate the data and keep adding values and recalculating the mean, does the value become a constant or does it go all over the place. You can check this by creating a running mean and seeing if the running means are diverging/converging. Graphing helps to show this (SGPLOT).

So once you fix the simulation portion, you can add the running total part and then graph the results.

This is my interpretation based on ONLY the information you've included in the post.
JamesHawthorne
Fluorite | Level 6

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;

 

Reeza
Super User

@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;
JamesHawthorne
Fluorite | Level 6

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 ShortExpected Value of Long Divide by ShortExpected Value of Short Divide by LongExpected Value of Short Divide by Long

PGStats
Opal | Level 21

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;

SGPlot8.png

 

PG
JamesHawthorne
Fluorite | Level 6

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; 

 

PGStats
Opal | Level 21

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.

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1172 views
  • 0 likes
  • 3 in conversation