mmcdara

902 Reputation

13 Badges

3 years, 267 days

MaplePrimes Activity


These are questions asked by mmcdara

Hi,

Why extracting the features of the events doesn't return the good results when executed within a loop (see the pink test) ?

Thanks in advance.

PS: please, spare me  replies of the type "you can solve this equation formally"

 

restart;

interface(version);

`Standard Worksheet Interface, Maple 2015.2, Mac OS X, December 21 2015 Build ID 1097895`

(1)

sys := { diff(x(t), t) = 1, x(0) = 0 }:
evs := [ [x(t)-0.1, none],  [x(t)-0.3, none], [x(t)-0.5, none] ]:
sol := dsolve(sys, numeric, events=evs):

plots:-odeplot(sol, [t, x(t)], t=0..0.5, gridlines=true);

 

# times that fired the events

sol(1): # initialization

sol(eventfired=[1]);
sol(eventfired=[2]);
sol(eventfired=[3]);
 

[HFloat(0.1)]

 

[HFloat(0.3)]

 

[HFloat(0.49999999999999994)]

(2)

# Same times computed  within a loop

for i from 1 to 3 do
  te := op(sol(eventfired=[i]));
end do;

HFloat(0.1)

 

HFloat(0.3)

 

HFloat(0.49999999999999994)

(3)

# Values of x(t) computed  within a loop
#
# Why are calues for events 2 and 3 wrong ?
for i from 1 to 3 do
  te := op(sol(eventfired=[i]));
# xe := sol(te);             # this doesn't return the correct result,
# xe := subs(sol(te), x(t)); # this doesn't work neither
  xe := eval(x(t), sol(te)); # this doesn't work neither
end do;

HFloat(0.1)

 

HFloat(0.1)

 

HFloat(0.0)

 

HFloat(0.0)

 

HFloat(0.0)

 

HFloat(0.0)

(4)

 


 

Download Incomprehensible.mw

 

Hi, 

This is a question more or less related to this one Is it possible to define variables with unusual na... but I think it's better to open a new thread. If some think otherwise, please feel free to displace it to the link above.

I want to relabel the vertices of a graph by using special characters.
It happens that this works perfectly if I do not impose the style of the drawing but that it doesn't if I set, for instance, spring=style.
In the attached file you will see that the subsitution of the old vertex names by the new ones doesn't work if apply it directly on the many operators of the PLOT command contains.

Is this behaviour fixed in more recent versions of Maple or it's still present?


Strange-Behaviour-with-SpringStyle.mw

Hi, 

