TransWikia.com

Defining a Gaussian mixture distribution for $N$ number of Gaussian distributions

Mathematica Asked on August 16, 2021

I am experimenting a bit with mixed-Gaussian distributions. If I want to define a mixture of two or three distributions, I do this with:

GaussianMixture2[w1_, w2_, u1_, u2_, s1_, s2_] := Evaluate[MixtureDistribution[{w1, w2}, {NormalDistribution[u1, s1], NormalDistribution[u2, s2]}]]
GaussianMixture3[w1_, w2_, w3_, u1_, u2_, u3_, s1_, s2_, s3_] := Evaluate[MixtureDistribution[{w1, w2, w3}, {NormalDistribution[u1, s1], NormalDistribution[u2, s2], NormalDistribution[u3, s3]}]]

However if I want to do this for $N = 10$ distributions, it becomes to get clumsy and silly. Does anyone know how I can define a function similar to those above for $N$ number of distributions.

I want to be able to use the function in FindDistributionParameters, which I can with the above functions, and get a value for each individual $u_{i}$ and $s_{i}$. In addition, It would be nice to ensure that the sum of all weights, $w_{i}$, must be equal to $1$.

One Answer

(* generate some data *)
SeedRandom[1];
n = 10;
means = RandomReal[{-5, 5}, n];
stddevs = RandomReal[{0.1, 2}, n];
weights = Normalize[RandomReal[1, n], Total];
mixture = MixtureDistribution[weights, 
   MapThread[NormalDistribution[#1, #2] &, {means, stddevs}]];
data = RandomVariate[mixture, 10000];
Histogram[data]

(* create the parameters *)
paraMeans = Array[m, n];
paraStddevs = Array[s, n];
paraWeights = Array[w, n];
(* random initial values *)
weightsInit = Transpose[{paraWeights, Normalize[RandomReal[1, n], Norm]}];
meansInit = Transpose[{paraMeans, ConstantArray[Mean[data], n]}];
stddevsInit = Transpose[{paraStddevs, RandomReal[1, n]}];

(* create the mixture and fit *)
mixturemodel = MixtureDistribution[paraWeights, 
   MapThread[NormalDistribution[#1, #2] &, {paraMeans, paraStddevs}]];
guessedParams = 
 FindDistributionParameters[data, mixturemodel, 
  Flatten[{weightsInit, meansInit, stddevsInit}, 1], AccuracyGoal -> 2]

(* try it out and compare *)
Histogram[RandomVariate[mixturemodel /. guessedParams, 10000]]

Correct answer by flinty on August 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP