sand15

1429 Reputation

15 Badges

11 years, 69 days

MaplePrimes Activity


These are replies submitted by sand15

@Andiguys 

Your expressions are that complex that I am pretty sure they come from some calculus you do not give the details here, and whose results you copied-pasted into the worksheet you provide.

The reason why you get those complex expressions is likely a consequence of you doing something like
x1 := some expression containing quantities q1, .., qn
x2 := some expression containing some quantities among q1, ... ,qn plus extra quanrities r1, ..., rn
x3 := some expression containing some quantities among q1, ..., qn and r1, .., rn, plus extra quanrities s1, ..., sn
... and so on up to a quite simple expression of the form z = f(x1, x2, ..., xm)

Doing this will automatically result in an extremely complec form for z.

A better way to proceed if you want to get a concise expression for z is to write instead
z := f(x1, x2, ..., xm)
substitutions := {x1 = ?..., x2= ..., x3= ..., ...}

Now z appears in its simplest form and you don't have t to worry about how to "shorten" the expression of z.
If you run the command eval(z, substitutions), you will get an extremely complex expression that you will probably struggle to “shorten” significantly.

Here are two simple illustrations WhatYouMustNotDo.mw WhatYouMustNotDo_2.mw of what I said.

Before CAS existed, people thought twice before embarking on laborious calculations and tried to simplify the initial expressions as much as possible before undertaking these calculations, sometimes even assigning other names to sub-expressions during the calculation.
Today's CAS are so powerful that they eliminate the need to think, leading many users to want to simplify results that they have unnecessarily complicated out of laziness.
Try to avoid taking this way too.

@C_R 

I need to think about it before giving a solid answer.

Sorry.

@C_R 

Yes It is indeed a pity that Maple does not offer a function to draw (in a correct way) the cumulative distribution of discrete random variable or of a sample.

The CumulativeSumChart (see CUSUM) is essentially used in "quality control" and has nothing to do with the cumulative function (the name CumulativeSumChart can be misleading). Note that the latter is a non decreasing function while the former can have arbitrary variations. 