This thread is more or less related to a previous one about the Statistics:-Sample procedure.
(see https://www.mapleprimes.com/questions/228421-A-Serious-Problem-With-StatisticsSample )
I've just implemented two variant of the Box-Muller procedure to sample normal rvs.
The source is "The art of computer programming", Donald E. Knuth, 2nd edition, p117 (aka "algorithm P").

The first implementation (BoxMuller_1) is basically what D.E. Knuth writes, except that I "vectorize" some operations in order to avoid using an if.. then..else structure (as a minor consequence I generate a little bit more numbers than required).
This procedure uses the build-in Maple's procedure select (please see the link above and acer's and carl love's replies).
It appears to be relatively slow.
More of this CodeTools:-Usage(BoxMuller_1(10^6)) generates a "conenction to kernel lost" for some unknown reason.

The variant named BoxMuller_2 uses sort and ListTools:-BinaryPlace instead of select.
Ir appears to be around 3 times faster than BoxMuller_1, but remains 10 time slower than Statistics:-Sample(..., method=envelope) (here again, see the link above to understand why method=envelope is needed).

I wonder how it could be possible to speed up this procedure. In particular acer showed in one of his reply (again see the link above) how using hfloats can improve the efficiency of a procedure, but I'm very incompetent on this point.
Does anyone have any idea?

Thanks in advance.

PS: this file has been written with Maple 2015.2


 

restart:

BoxMuller_1 := proc(N)
  local V, S, T, L, X1, X2:
  V  := Statistics:-Sample(Uniform(-1, 1), [ceil(2*N/3), 2]):
  S  := V[..,1]^~2 +~ V[..,2]^~2;
  T  := < subs(NULL=infinity, select(`<`, S, 1)) | V >;
  T[.., 1]  := (-2 *~ log~(T[.., 1]) /~ T[.., 1])^~(0.5);
  X1 := select[flatten](type, T[.., 1] *~  T[..,2], 'float');
  X2 := select[flatten](type, T[.., 1] *~  T[..,3], 'float');
  return <X1 , X2>:
end proc:


BoxMuller_2 := proc(N)
  local V1, V2, S, W, u, r, T, X1, X2:
  V1 := Statistics:-Sample(Uniform(-1, 1), ceil(2*N/3)):
  V2 := Statistics:-Sample(Uniform(-1, 1), ceil(2*N/3)):
  S  := V1^~2 +~ V2^~2;
  W  := sort(S, output = [sorted, permutation]):
  u  := ListTools:-BinaryPlace(W[1], 1);
  r  := [$1..u]:
  T  := (-2 *~ log~(W[1][r]) /~ W[1][r])^~(0.5);
  X1 := T *~ V1[W[2][r]];
  X2 := T *~ V2[W[2][r]];
  return <X1 | X2>^+:
end proc:  

CodeTools:-Usage(Statistics:-Sample(Uniform(-1, 1), 10^5)):
CodeTools:-Usage(Statistics:-Sample(Uniform(-1, 1), 10^5, method=envelope)):
# CodeTools:-Usage(BoxMuller_1(10^6)): # generates a "connection to kernel lost" error msg
CodeTools:-Usage(BoxMuller_2(10^6)):

print():
CodeTools:-Usage(BoxMuller_1(10^5)):
CodeTools:-Usage(BoxMuller_2(10^5)):
print():

S1 := BoxMuller_1(10^5):
S2 := BoxMuller_2(10^5):

memory used=1.17MiB, alloc change=0 bytes, cpu time=12.00ms, real time=12.00ms, gc time=0ns
memory used=3.32MiB, alloc change=32.00MiB, cpu time=64.00ms, real time=64.00ms, gc time=0ns

memory used=148.17MiB, alloc change=119.93MiB, cpu time=706.00ms, real time=637.00ms, gc time=14.63ms

 

 

memory used=67.43MiB, alloc change=186.80MiB, cpu time=946.00ms, real time=762.00ms, gc time=285.14ms
memory used=14.81MiB, alloc change=0 bytes, cpu time=67.00ms, real time=62.00ms, gc time=0ns

 

(1)

if false then
DocumentTools:-Tabulate(
  [
    plots:-display(
      Statistics:-Histogram(S1),
      plot(Statistics:-PDF(Normal(0, 1), x), x=-4..4, color=red,thickness=3)
    ),
    plots:-display(
      Statistics:-Histogram(S2),
      plot(Statistics:-PDF(Normal(0, 1), x), x=-4..4, color=red,thickness=3)
    )
  ], width=60
)
end if:
  

# carl love's procedure with slight modifications t

SampleCheck := proc(X, f, N::posint)
uses St= Statistics;
  local S, M, O, E:
  S:= f(N):
  M:= numelems(S):
  O:= <St:-TallyInto(S, <[$(floor@min-1..ceil@max)(S)]>)>:
  E:= St:-Probability~(X <~ (rhs@lhs)~(O), 'numeric') * M:
  <rhs~(O)[2..] | E[2..] - E[..-2]>
end proc:
 

interface(rtablesize=20):
SampleCheck(Statistics:-RandomVariable(Normal(0,1)), 'BoxMuller_1', 10^5),
SampleCheck(Statistics:-RandomVariable(Normal(0,1)), 'BoxMuller_2', 10^5);

Matrix(11, 2, {(1, 1) = 1, (1, 2) = HFloat(0.029943974977392658), (2, 1) = 5, (2, 2) = HFloat(3.28979552036377), (3, 1) = 121, (3, 2) = HFloat(138.1791685600983), (4, 1) = 2276, (4, 2) = HFloat(2243.2153196005097), (5, 1) = 14488, (5, 2) = HFloat(14245.846696531142), (6, 1) = 35630, (6, 2) = HFloat(35780.43897239682), (7, 1) = 35886, (7, 2) = HFloat(35780.43897239682), (8, 1) = 14071, (8, 2) = HFloat(14245.846696531138), (9, 1) = 2212, (9, 2) = HFloat(2243.2153196005092), (10, 1) = 130, (10, 2) = HFloat(138.1791685601056), (11, 1) = 2, (11, 2) = HFloat(3.2897955203516176)}), Matrix(10, 2, {(1, 1) = 1, (1, 2) = HFloat(3.293310594473029), (2, 1) = 127, (2, 2) = HFloat(138.32680996055558), (3, 1) = 2241, (3, 2) = HFloat(2245.6121457991635), (4, 1) = 14226, (4, 2) = HFloat(14261.068070193269), (5, 1) = 35632, (5, 2) = HFloat(35818.66958395649), (6, 1) = 35864, (6, 2) = HFloat(35818.66958395649), (7, 1) = 14451, (7, 2) = HFloat(14261.068070193272), (8, 1) = 2266, (8, 2) = HFloat(2245.6121457991685), (9, 1) = 125, (9, 2) = HFloat(138.32680996054842), (10, 1) = 1, (10, 2) = HFloat(3.293310594468494)})

(2)

 


 

Download Box-Muller.mw

Hi, 
The choice method=default (the implicit choice) gives dramatically false results as demonstrated here for the sampling of a normal r.v.
It's likely to be the case for any continuous distribution.
In a few words the tails of the distribution are not correctly sampled (far from the mean by 4 standard deviations for a normal r.v., not an extremely rare event in practical applications).

I think the attached file is sufficiently documented for you to understand the problem.


 

restart:


The problem presented here also exists with Maple 2018 and Maple 2019

interface(version)

`Standard Worksheet Interface, Maple 2015.2, Mac OS X, December 21 2015 Build ID 1097895`

(1)

with(Statistics):

X := RandomVariable(Normal(0, 1)):


Generate a sample of X of size 10^6

Here is the R code for those who would like to verify the results and the performances

 

N <- 10^6

R <- 20

Q <- c(0:5)

 

M <- matrix(nrow=R, ncol=length(Q))

 

for (r in c(1:20)) {

  S <- rnorm(N, mean=0, sd=1)

  for (q in Q) {

    M[r, q+1] <-length(S[S>q])

  }

}

 

Expected.Number.of.Outliers <- floor(N - pnorm(Q, mean=0, sd=1) * N)

Observed.Number.of.Outliers <-  round(colMeans(M))

 

Expected.Number.of.Outliers

Observed.Number.of.Outliers

 

Absolute.Differences <- M - kronecker(t(Expected.Number.of.Outliers), rep(1, R))

boxplot(differences)

Remark the loop below takes about 30 seconds to run (to be compared to the 20 minutes
it would take am I used the build-in procedure Select, and to  the 3 seconds R demands).

N := 10^6:
R := 20:

Q  := [$0..5]:
nQ := numelems(Q):
M  := Matrix(R, nQ, 0):

for r from 1 to R do
  S := Sample(X, N):
  for q in Q do
    M[r, q+1] := add(Heaviside~(S -~ q)):
  end do:
end do:

Expected_Number_Of_Outliers := Vector[row](nQ, i -> Probability(X > Q[i], numeric)*N);

Expected_Number_Of_Outliers := Vector[row](6, {(1) = HFloat(500000.0), (2) = HFloat(158655.25393145697), (3) = HFloat(22750.13194817921), (4) = HFloat(1349.8980316301036), (5) = HFloat(31.67124183311998), (6) = HFloat(0.2866515719235352)})

(2)

Observed_Number_Of_Outliers := Mean(M);

Observed_Number_Of_Outliers := Vector[row](6, {(1) = 500291.4, (2) = 158782.3, (3) = 22761.35, (4) = 1349.5, (5) = 51.6, (6) = 1.9}, datatype = float[8])

(3)


While the values of  the Observed_Number_of_Outliers seem to be reasonably correct for q =0, 1, 2 and 3
there are highly suspicious for q = 4 and likely for q = 5 (but N is too small to be categorical)

So let's examine more closely rhe results for q = 4

`q=4`:= convert(M[..,5], list);
 min(`q=4`)

[HFloat(49.0), HFloat(63.0), HFloat(42.0), HFloat(47.0), HFloat(44.0), HFloat(68.0), HFloat(46.0), HFloat(51.0), HFloat(58.0), HFloat(55.0), HFloat(50.0), HFloat(51.0), HFloat(68.0), HFloat(45.0), HFloat(58.0), HFloat(44.0), HFloat(59.0), HFloat(49.0), HFloat(47.0), HFloat(38.0)]

 

HFloat(38.0)

(4)


This means we obtained 20 estimations out of 20 of the probability that X exceeds q=4.
There are only about on chance out of 1 million to get such a result (roughly (1/2)^20 ).

Here is the result R returned for the same quantile q=4

 M[,5]

 [1] 24 37 23 38 30 43 23 30 25 39 25 29 29 36 39 41 35 37 36 34

(9 values out of 20 are less than the expected value of 31.67)

A very simple test based on the Binomial distribution will prove, without any doubt, that the
distribution of the 20 numbers of outliers for q = 4 implies the sampling algorithm is of
is of very poor quality.



Is it possible to obtain correct results with Maple ?


(simulations done below for the case q=4 only)

N := 10^6:
R := 20:

Menv := Vector[column](R, 0):

for r from 1 to R do
  S := Sample(X, N, method=envelope):
  Menv[r] := add(Heaviside~(S -~ 4)):
end do:

`q=4`:= convert(Menv, list);
Mean(Menv);
Variance(Menv);

[HFloat(26.0), HFloat(26.0), HFloat(35.0), HFloat(34.0), HFloat(30.0), HFloat(30.0), HFloat(28.0), HFloat(24.0), HFloat(27.0), HFloat(31.0), HFloat(28.0), HFloat(21.0), HFloat(34.0), HFloat(33.0), HFloat(29.0), HFloat(38.0), HFloat(38.0), HFloat(33.0), HFloat(39.0), HFloat(25.0)]

 

HFloat(30.45)

 

HFloat(24.892105263157895)

(5)



Conclusion, the "default" sampling strategy suffers strong flaw.

Remark: it's well known that one of the best method to sample normal random variables
is Box-Mueller's: why it has not been programmed by default ?

 

 


 

Download Bug_Normal_Sampler.mw


 

Hi,

Sorry to ask such a stupid question but I can't find out where my error is. Probably it's so huge it blinds me!

The double loop and the matrix product F^+ . F should give the same result, no? (it seems that F^+ . F has its rows reordered ?)


 

restart:

N   := 3:
P   := 2:
niv := [seq(Z[i], i=1..N)];
f   := Matrix(N^P, P, (i,j) -> `if`(j=P, niv[(i mod 3)+1], niv[iquo(i-1,3)+1]));

niv := [Z[1], Z[2], Z[3]]

 

f := Matrix(9, 2, {(1, 1) = Z[1], (1, 2) = Z[2], (2, 1) = Z[1], (2, 2) = Z[3], (3, 1) = Z[1], (3, 2) = Z[1], (4, 1) = Z[2], (4, 2) = Z[2], (5, 1) = Z[2], (5, 2) = Z[3], (6, 1) = Z[2], (6, 2) = Z[1], (7, 1) = Z[3], (7, 2) = Z[2], (8, 1) = Z[3], (8, 2) = Z[3], (9, 1) = Z[3], (9, 2) = Z[1]})

(1)

ds := subs(niv =~ [$0..N-1], f);

ds := Matrix(9, 2, {(1, 1) = 0, (1, 2) = 1, (2, 1) = 0, (2, 2) = 2, (3, 1) = 0, (3, 2) = 0, (4, 1) = 1, (4, 2) = 1, (5, 1) = 1, (5, 2) = 2, (6, 1) = 1, (6, 2) = 0, (7, 1) = 2, (7, 2) = 1, (8, 1) = 2, (8, 2) = 2, (9, 1) = 2, (9, 2) = 0})

(2)

vs := [ seq(V__||i, i=1..P)]:
es := unapply( sort( [ seq( mul(vs ^~ [entries(ds[i,..], nolist)]), i=1..N^P) ] ), vs);
 

proc (V__1, V__2) options operator, arrow; [1, V__1, V__2, V__1^2, V__2^2, V__1*V__2, V__1*V__2^2, V__1^2*V__2, V__1^2*V__2^2] end proc

(3)

ff := convert([ seq(es(entries(ffd[i,..], nolist)), i=1..N^P) ], Matrix);


UnityRoots := [solve(z^3=1, z)]:
F := simplify(subs(niv =~ UnityRoots, ff)) /~ sqrt(N^P):

ff := Matrix(9, 9, {(1, 1) = 1, (1, 2) = Z[1], (1, 3) = Z[2], (1, 4) = Z[1]^2, (1, 5) = Z[2]^2, (1, 6) = Z[1]*Z[2], (1, 7) = Z[1]*Z[2]^2, (1, 8) = Z[1]^2*Z[2], (1, 9) = Z[1]^2*Z[2]^2, (2, 1) = 1, (2, 2) = Z[1], (2, 3) = Z[3], (2, 4) = Z[1]^2, (2, 5) = Z[3]^2, (2, 6) = Z[1]*Z[3], (2, 7) = Z[1]*Z[3]^2, (2, 8) = Z[1]^2*Z[3], (2, 9) = Z[1]^2*Z[3]^2, (3, 1) = 1, (3, 2) = Z[1], (3, 3) = Z[1], (3, 4) = Z[1]^2, (3, 5) = Z[1]^2, (3, 6) = Z[1]^2, (3, 7) = Z[1]^3, (3, 8) = Z[1]^3, (3, 9) = Z[1]^4, (4, 1) = 1, (4, 2) = Z[2], (4, 3) = Z[2], (4, 4) = Z[2]^2, (4, 5) = Z[2]^2, (4, 6) = Z[2]^2, (4, 7) = Z[2]^3, (4, 8) = Z[2]^3, (4, 9) = Z[2]^4, (5, 1) = 1, (5, 2) = Z[2], (5, 3) = Z[3], (5, 4) = Z[2]^2, (5, 5) = Z[3]^2, (5, 6) = Z[2]*Z[3], (5, 7) = Z[2]*Z[3]^2, (5, 8) = Z[2]^2*Z[3], (5, 9) = Z[2]^2*Z[3]^2, (6, 1) = 1, (6, 2) = Z[2], (6, 3) = Z[1], (6, 4) = Z[2]^2, (6, 5) = Z[1]^2, (6, 6) = Z[1]*Z[2], (6, 7) = Z[1]^2*Z[2], (6, 8) = Z[1]*Z[2]^2, (6, 9) = Z[1]^2*Z[2]^2, (7, 1) = 1, (7, 2) = Z[3], (7, 3) = Z[2], (7, 4) = Z[3]^2, (7, 5) = Z[2]^2, (7, 6) = Z[2]*Z[3], (7, 7) = Z[2]^2*Z[3], (7, 8) = Z[2]*Z[3]^2, (7, 9) = Z[2]^2*Z[3]^2, (8, 1) = 1, (8, 2) = Z[3], (8, 3) = Z[3], (8, 4) = Z[3]^2, (8, 5) = Z[3]^2, (8, 6) = Z[3]^2, (8, 7) = Z[3]^3, (8, 8) = Z[3]^3, (8, 9) = Z[3]^4, (9, 1) = 1, (9, 2) = Z[3], (9, 3) = Z[1], (9, 4) = Z[3]^2, (9, 5) = Z[1]^2, (9, 6) = Z[1]*Z[3], (9, 7) = Z[1]^2*Z[3], (9, 8) = Z[1]*Z[3]^2, (9, 9) = Z[1]^2*Z[3]^2})

(4)

Scalar products of pairs of comumn vectors

F must be an orthogonal array

for i1 from 1 to N^P do
  for i2 from 1 to N^P do
    printf("%a ", simplify(add(F[..,i1] . F[.., i2])))
  end do:
  printf("\n"):
end do:
printf("\n");

1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
 

 

or more simply:

simplify(F^+ . F)

Matrix([[1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0]])

(5)

 


 

Download Too_Blind_To_Find_My_Mistake.mw

1 2 3 4 5 6 7 Last Page 3 of 14