mmcdara

7037 Reputation

18 Badges

8 years, 230 days

MaplePrimes Activity


These are questions asked by mmcdara

The Statistics package contains a function named Specialize (which quite strangely doesn't appear when you expand the sections of this package).
Here is what help(Specialize) says:

The Specialize function takes a random variable or distribution data structure that contains symbolic parameters, and performs a substitution to specialize the given random variable or distribution.

My goal was to work with mixtures of two random variables. There are many ways to do that depending on the what you really want to achieve, but an elegant way is to define such a mixture this way:

  • Let X and Y two random variables representing the two components to be mixed.
    For instance X = Normal(mu, sigma) and Y = Normal(nu, tau).
     
  • Let B a Bernoulli random variable with parameter P.
     
  • Then M = B*X + (1-B)*Y represents a random mixture of the two components in proportions (p, 1-p).
    Note that M is a 5-parameters random variable.

Doing the things this way enables getting a lot of formal informations about M such as its mean, variance, and so on.

In order to illustrate what the mixture is I draw the histogram of a sample of M.
To do this I Specialized the three random variables X, Y, B.

I used parameters

mu=-3, nu=3, sigma=1, tau=1, p=1/2


My first attempt was to draw a sample of the random variable Mspec defined this way

Mspec := Specialize(B, [p=1/2])*Specialize(X, [mu=-3, sigma=1]) + (1-Specialize(B, [p=1/2]))*Specialize(Y, [nu=3, tau=1]);

As you see in the attached file (first plot) the histogram is wrong (so is the variance computed formally).

I changed this into

Mspec := Specialize(B, [p=1/2])*(Specialize(X, [mu=-3, sigma=1])-Specialize(Y, [nu=3, tau=1])) + Specialize(Y, [nu=3, tau=1]);

without more significative success: while the variance is nox corrext the histogram still remains obviously wrong (plot number 2)

My last attempt, which now gives q correct result (plot 3) was:

Bspec := Specialize(B, [p=1/2]);
Mspec := Bspec*Specialize(X, [mu=-3, sigma=1]) + (1-Bspec)*Specialize(Y, [nu=3, tau=1]);

Specialize.mw

I agree that one can easily do this stuff without using  Specialize.
For instance by using the procedure given at the end ofthe attached file.
Or by truly constructing a mixture Distribution (which would be more elegant but more complex).

I also agree that Specialize is in itself of a relative low interest except for educational purposes (you present the theoritical results and next you run a numerical application while giving numeric values to the formal parameters).

But why providing such an anecdotal function if it doesn't do the job correctly?

Note that these results were obtained with Maple 2015, but I doubt they'll be any better for more recent versions, given the confidential nature of Specialize.



In case this error has been corrected in more recent versions, please feel free to delete this question.

In the felp page relative to anames this example is given

restart:
test1 := 1: test2 := 2: test3 := 3.2: testall := 3.2:
select(type,{anames()},suffixed(`test`));
                 {test1, test2, test3, testall}

Note the backward quotes in suffixed(`test`).
This works only if test is not an assigned name itself:

restart:
test := 0: test1 := 1: test2 := 2: test3 := 3.2: testall := 3.2:
select(type,{anames()},suffixed(`test`));
Error, (in type/suffixed) expecting a 1st argument of type {string, symbol, list(symbol, string), set(symbol, string)}

With simple quotes:

select(type,{anames()},suffixed('test'));
              {test, test1, test2, test3, testall}

 

I recently stumbled across this video Why do calculators get this wrong

At some point, the author (Matt Parker) mentions the Farey's algorithm as a way to get a rational approximation of a number between 0 and 1.
Just for fun I applied this algorithm to build increasingly accurate rational approximations of Pi (see here Rational_approximation.mw). 
The rational approximation 

convert(evalf(Pi), rational);
                             104348
                             ------
                             33215 

is one of those the Farley's algorithm returns.

Is this pure chance, or does Maple indeed use the Farley's algorithm?
If not, what algorithm does convert/rational use?

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

Let M a block matrix, for instance:

restart:
S := [a, b, c, d]:
M := Matrix(2$2, (i, j) -> Matrix(2$2, symbol=S[2*(i-1)+j]))

It is quite simple to transform M into a 4x4 Matrix P with elements 

P[1, 1] = a[1, 1], P[1, 2] = a[1, 2], P[1, 3] = b[1, 1], ..., P[4, 4]= d[2, 2]

But does it exist a built-in Maple function which does this "unfolding"?

Thanks in advance

4 5 6 7 8 9 10 Last Page 6 of 45