/** * Creates a new GaussianMixture distribution for a given variable. * * @param var1 a {@link Variable} object. */ public GaussianMixture(Variable var1) { this.var = var1; terms = new ArrayList<Normal>(); Normal aux = new Normal(var1); aux.setMean(0); aux.setVariance(1); terms.add(aux); coefficients = new double[1]; coefficients[0] = 1; }
/** * Sets the parameters of this GaussianMixture. * * @param params an Array of doubles containing the GaussianMixture parameters. */ public void setParameters(double[] params) { if (params.length % 3 != 0) { throw new UnsupportedOperationException( "The number of parameters for the Gaussian mixture is not valid"); } else { int numTerms = params.length / 3; this.coefficients = new double[numTerms]; this.terms = new ArrayList<>(numTerms); for (int index = 0; index < numTerms; index++) { this.coefficients[index] = params[3 * index]; Normal aux = new Normal(this.var); aux.setMean(params[3 * index + 1]); aux.setVariance(params[3 * index + 2]); this.terms.add(aux); } } };
/** * Randomly initializes this GaussianMixture for a given number of terms. * * @param random a {@link java.util.Random} object. * @param numTerms a number of terms. */ public void randomInitialization(Random random, int numTerms) { this.coefficients = new double[numTerms]; this.terms = new ArrayList<>(numTerms); for (int k = 0; k < numTerms; k++) { this.coefficients[k] = random.nextDouble(); Normal aux = new Normal(this.var); aux.setMean(5 * random.nextGaussian()); aux.setVariance(random.nextDouble()); this.terms.add(aux); } ; DoubleStream aux = Arrays.stream(this.coefficients); double suma = aux.sum(); aux = Arrays.stream(this.coefficients); this.coefficients = aux.map(x -> x / suma).toArray(); // System.out.println(coefficients); // this.coefficients = this.coefficients / .map().sum(); }