acer

32490 Reputation

29 Badges

20 years, 7 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@mmcdara Well, yes, that integration is (for this kind of example) an alternative to tallying as a means to computing the probabilities for each of the possible outcomes. It involves more calls to g.

But the full set of inputs -- to be passed to g -- is still produced with the nested seq. (The RVs assigned to Dice1 and Dice2 are not used.)

@erik10 I forgot to illustrate: You can also plot the CDF of the random variable W that I obtained above from an EmpiricalDistribution. I'll go back and add it in.

If you don't mind generating all the outcomes (as mmcdara has shown using a nested `seq`), then you could also proceed as follows.

(I originally assumed that you wanted Statistics to work out everything from the two Dice RandomVariables. But proceeding empirically seems like a viable alternative to wrestling with Statistic's handling of formulas combining discrete distributions.)

restart:

plots:-setoptions(gridlines=false):

with(Statistics):

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

all:=[seq(seq(U(i,j),i=[$1..6]),j=[$1..6])];

[-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]

M:=[op({op(all)})];

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

W:=RandomVariable(EmpiricalDistribution(all)):

Mean(W);

-1/9

Variance(W);

620/81

[seq(w=Probability(W=w),w=M)];

[-4 = 11/36, 0 = 5/36, 1 = 2/9, 2 = 1/6, 3 = 1/9, 4 = 1/18]

P:=unapply(ProbabilityFunction(W,x),x):

map(x->x=P(x),M);

[-4 = 11/36, 0 = 5/36, 1 = 2/9, 2 = 1/6, 3 = 1/9, 4 = 1/18]

S:=Sample(W,10^5):
Histogram(S,
          ':-binbounds'=[seq(j+1/2,j=min(S)-1..max(S))],
          ':-view'=0..12/36,
          ':-axis[2]'=[':-tickmarks'=[0=0,
                                      seq(i/36=sprintf("%a/%a",op(i/36)),i=1..12)],
                                     ':-gridlines'=[seq(i/36,i=1..11)]]);

map(P,M);
ListTools:-PartialSums(map(P,M));
L:=map(x->Probability(W<=x),M);

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

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

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

LL:=map(u->u=`if`(u::fraction,sprintf("%a/%a",op(u)),u),[0,op(L)]):
plot(CDF(W,x), x=-5..5,
     filled, color="Niagara Azure", transparency=0,
     axis[2]=[tickmarks=LL, gridlines=LL],
     axes=frame, size=[410,400]);

# Or, depending on what you think the values on the axis should be...
#
LL:=map(u->u=`if`(u::fraction,sprintf("%a/%a",op(u)),u),[0,op(L)]):
plot(CDF(W,M[floor(i)]), i=0..nops(M)+1,
     filled, color="Niagara Azure", transparency=0,
     axis[1]=[tickmarks=[seq(i=M[i],i=1..nops(M))]],
     axis[2]=[tickmarks=LL, gridlines=LL],
     axes=frame, labels=["",""], size=[410,400]);

 

Download histo_fun_M4.mw

@erik10 I tried for some time last night to get ProbabilityFunction to produce something useful for this example, without success.

I tried with `Or`, as well as variations using min, etc, for the first conditional. I tried using variations using min/max/signum/abs for the fallback value. I also played with mmcdara's idea of using Heaviside.

In some variants (where I also used unknown x as the second argument) I was able to get an invalid nested piecewise thing, where the inner piecewise structures had lists (with two or no entries) as the values. And it seemed to not be inferring the right support from both random variables used in the formula. I'm going to submit a bug report. In some other variants I obtained incorrect results containing Dirac(0).

It seems more a pity, since Mean seems to handle it. I don't know exactly how Probability or ProbabilityFunction is trying to do things differently (using sum, say). I also don't know exactly how or whether it considers the "constructed" formula as itself representing a discrete RV.

I'll let you know, if I discover anything useful.

For a very old version like Maple 13, and for a 3D plot, you may need to use an approach like Kitonum has done here.

@mmcdara Actually it's even less useful to utilize `if`, since that applies evalb itself.

Above, I had it in my head that you were trying piecewise rather than `if`.

For the Mean example it is possible to grind it out with magic amounts of delayed evaluation. But it gets ugly. And that kind of unevaluation quote usage it prone to breakage since every intermediate evaluation strips off a pair. Not recommended.

restart;

with(Statistics):

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

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

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

K(Dice1,Dice2);

piecewise(_R = 1 or _R0 = 1, -4, abs(-_R0+_R))

Mean(K(Dice1,Dice2));

-1/9

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

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

W1(Dice1,Dice2);

abs(-_R0+_R)

Mean(W1(Dice1,Dice2));

35/18

W2 := (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

W2(Dice1,Dice2);

piecewise(_R = 1 or _R0 = 1, -4, abs(-_R0+_R))

Mean(W2(Dice1,Dice2));

-1/9

W3 := (a,b) -> `if`(a=1 or b=1, -4, abs(b-a));

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

W3(Dice1,Dice2);

abs(-_R0+_R)

Mean(W3(Dice1,Dice2));

35/18

W4 := (a,b) -> '''`if`'''('a=1 or b=1', -4, abs(b-a));

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

W4(Dice1,Dice2);

(''`if`'')(_R = 1 or _R0 = 1, -4, abs(-_R0+_R))

Mean(W4(Dice1,Dice2));

-1/9

 

Download ugh.mw

@mmcdara I have to dash out the door right now, but regarding Question 1,

The lowercase `or` and `and` commands do an immediate evalb of a strict equality or strict "non-equality".

a = b or u = 5;
                             false

a = b or u < 5;
                             u < 5

b <> 4 or 5 = 7;
                              true

a = b and b <> 4;
                             false

It can sometimes work if you use a pair of non-strict inequalities (which logically imply the strict equality). Eg,

a<=b and a>=b and u<5;
                       a <= b and b <= a and u < 5

Hence my use of the capitalized `Or` command.

Here's what you were, in effect, passing to the Mean command,

with(Statistics): 	
Dice1 := RandomVariable(DiscreteUniform(1, 6)):
Dice2 := RandomVariable(DiscreteUniform(1, 6)):

K := (a,b) -> `if`(a=1 or b=1, -4, abs(b-a) ):

K(Dice1, Dice2);
                         |-_R16 + _R15|

Mean(K(Dice1, Dice2));
                               35
                               --
                               18

Mean(abs(Dice2-Dice1));
                               35
                               --
                               18

@emendes From the second bullet point of the Description in the Help page,

"The return value of the command is the identity of the inserted parent Table, as a string. The display of the returned value can be suppressed by terminating the statement with a full colon."

You could read the other bullet points in that Description, as there are ramifications as to what you can and cannot do with Tabulate.

The return value, being the identity of the parent Table, could allow one you to utilize the repliceid option of the DocumentTools:-InsertContent command to redisplay entirely new content to the originally inserted Task region (even from another Execution Group). That's a very esoteric use, and I don't recall ever seeign anyone else use it.

It's easier to do running replacement from within the very same Execution Group, simply by calling Tabulate more than once. This can produce a nice effect if you want in-place overwriting of the displayed content at key points of a long computation.

But it's important to realize that, unlike plots:-display, the return value of Tabulate is not any kind of "plot" structure understood by commands from the plots package.

By the way, you can resize the GUI Table that is inserted by plots:-display. You can even right click in the Table and adjust the Table properties, to change the exterior and interior borders, width-mode, etc. But such changes are not stored in any usual programmatic structure, and are transient in the sense that repeating the plots:-display call will wipe out such manual changes.

In summary, Tabulate gives only an immediate and purely printing effect, but allows you to programmatically control more aspects of the encapsulating GUI Table than does plots:-display.

 

@Rim So, clearly you need at least some amount of trig simplification, because your examples might include the need to make, say, sin(t)^2+cos(t)^2 -> 1.

If you're building up bigger and bigger expressions within a loop, it's might be best to keep the intermediate expressions "simplified" at each iteration. But you may wish to avoid everything except basic factoring/grouping and that trig simplification. That is, you may be better off with some degree of control about expression swell and trig simplification at each iteration, but without `simplify` trying its hardest to get the very "simplest" representation at each iteration.

I'm mostly just guessing, because I don't have the full package or the complete examples.

So I'll make a few suggestions for replacement to the costly (according to your analysis) call to just `simplify`. You could try them out separately and re-profile. Perhaps one of them might be faster for your full examples.

The idea is to replace the call  simplfy(ee)  which you've identified as a bottleneck, where ee is just the single expression argument.

simplify(ee, trig)
simplify(normal(ee, trig))
`simplify/nosize`(ee, trig)
`simplify/nosize`(normal(ee),trig)

There are other actions that might be thrown into this mix (combine, factor, frontend, etc).

What might be even more fruitful is if you could produce some enormous, "wrong" final result that has not been simplified as you need. If you can produce such, you could upload a worksheet here as an attachment (big green up-arrow).

What version are you using, BTW?

You seem to have made a typo as a leading minus sign on your second example.

some_trig_simp.mw

@Rim Suppose that the code just calls simplify at some internal part of the code.

One possibility is that the code does this inefficiently, eg. calling it unnecessarily and repetitively inside a loop. Presumably you are already checking out for that.

Another possibility is that simplify is too costly a tool for the simplification task at hand. We might be able to help there, but it would likely require several large examples of both input expression and desired output expression. There may be a combination of lighter weight commands that produce the same simplifications.

If you repost the procedure's source code here the please provide either a URL where you got it or the name of the author(s).

@Carl Love 

Free-dealing/Free-use exceptions to copyright for some purposes can include the need for proper attribution.

But also (and if applicable here then supercedent) copyright free-use pertains to published work. If the code itself has not been published by its rightful owner then the question of copying/reproducing it is moot. Publishing someone else unpublished work is not copying and is not ok on the fair-dealing/fair-use grounds governing copyrights.

For example, this thesis says, in its Appendix A, that some of the "files can be received from the Chair for Automation and Control of the Department for Measurement and Automation of the University of the German Armed Forces in Munich". That need not mean that the code available only in said files is published.

@Carl Love He previously posted the full code of the procedure skewSimplifier. I deleted it as an unattributed procedure from someone's else's package and thesis, posted without the author's explicit permission.

If he can show the permission of the author of that full procedure to re-post it here, or at the very least add proper attribution, then I'll let it stand.

Even fair use (or fair-dealing) exceptions to copyright generally requires proper attribution of authorship.

[note. I have removed the link that I added. The OP can add a correct link and attribution if he wishes.]

 

I'm interested to know why you want to program this using Maplets rather than Embedded Components. (Each has its merits, and drawbacks.)

@Josolumoh 

These both work for me as I might expect,

ExportMatrix("EM.txt", Matrix(Vector[column](5,i->i)), target = csv);

ExportMatrix("EM.txt", Matrix(Vector[row](5,i->i)), target = csv);

So perhaps you could try wrapping your result (from Statistics:-Sample) in a call to Matrix. (A column Vector or a row Vector produces the nx1 or 1xn Matrix, accordingly.) This seems to work reasonably with ExportMatrix.

Where is the code itself?

First 226 227 228 229 230 231 232 Last Page 228 of 594