Cumulative Sum Charts should be used to plot stochastic process (the corresponding help page says it plots  "the cumulative sum chart for the specified data" but unfortunately gives no reference to indicate what this cumulative sum chart, which can misled those who use the CumulativeSumChart function.

For fun, look Here.mw .

@janhardo 

I vote up

@Andiguys 

For what it's worth: answer.mw

@Andiguys 

Plotting errirs are generally the simplest errors to fix because they tell you the object you want to plot is not plottable.
So simply print X_11_21 and P11 and try to determine if they are both plottable.

After correction and text location adjustments you should get this


TIP: Are both X_11_21 and P11 numeric? And if not why?   

@Andiguys 

As you mentioned the save command I guess you want to use the save/read mechanism, am I right?
So you want to save a plot structure assigned to some name in order to recover it in some other worksheet, ok?
This is completely different than generating a png/jpeg/pdf/ps file in order to include it in a paper for instance.

The  save/read  commands manipulate binary files named 'm files'.

You will find in the attached file how to use the save/read mechanism (not limited to plots because you save a name [or a sequence of names] and thus almost anything).

I nevertheless added some stuff about the use of plotsetup in case you would be interested in exporting a plot (no longer a name) into a file. I did so because your "On a new page, I want to display this image..." statement was a little bit confusing to me because save does not save an image (contrary to what plotsetup or Export do)  but the code which create this image.

Depends_on_what_you_want_to_do.mw

@acer 

The hatching was also part of your answer, even though it was not an initial request.
Aware that the were not "one of the OP's original queries" and that you had added them for your own convenience, I did not deem it necessary to send my comment to @Andiguys as well.

See you soon on another thread

@acer 

A way (the only one?) to place the text in the foreground and the hatching in the background.

Text_foreground_hatches_background.mw

@janhardo 

restart;

with(plots):
with(Statistics):

# Goed gescheiden componenten
G13 := RandomVariable(Normal(-1, 0.7));
G14 := RandomVariable(Normal(10, 0.9)):
G15 := RandomVariable(Normal(4, 0.6)):

G16 := RandomVariable(Normal(-5, 0.7)):
G17 := RandomVariable(Normal(1, 0.9)):
G18 := RandomVariable(Normal(15, 0.6)):

G19 := RandomVariable(Normal(-2, 0.7)):
G20 := RandomVariable(Normal(-1, 0.9)):
G21 := RandomVariable(Normal(13, 0.6)):

_R

(1)


The three lines which follow can be usefull to get the parameters of the nine random variables defined above.

Of course it would have been simpler to define mu_list and sigma_list before defigning these random variables.
 

RVs := sort( select(type, [anames(alluser)], suffixed(_ProbabilityDistribution)) );

mu_list    := Mean~(RVs);
sigma_list := StandardDeviation~(RVs)

[_ProbabilityDistribution, _ProbabilityDistribution0, _ProbabilityDistribution1, _ProbabilityDistribution2, _ProbabilityDistribution3, _ProbabilityDistribution4, _ProbabilityDistribution5, _ProbabilityDistribution6, _ProbabilityDistribution7]

 

[-1, 10, 4, -5, 1, 15, -2, -1, 13]

 

[.7, .9, .6, .7, .9, .6, .7, .9, .6]

(2)


Define proportions from weights
 

weights_sep := [0.33, 0.34, 0.33, 0.35, 0.35, 0.30,0.4, 0.4, 0.4]:
proportions := weights_sep /~ add(weights_sep);
 

[.1031250000, .1062500000, .1031250000, .1093750000, .1093750000, 0.9375000000e-1, .1250000000, .1250000000, .1250000000]

(3)


The simplest way, IMO, to generate a sample of the mixture is to randomly select each component accordingly to its proportion within
the mixture and sample the selected component.

Formally
  
   sample := Vector(N):
   for n from 1 to N do
      select component C accordingly oo its proportion within
      sample[n] := Sample(C, 1)[1]
   end do:

This algorithm is very inefficient and a far better way to proceed is to define a random variable 'selector' of ProbabilityTable distribution
where probabilities are equal to 'proportions'.
A sample of  'selector' of size N is a collection of numbers 1, 2, ...9 where the proportion of '1', for instance, is equal to propotions[1].
Let N1 the number of '1', N2 the number of '2', ....
For each component Ck draw a sample Sk of size Ck and assemble the nine samples.

Note that this method works whatever the distributions are.
Using the method=[envelope, range=...] would be a very bad idea if some distributions have a bounded support (generally an error is returned
indicating that the pdf cannot be evaluated a the boundaries of the supports).
 

selector := RandomVariable(ProbabilityTable(proportions)):

N := 5000:
selections := Sample(selector, N):

sub_sizes := rhs~(sort(Tally(selections), key=(x -> lhs(x))));

sample := convert(`<|>`( seq( Sample(RVs[i], sub_sizes[i]), i=1..numelems(RVs) ) ), Vector[column]):
 

[521, 547, 535, 536, 524, 479, 618, 590, 650]

(4)


The pdf of the mixture  can be easily obtained:
 

phi := unapply(PDF(Normal(a, b), x), (x, a, b)):

f := add( proportions[i] * phi(x, mu_list[i], sigma_list[i]), i=1..numelems(RVs) ):


Plotting domain
 

qq := (min..max)(sample)

-7.14040972574213040 .. 16.3984895268977589

(5)


Visualization

  # --------------------------------------------------
  # 5. VISUALISATIE
  # --------------------------------------------------
  
  hist_plot := Histogram(sample, minbins=round(sqrt(N)), color="LightGray", style=polygon):
  pdf_plot  := plot(f(x), x=qq, color=red, thickness=2):

  print(display(hist_plot, pdf_plot,
    title="Gaussian Mixture",
    labels=["x","density"],
    view=[qq, DEFAULT]
  ));

 
 

``

Download Improvements.mw

@Carl Love

I don't think I wrote something like that.

As a reminder I wrote

Then @Carl Love probably confused by your relation (4) told you you were computed a sum and Maple  equation (5) was right.

and

So I'm sorry to say that if you really want to compute mixtures properties, all the answers that have been given here contain, are wrong in a general sense, largely due to the confusion you initiated between the weighted sum and the mixture  of random  variables.

OP's question began with him writting
I would like to build a Gaussian Mixture Model in Maple using the most straightforward approach:
and providing what I took as an illustration.
The original error comes from the OP defining a mixture of two gaussian random variables as a sum of two gaussians random variables, which is generally false unless the two (more generally all) random variables have the sam expectation) which led you in a direction that does not correspond to the one the use of the word mixture should imply.

You write
I'd like to point out the I didn't say a single word about mixtures of random variables, nor did I claim to
indeed, for the reason I explained above you focused on OP's equation (4) not on the mixture word.

I think you misinterpreted my response and overreacted to my words, as it was not my intention to offend you.
I consider that the misunderstanding is over.

@janhardo 

The formula for the pdf you write in mixtureDist is correct, so is the one for the mean, but your variance formula is correct only if the two gaussian random variables share the same mean.

For decades, I have used C++ and FORTRAN debuggers.
They have continuously improved over the years, enabling debugging parallel codes on massively parallel architectures while becoming more and more friendly and easy to use.
Maple's debugger is really pitiful in comparison.

The only situation I keep using it is to access built-in procedures: when I have to debug my own code I prefer doing it the old-fashioned way, that is using print/printf commands instead.

@CarlosRodrigues

I thought that a week would have been enough time for you to analyze the answers you received and have the courtesy to respond to us.

@acer 

as indeed a good idea.
Even if this took me a lot of time to arrive where I wanted to, O finally discovered that Statistics:-LinearFit and Statistics:-Fit both use at the very end the NAG routine g02daf.
So debugging endeds here but I got nevertheless some interesting informations.

Thanks for your help

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