mmcdara

7891 Reputation

22 Badges

9 years, 55 days

MaplePrimes Activity


These are replies submitted by mmcdara

@radaar 

So I gave you the solution 3 hours ago

 

@erik10 

Here is an alternative way to proceed.

You will lose a part of the "upstream" conceptualization to the benefit of some "downstream" symbolic computations

 






restart:

with(Statistics):

K := (a,b) -> piecewise(a=1 or b=1, -4, abs(b-a) );

proc (a, b) options operator, arrow; piecewise(a = 1 or b = 1, -4, abs(b-a)) end proc

(1)

OneDice := [$1..6]:
Gain    := [ seq(seq(K(i, j), i in [$1..6]), j in [$1..6]) ];

tally   := [ map(k -> [ k, numelems([ListTools:-SearchAll(k, dice)]) ], {dice[]})[] ];

[-4, -4, -4, -4, -4, -4, -4, 0, 1, 2, 3, 4, -4, 1, 0, 1, 2, 3, -4, 2, 1, 0, 1, 2, -4, 3, 2, 1, 0, 1, -4, 4, 3, 2, 1, 0]

 

[[-4, 11], [0, 5], [1, 8], [2, 6], [3, 4], [4, 2]]

(2)

code := [$1..numelems(tally)] =~ op~(1, tally);
edoc := table(rhs~(code) =~lhs~(code));
prob := op~(2, tally) / add(op~(2, tally));

[1 = -4, 2 = 0, 3 = 1, 4 = 2, 5 = 3, 6 = 4]

 

table( [( 0 ) = 2, ( 1 ) = 3, ( 2 ) = 4, ( -4 ) = 1, ( 3 ) = 5, ( 4 ) = 6 ] )

 

[11/36, 5/36, 2/9, 1/6, 1/9, 1/18]

(3)

Game := RandomVariable(ProbabilityTable(prob));

_R0

(4)

ProbabilityFunction(Game, k)

piecewise(k < 1, 0, k <= 6, [11/36, 5/36, 2/9, 1/6, 1/9, 1/18][floor(k)], 0)

(5)

# rough graph obtained without paying attention to the fact that the cdf is discontinuous....

plot(CDF(Game, k), k=0..numelems(code)+1, tickmarks=[code, default], gridlines=true, color=blue, thickness=2)

 

# A few examples

Probability(Game = edoc[-4]);
Probability(Game = edoc[1]);

Game__mean := add(Probability(Game = edoc[k])*k, k in [indices(edoc, nolist)]);
Game__variance := add(Probability(Game = edoc[k])*k^2, k in [indices(edoc, nolist)]) - Game__mean^2;

11/36

 

2/9

 

-1/9

 

620/81

(6)

N := 10^3:
S := Sample(Game, N):
Histogram(S, tickmarks=[code, default], view=[0..numelems(code)+1, default]);

 

 



Download Another-Way.mw

I understood the scaling problem when comparing PDF(MyX, x) and Histogram(A).

It comes from the limited range choosen in the envelope method.
Let X a random variable with pdf P.

Choosing method=envelope and a  finite range [a, b] means you want to sample X between a and b.
If [a, b] contains the support of X you sample the whole X.

But if X has an infinite support (as for Levy distribution), using range=[a, b] means you sample a new distribution X' defined by 
X' = X*(Heaviside(X-a)-Heaviside(X-b)).
Obviously the integral, between a and b, of the pdf of X'  is 1, but Prob(X > a and X < b) is less than 1.

