List of usage examples for org.apache.commons.math3.distribution MultivariateNormalDistribution sample
public double[][] sample(final int sampleSize)
From source file:org.apache.hadoop.hive.ql.abm.simulation.MCSimNode.java
public List<SimulationResult> simulate(IntArrayList[] requests) { init(requests);// w w w . j av a 2 s .c o m if (dimension == 0) { return null; } if (parent == null) { return defaultSimulate(); } List<SimulationResult> parRet = parent.simulate(targets); if (parRet == null) { return defaultSimulate(); } List<SimulationResult> ret = new ArrayList<SimulationResult>(); for (SimulationResult res : parRet) { ArrayList<IntArrayList> condIds = res.lastCondId; boolean[] fake = new boolean[dimension]; double[] mu = new double[dimension]; double[][] A = new double[dimension][dimension]; double[][] B = new double[res.invSigma.getColumnDimension()][dimension]; double[] zero = new double[dimension]; for (int i = 0; i < within1.length; ++i) { IntArrayList cIds = condIds.get(i); within1[i].fill(cIds, fake, mu, A); InterDistOracle[] w2s = within2[i]; for (int j = i + 1; j < within2.length; ++j) { w2s[j].fillSym(cIds, condIds.get(j), fake, mu, A); } } int dif = between.size() - res.condIds.size(); double[] pmu = res.getMean(); for (int i = 0; i < targets.length; ++i) { int cum = 0; IntArrayList cIds = condIds.get(i); for (int k = 0; k < res.condIds.size(); ++k) { ArrayList<IntArrayList> condIds2 = res.condIds.get(k); InterDistOracle[] os = between.get(k + dif)[i]; for (int j = 0; j < os.length; ++j) { cum += os[j].fillAsym(cIds, condIds2.get(j), fake, mu, pmu, B, cum); } } } for (int k = 0, cum = 0; k < res.condIds.size(); ++k) { ArrayList<IntArrayList> pCIds = res.condIds.get(k); InterDistOracle[][] oss = between.get(k + dif); for (int j = 0; j < oss.length; ++j) { IntArrayList cIds = pCIds.get(j); InterDistOracle[] os = oss[j]; int off = 0; for (int i = 0; i < os.length; ++i) { off = os[i].fillAsym(cIds, condIds.get(i), fake, pmu, mu, B, cum); } cum += off; } } Array2DRowRealMatrix a = new Array2DRowRealMatrix(A); Array2DRowRealMatrix b = new Array2DRowRealMatrix(B); Array2DRowRealMatrix c = (Array2DRowRealMatrix) b.transpose(); Array2DRowRealMatrix tmp = c.multiply(res.invSigma); Array2DRowRealMatrix sigma = a.subtract(tmp.multiply(b)); double[] scale = correct(sigma.getDataRef()); MultivariateNormalDistribution dist = new MultivariateNormalDistribution(zero, sigma.getDataRef()); double[][] smpls = dist.sample(res.samples.size()); int pos = 0; for (double[] smpl : smpls) { if (scale != null) { restore(smpl, scale); } fixFake(fake, mu, smpl); double[] s = res.getSample(pos); subtract(s, pmu); add(smpl, tmp.operate(s)); res.samples.get(pos)[level] = smpl; ++pos; } res.means.add(mu); res.invSigma = new Array2DRowRealMatrix( new LUDecomposition(concat(a, c, b, res.invSigma)).getSolver().getInverse().getData()); dispatch(res, ret); } return ret; }
From source file:org.apache.hadoop.hive.ql.abm.simulation.MCSimNode.java
private List<SimulationResult> defaultSimulate() { List<SimulationResult> ret = new ArrayList<SimulationResult>(); boolean[] fake = new boolean[dimension]; double[] mu = new double[dimension]; double[][] A = new double[dimension][dimension]; double[] zero = new double[dimension]; ArrayList<IntArrayList> condIds = zeroCondIds; for (int i = 0; i < within1.length; ++i) { IntArrayList cIds = condIds.get(i); within1[i].fill(cIds, fake, mu, A); InterDistOracle[] w2s = within2[i]; for (int j = i + 1; j < within2.length; ++j) { w2s[j].fillSym(cIds, condIds.get(j), fake, mu, A); }//from ww w. jav a 2 s . com } double[] scale = correct(A); MultivariateNormalDistribution dist = new MultivariateNormalDistribution(zero, A); double[][] smpls = dist.sample(NUM_SIMULATIONS); SimulationResult res = new SimulationResult(); for (double[] smpl : smpls) { if (scale != null) { restore(smpl, scale); } fixFake(fake, mu, smpl); double[][] samples = new double[NUM_LEVEL][]; samples[level] = smpl; res.samples.add(samples); } res.means.add(mu); res.invSigma = new Array2DRowRealMatrix( new LUDecomposition(new Array2DRowRealMatrix(A)).getSolver().getInverse().getData()); dispatch(res, ret); return ret; }