mean = np.average(vals) stdv = np.std(vals) checkAnswer('mean of 10x100 batch samples for local engine provided',mean,0.5,tol=1e-3) checkAnswer('stdv of 10x100 batch samples for local engine provided',stdv,np.sqrt(1./12.),tol=1e-3) ### randomNormal ## first test box muller mean,stdev = randomUtils.BoxMullerGenerator().testSampling(1e5,engine=None) checkAnswer('Box Muller generator mean for engine not provided',mean,0.0,tol=5e-3) checkAnswer('Box Muller generator stdv for engine not provided',stdev,1.0,tol=1e-3) mean,stdev = randomUtils.BoxMullerGenerator().testSampling(1e5,engine=eng) checkAnswer('Box Muller generator mean for local engine provided',mean,0.0,tol=5e-3) checkAnswer('Box Muller generator stdv for local engine provided',stdev,1.0,tol=1e-3) ## test single value vals = randomUtils.randomNormal(engine=None) checkAnswer('random normal single value for engine not provided',vals,1.90167449657) vals = randomUtils.randomNormal(engine=eng) checkAnswer('random normal single value for local engine provided',vals,1.90167449657) ## test single point right = [1.11130480322, 0.698326166056, 2.82788725018] vals = randomUtils.randomNormal(3,engine=None) checkArray('random normal single point for engine not provided',vals,right) vals = randomUtils.randomNormal(3,engine=eng) checkArray('random normal single point for local engine provided',vals,right) ## test many points vals = randomUtils.randomNormal(3,5,engine=None) checkAnswer('randomNormal number of samples for engine not provided',len(vals),5) checkAnswer('randomNormal size of sample for engine not provided',len(vals[0]),3)
def _nextNeighbour(self, rlz, fraction=1): """ Perturbs the state to find the next random neighbour based on the cooling schedule @ In, rlz, dict, current realization @ In, fraction, float, optional, the current iteration divided by the iteration limit i.e., $\frac{iter}{Limit}$ @ Out, nextNeighbour, dict, the next random state for exponential cooling: .. math:: fraction = \\frac{iter}{Limit} amp = 1-fraction delta = \\frac{-amp}{2} + amp * r where :math: `r \sim \mathcal{U}(0,1)` for boltzmann cooling: .. math:: amp = min(\\sqrt(T), \\frac{1}{3*alpha} delta = r * alpha * amp where :math: `r \\sim \\mathcal{N}(0,1)` for cauchy cooling: .. math:: amp = r delta = alpha * T * tan(amp) where :math: `r \\sim \\mathcal{U}(-\\pi,\\pi)` for veryfast cooling: .. math:: amp = r delta = \\sign(amp-0.5)*T*((1.0+\\frac{1.0}{T})^{\\abs{2*amp-1}-1.0} where :math: `r \\sim \\mathcal{U}(0,1)` """ nextNeighbour = {} D = len(self.toBeSampled.keys()) alpha = 0.94 if self._coolingMethod in ['exponential', 'geometric']: amp = ((fraction)**-1) / 20 r = randomUtils.random(dim=D, samples=1) delta = (-amp / 2.) + amp * r elif self._coolingMethod == 'boltzmann': amp = min(np.sqrt(self.T), 1 / 3.0 / alpha) delta = randomUtils.randomNormal(dim=D, size=1) * alpha * amp elif self._coolingMethod == 'veryfast': amp = randomUtils.random(dim=D, samples=1) delta = np.sign(amp - 0.5) * self.T * ( (1 + 1.0 / self.T)**abs(2 * amp - 1) - 1.0) elif self._coolingMethod == 'cauchy': amp = (np.pi - (-np.pi)) * randomUtils.random(dim=D, samples=1) - np.pi delta = alpha * self.T * np.tan(amp) for i, var in enumerate(self.toBeSampled.keys()): nextNeighbour[var] = rlz[var] + delta[i] self.info['amp_' + var] = amp self.info['delta_' + var] = delta[i] self.info['fraction'] = fraction return nextNeighbour
## 2d batch sampling randomUtils.randomSeed(42) vals = randomUtils.random(10, 1000) # check statistics mean = np.average(vals) stdv = np.std(vals) checkAnswer('mean of 10x100 batch samples', mean, 0.5, tol=1e-3) checkAnswer('stdv of 10x100 batch samples', stdv, np.sqrt(1. / 12.), tol=1e-3) ### randomNormal ## first test box muller mean, stdev = randomUtils.BoxMullerGenerator().testSampling(1e5) checkAnswer('Box Muller generator mean', mean, 0.0, tol=5e-3) checkAnswer('Box Muller generator stdv', stdev, 1.0, tol=1e-3) ## test single value vals = randomUtils.randomNormal() checkAnswer('random normal single value', vals, 1.90167449657) ## test single point right = [1.11130480322, 0.698326166056, 2.82788725018] vals = randomUtils.randomNormal(3) checkArray('random normal single point', vals, right) ## test many points right = [0, 0, 0] vals = randomUtils.randomNormal(3, 5) checkAnswer('randomNormal number of samples', len(vals), 5) checkAnswer('randomNormal size of sample', len(vals[0]), 3) ### randomIntegers(), sampling integers in a range randomUtils.randomSeed(42) right = [14, 18, 20, 12, 17] for i in range(5):