Question: A question about RandomVariable

The automatic transformation below is a rule in Maple

a := 3:
b := a^2
                               9

that we can prevent using single quotes for instance.

When a is a random variables this becomes

a := Statistics:-RandomVariable(Uniform(0, 1));
                               _R
b := a^2
                                2
                              _R 

# Thus, for instance
Statistics:-Mean(b);
                               1
                               -
                               3



Of course you do know that b is defined as a^2, but if you present this to a student it will be puzzled by the output of b containing _R and not a. Of course you can explain it why this happens, but if b has a more complex expression inoking several random variables, it becomes almost impossible to understand what its display means as it contains only names like _R, _R0, ...

To avoid this difficulty a possibility is to reverse the order of the operations this way

a := 'a':
b := a^2;
                                2
                               a 
a := Statistics:-RandomVariable(Uniform(0, 1));
                              _R
# And then
Statistics:-Mean(b);
                               1
                               -
                               3


But this become quite tricky in some situations, for instance

restart:
b := a^2 - Mean(a)^2
                          2          2
                         a  - Mean(a) 
a := Statistics:-RandomVariable(Uniform(0, 1)):

# The expected result can be got this way
eval(b, Mean = Statistics:-Mean):
Statistics:-Mean(%);
                               1 
                               --
                               12


Or, if you upload the Statistics package

restart
with(Statistics):
b := a^2 - ':-Mean'(a)^2
                          2          2
                         a  - Mean(a) 
a := RandomVariable(Uniform(0, 1)):
eval(b, ':-Mean' = Mean):
Mean(%);
                               1 
                               --
                               12


As we preserved a comprehensible expression for b in the last two examples, this come at the expense of a more complex calculation of the final result.

My goal is the quite simple:  I would like to preserve as long as possible the original names of the random variables (a in the examples above), and get the desired result without never make their "aliases" _R, _R0, _R1, .... appear in intermediate outputs.
The strategy I use is sketched in the file  Manipulation_of_random_variables.mw

Do you have another idea to achieve this goal?

In fact this question could be also stated "Given a random variable _R, could it be possible to recover the name it has been assigned to. I thought to something like this:

restart:
a := Statistics:-RandomVariable(Uniform(0, 1));
b := Statistics:-RandomVariable(Uniform(0, 1));
c := 1:
d := [$1..3]:
                               _R
                              _R0

# This seems correct
FromNames2RV := [anames(RandomVariable)] =~ eval([anames(RandomVariable)]);
                       [b = _R0, a = _R]

# Unfortunately further use of FromNames2RV evaluates its left hand sides
FromNames2RV 
                      [_R0 = _R0, _R = _R]


TIA

Please Wait...