mmcdara

6635 Reputation

18 Badges

8 years, 128 days

MaplePrimes Activity


These are questions asked by mmcdara



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

There are things that seem simple but rapidly turn into a nightmare.

Here is an example: what I want is to the expression given at equation (4) in the attached file.

Using Int gives a wrong result.
Using int gives a right one but not of the desired form (some double integrals are nested while others are not).

I've been stuck on this problem for hours, can you please help me to fix it?

TIA

restart

use Statistics in
  # For more generality defina an abstract probability distribution.
  AbstractDistribution := proc(N)
    Distribution(
      PDF = (x -> varphi(seq(x[n], n=1..N)))
    )
  end proc:

  # Define two random variables pf AbstractDistribution type.
  X__1 := RandomVariable(AbstractDistribution(2)):
  X__2 := RandomVariable(AbstractDistribution(2)):

end use;

proc (N) Statistics:-Distribution(Statistics:-PDF = (proc (x) options operator, arrow; varphi(seq(x[n], n = 1 .. N)) end proc)) end proc

 

_R

 

_R0

(1)

F := (U1, U2) -> U1/(U1+U2);
T := mtaylor(F(X__1, X__2), [X__1=1, X__2=1], 2):

proc (U1, U2) options operator, arrow; U1/(U1+U2) end proc

(2)


Error: x[2] is droped out of the double integral in the rightmost term

use IntegrationTools in

J := eval([op(expand(T))], [seq(X__||i=x[i], i=1..2)]);
L := add(
       map(
         j ->  
         if j::numeric then
           j
         else
           (Expand@CollapseNested)(
             Int(
               j * Statistics:-PDF(X__1, x)
               , seq(x[i]=-infinity..+infinity, i=1..2)
             )
           )
         end if
         , J
       )  
     ):
ET := %
end use;

[1/2, (1/4)*x[1], -(1/4)*x[2]]

 

1/2+(1/4)*(Int(x[1]*varphi(x[1], x[2]), [x[1] = -infinity .. infinity, x[2] = -infinity .. infinity]))-(1/4)*x[2]*(Int(varphi(x[1], x[2]), [x[1] = -infinity .. infinity, x[2] = -infinity .. infinity]))

 

1/2+(1/4)*(Int(x[1]*varphi(x[1], x[2]), [x[1] = -infinity .. infinity, x[2] = -infinity .. infinity]))-(1/4)*x[2]*(Int(varphi(x[1], x[2]), [x[1] = -infinity .. infinity, x[2] = -infinity .. infinity]))

(3)


I want this

'ET' = 1/2
       +
       (1/4)*(Int(Int(x[1]*varphi(x[1], x[2]), x[1] = -infinity .. infinity), x[2] = -infinity .. infinity))
       -(1/4)*(Int(Int(x[2]*varphi(x[1], x[2]), x[1] = -infinity .. infinity), x[2] = -infinity .. infinity))

ET = 1/2+(1/4)*(Int(Int(x[1]*varphi(x[1], x[2]), x[1] = -infinity .. infinity), x[2] = -infinity .. infinity))-(1/4)*(Int(Int(x[2]*varphi(x[1], x[2]), x[1] = -infinity .. infinity), x[2] = -infinity .. infinity))

(4)


With int instead of Int one integral is double the other is double-nested

L := add(
       map(
         j ->  
         if j::numeric then
           j
         else
             int(
               j * Statistics:-PDF(X__1, x)
               , seq(x[i]=-infinity..+infinity, i=1..2)
             )
         end if
         , J
       )  
     ):
ET := %

1/2+int(int((1/4)*x[1]*varphi(x[1], x[2]), x[1] = -infinity .. infinity), x[2] = -infinity .. infinity)+int(-(1/4)*x[2]*(int(varphi(x[1], x[2]), x[1] = -infinity .. infinity)), x[2] = -infinity .. infinity)

(5)


As the expression of ET is now correct, I tried to use IntegrationTools to get the
form I want (equation (4)).

But as soon as I replace int by Int x[2] is again droped out.

So it's not even worth thinking about using CollapseNested!

 

use IntegrationTools in
  eval(ET, int=Int);  
end use;

1/2+Int(Int((1/4)*x[1]*varphi(x[1], x[2]), x[1] = -infinity .. infinity), x[2] = -infinity .. infinity)+Int(-(1/4)*x[2]*(Int(varphi(x[1], x[2]), x[1] = -infinity .. infinity)), x[2] = -infinity .. infinity)

(6)

 

Download Int_int.mw

3 4 5 6 7 8 9 Last Page 5 of 44