When using method=envelope you do not specify if [a, b] includes the support of X (which should be the case for the method to give correct results).
Her I used method=envelope in a hurry to sample a distribution (Levy's) Maple doesn't kow how to sample. Then Maple thought that I used method=envelope in a regular way and deduced that the support of the distribution was included in [a, b].
Which means that, a priori, Prob(X > a and X < b) = 1.

But, being not the case, the histogram appears above the pdf curve of X is Prob(X > a and X < b)  < 1
Let's say that the area of the histogram is 1 but the area below the pdf curve is 



(maybe plus a visualization difficulty as this excerpt of the full worksheet (just load it and add its content at the end of my previous mailing) suggests)



Excerpt.mw

 

@Kitonum 

Or even 

sigma2:=()->sqrt((mu(args^~2)-mu(args)^2)*nargs/(nargs-1)):

which uded twice your "mu" function


But I already liked your two last lines

 

@acer 

Received loud and clear.
Thanks for your quick and detailed answer 

@acer 

Interesting the way you define your function K.

      My first attempt was (with your notations)
      K := (a,b) -> `if`(a=1 or b=1, -4, abs(b-a) ):
      # or  K := (a,b) -> piecewise(a=1 or b=1, -4, abs(b-a) ):

      But whatever this definition of K  Mean(K(Dice1, Dice2));   
      always returned the unexpected value 35/18.
      (That's why I used these artificial shifted Heaviside functions.)


I woukd like to ask you two questions:

Question 1 :
Do you have some idea why  
        K := (a,b) -> piecewise( Or(a=1, b=1), -4, abs(b-a) ):
        Mean(K(Dice1, Dice2)); 
returns the good result but not 
        K := (a,b) -> `if`(a=1 or b=1, -4, abs(b-a) ):
        Mean(K(Dice1, Dice2));
   


Question 2
My "Heaviside trick" is limited in that it does not allow to obtain the values of Probability(K(Dice1, Dice2)=k) (or < instead of =) for k in [-4, 0, 1, 2, 3, 4].
Neither is your K function, by the way.
Do you have some idea to get the values of these probabilities (Obviously by keeping the same framework consisting in defining two RVs and a function of them)?

 

 

 

@erik10 

Here it is :-)
 

restart:

with(Statistics):

Dice1 := RandomVariable(DiscreteUniform(1, 6));
Dice2 := RandomVariable(DiscreteUniform(1, 6));

_R

 

_R0

(1)

g := (Dice1, Dice2) -> -4*(1-Heaviside(Dice1-3/2)*Heaviside(Dice2-3/2)) + abs(Dice2-Dice1)*Heaviside(Dice1-3/2)*Heaviside(Dice2-3/2)

proc (Dice1, Dice2) options operator, arrow; -4+4*Heaviside(Dice1-3/2)*Heaviside(Dice2-3/2)+abs(Dice2-Dice1)*Heaviside(Dice1-3/2)*Heaviside(Dice2-3/2) end proc

(2)

Mean(g(Dice1, Dice2));
Variance(g(Dice1, Dice2));

-1/9

 

620/81

(3)

S := Sample(g(Dice1, Dice2), 10^3):
Tally(S);
Histogram(S);

[HFloat(-4.0) = 301, HFloat(0.0) = 162, HFloat(1.0) = 225, HFloat(4.0) = 36, HFloat(3.0) = 106, HFloat(2.0) = 170]

 

 

 


 

Download Same-Player-Shoots-Again.mw

@Rouben Rostamian  

 

If I correctly understand your problem, I would answer question 2 by saying n=32339
 

Nb_of_all_different_sequences := 6^6;

# also given by

combinat:-numbperm([seq(k$6, k=1..6)], 6)

46656

 

46656

(1)

# The probability to get the sequence S = [1, 2, 3, 4, 5, 6] is

P := 1 / Nb_of_all_different_sequences

1/46656

(2)

with(Statistics):

# The "world" is made of the 46656 different sequences.
# An experiment consist of randomly picking one sequence from this "world".
# The occurrence of the sequence S can then modelled by a Bernoulli RV
# with parameter P.
# The number of sequences we must pick to obtain N occurences of S is then
# described by a begative binomial RV with parameters N and P

X := N -> RandomVariable(NegativeBinomial(N, P))

proc (N) options operator, arrow; Statistics:-RandomVariable(NegativeBinomial(N, P)) end proc

(3)

# Median of the number of sequences one must pick from the "world" before
# to get the sequence S

Quantile(X(1), 0.5, numeric);

32339.

(4)

 


 

Download Negative-Binomial.mw

@Carl Love 

You're probably referring to my question titled A question about the persistence of eval (2018/09/28)

@Carl Love 

Thanks Carl

@vv 

Thanks vv

@vv 

I wasn't sure of the results I got.

Could you be kind enough to produce the code you used to find all those cycles in order I try to understand why I missed 13 of them?

I believe my mistake comes from using sets instead of lists. It made easier the composition of two cycles but it fotgot the order of the vertices (or edges).

Thanks

@Carl Love 

Very good, 

I was so focused on the expressions of the cycles themselves (and on an algorithm to find them) hat I did not even look to Graph Theory to search for some theoritical result about their numbers.

Do you know about an algorithm that build all the cycles from the cycle basis? (@vv)

 

@LichengZhang 

I think this new code is correct.
Being not easy on complex graph to verify its answer, even on the OctahedronGraph above, I inserted an animation which helps visualize all the cycles found.

 

restart:

with(GraphTheory):
with(SpecialGraphs):
with(combinat, powerset):

graph := 1;

if graph = 1 then
  G := OctahedronGraph();
  Cycles := CycleBasis(G);
  N := numelems(Cycles);
 
elif graph = 2 then
  G := CompleteGraph(4);
  Cycles := CycleBasis(G);
  N := numelems(Cycles);

elif graph = 3 then
  G := Graph({{1, 2}, {2, 3}, {3, 4}, {4, 1}, {2, 5}, {5, 6}, {6, 3}});
  Cycles := CycleBasis(G);
  N := numelems(Cycles);

elif graph = 4 then
  G := Graph({{1, 2}, {2, 3}, {3, 4}, {4, 1}, {5, 6}, {6, 7}, {7, 8}, {8, 5}, {3, 8}});
  Cycles := CycleBasis(G);
  N := numelems(Cycles);

elif graph = 5 then
  G := Graph({{1, 2}, {2, 3}, {3, 4}, {4, 1}, {5, 6}, {6, 7}, {3, 7}, {3, 5}});
  Cycles := CycleBasis(G);
  N := numelems(Cycles);

end if;


try
  DrawGraph(G, style=planar):
catch:
  DrawGraph(G, style=spring);
end try;

1

 

GRAPHLN(undirected, unweighted, [1, 2, 3, 4, 5, 6], Array(%id = 18446744078244058886), `GRAPHLN/table/9`, 0)

 

[[1, 3, 2, 4], [1, 3, 2, 5], [1, 3, 2, 6], [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6]]

 

7

 

 

# close the list of vertices by adding the first vertex to its right

for n from 1 to N do
   C||n := [Cycles[n][], Cycles[n][1]];
end do:

# Create the sets of edges for each cycle

for n from 1 to N do
   S||n := map(m -> {(C||n)[m], (C||n)[m+1]}, {$1..numelems(C||n)-1});
end do;
 

{{1, 3}, {1, 4}, {2, 3}, {2, 4}}

 

{{1, 3}, {1, 5}, {2, 3}, {2, 5}}

 

{{1, 3}, {1, 6}, {2, 3}, {2, 6}}

 

{{1, 3}, {1, 5}, {3, 5}}

 

{{1, 3}, {1, 6}, {3, 6}}

 

{{1, 4}, {1, 5}, {4, 5}}

 

{{1, 4}, {1, 6}, {4, 6}}

(1)

S0 := {seq(S||n, n=1..N)}:
PS := powerset(S0):
NS := numelems(PS);

128

(2)

SD := proc(L::{list, set})
local N, n, sd:
N:= numelems(L):
if N = 1 then
   return L[1]
else
   if L[1] intersect L[2] <> {} then
     sd := ( L[1] union L[2] ) minus ( L[1] intersect L[2] );
     for n from 3 to N do
       if sd intersect L[n] <> {} then
         sd := ( sd union L[n] ) minus ( sd intersect L[n] )
       else
         return {}
       end if:
     end do:
     return eval(sd)
   else
     return {}
   end if:
end if:
end proc:
 

AllCycles := NULL:
for ns from 2 to NS do
   SymDiff      := SD([seq(PS[ns][k], k=1..nops(PS[ns]))]):
   vertices   := sort(ListTools:-Flatten(op~([SymDiff[]]))):
   NbMaxOccur := 0:
   for v in {vertices[]} do
     NbMaxOccur := max(NbMaxOccur, numelems([ListTools:-SearchAll(v, vertices)]))
   end do:
   if NbMaxOccur < 3 then
     AllCycles := AllCycles, SymDiff
   end if;
end do:
AllCycles := {AllCycles} union {seq(S||n, n=1..N)} minus {{}};
NAC := numelems(AllCycles);

{{{1, 3}, {1, 5}, {3, 5}}, {{1, 3}, {1, 6}, {3, 6}}, {{1, 4}, {1, 5}, {4, 5}}, {{1, 4}, {1, 6}, {4, 6}}, {{2, 3}, {2, 5}, {3, 5}}, {{2, 3}, {2, 6}, {3, 6}}, {{2, 4}, {2, 5}, {4, 5}}, {{2, 4}, {2, 6}, {4, 6}}, {{1, 3}, {1, 4}, {2, 3}, {2, 4}}, {{1, 3}, {1, 4}, {3, 5}, {4, 5}}, {{1, 3}, {1, 4}, {3, 6}, {4, 6}}, {{1, 3}, {1, 5}, {2, 3}, {2, 5}}, {{1, 3}, {1, 6}, {2, 3}, {2, 6}}, {{1, 4}, {1, 5}, {2, 4}, {2, 5}}, {{1, 4}, {1, 6}, {2, 4}, {2, 6}}, {{1, 5}, {1, 6}, {2, 5}, {2, 6}}, {{1, 5}, {1, 6}, {3, 5}, {3, 6}}, {{1, 5}, {1, 6}, {4, 5}, {4, 6}}, {{2, 3}, {2, 4}, {3, 5}, {4, 5}}, {{2, 3}, {2, 4}, {3, 6}, {4, 6}}, {{2, 5}, {2, 6}, {3, 5}, {3, 6}}, {{2, 5}, {2, 6}, {4, 5}, {4, 6}}, {{3, 5}, {3, 6}, {4, 5}, {4, 6}}, {{1, 3}, {1, 4}, {2, 3}, {2, 5}, {4, 5}}, {{1, 3}, {1, 4}, {2, 3}, {2, 6}, {4, 6}}, {{1, 3}, {1, 4}, {2, 4}, {2, 5}, {3, 5}}, {{1, 3}, {1, 4}, {2, 4}, {2, 6}, {3, 6}}, {{1, 3}, {1, 5}, {2, 3}, {2, 4}, {4, 5}}, {{1, 3}, {1, 5}, {2, 5}, {2, 6}, {3, 6}}, {{1, 3}, {1, 6}, {2, 3}, {2, 4}, {4, 6}}, {{1, 3}, {1, 6}, {2, 5}, {2, 6}, {3, 5}}, {{1, 3}, {1, 6}, {3, 5}, {4, 5}, {4, 6}}, {{1, 4}, {1, 5}, {2, 3}, {2, 4}, {3, 5}}, {{1, 4}, {1, 5}, {3, 5}, {3, 6}, {4, 6}}, {{1, 4}, {1, 6}, {2, 3}, {2, 4}, {3, 6}}, {{1, 4}, {1, 6}, {2, 5}, {2, 6}, {4, 5}}, {{1, 4}, {1, 6}, {3, 5}, {3, 6}, {4, 5}}, {{1, 5}, {1, 6}, {2, 3}, {2, 5}, {3, 6}}, {{1, 5}, {1, 6}, {2, 3}, {2, 6}, {3, 5}}, {{1, 5}, {1, 6}, {2, 4}, {2, 5}, {4, 6}}, {{1, 5}, {1, 6}, {2, 4}, {2, 6}, {4, 5}}, {{2, 3}, {2, 6}, {3, 5}, {4, 5}, {4, 6}}, {{2, 4}, {2, 5}, {3, 5}, {3, 6}, {4, 6}}, {{2, 4}, {2, 6}, {3, 5}, {3, 6}, {4, 5}}, {{1, 3}, {1, 5}, {2, 3}, {2, 6}, {4, 5}, {4, 6}}, {{1, 3}, {1, 5}, {2, 4}, {2, 5}, {3, 6}, {4, 6}}, {{1, 3}, {1, 6}, {2, 3}, {2, 5}, {4, 5}, {4, 6}}, {{1, 3}, {1, 6}, {2, 4}, {2, 6}, {3, 5}, {4, 5}}, {{1, 4}, {1, 5}, {2, 3}, {2, 5}, {3, 6}, {4, 6}}, {{1, 4}, {1, 6}, {2, 3}, {2, 6}, {3, 5}, {4, 5}}}

 

50

(3)

# close the list of vertices by adding the first vertex to its right

for n from 1 to NAC do
   CAC||n := Graph(convert~(AllCycles[n], set));
end do:

p := proc(m)
  local n, CG:
  n := round(m):
  CG := CopyGraph(G):
  HighlightEdges(CG, Edges(CG), 'blue');
  HighlightEdges(CG, Edges(CAC||n), 'red');
  try
    DrawGraph(CG, style=planar):
  catch:
    DrawGraph(CG, style=spring);
  end try;
 
end proc:

plots:-animate(p, [m], m=1..NAC, frames=NAC)

 

 


 

Download TotalNumberOfCycles_A.mw

@LichengZhang 

My code in TotalNumberOfCycles.mw  (sent from @sand15 when I was at the office) is wrong.

I will try to fix it

First 132 133 134 135 136 137 138 Last Page 134 of 154