def computeReferenceValues(self, uqSetting, n=2000): # ---------------------------------------------------------- # dicretize the stochastic space with Monte Carlo # ---------------------------------------------------------- if uqSetting.getSize() < n: print("-" * 60) print("Latin Hypercube sampling") print("-" * 60) mcSampler = MCSampler.withLatinHypercubeSampleGenerator(self.params, n - uqSetting.getSize()) while uqSetting.getSize() < n: samples = mcSampler.nextSamples(n - uqSetting.getSize()) uqSetting.runSamples(samples) uqSetting.writeToFile() def f(y, t): return [fi(y, t) for fi in self.f] self.y0 = [1., .5, 0.] self.n = ((self.tn - self.t0) / self.dt) + 1 self.t = np.linspace(self.t0, self.tn, self.n, endpoint=True) # solve the ODEs soln = odeint(f, self.y0, self.t) self.y1 = soln[:, 0] self.y2 = soln[:, 1] self.y3 = soln[:, 2]
def run_mc(self, N=10, out=False, plot=False): # ---------------------------------------------------------- # dicretize the stochastic space with Monte Carlo # ---------------------------------------------------------- np.random.seed(1234567) print("-" * 60) print("Latin Hypercube Sampling") print("-" * 60) mcSampler = MCSampler.withLatinHypercubeSampleGenerator(self.params, N) mcUQSettingBuilder = UQBuilder() self.defineUQSetting(mcUQSettingBuilder) mcUQSetting = mcUQSettingBuilder.andGetResult() # ---------------------------------------------------------- # Monte Carlo Estimator # ---------------------------------------------------------- samples = mcSampler.nextSamples(N) mcUQSetting.runSamples(samples) samples = mcUQSetting.getTimeDependentResults(self.toi, qoi=self.qoi) # split the results into chunk of Ni samples num_samples = len(next(iter(samples.values()))) analysis = MCAnalysis(self.params, samples) analysis.setVerbose(False) stats = {"num_model_evaluations": num_samples, "mean_estimated": analysis.mean(), "var_estimated": analysis.var()} if out: # store results filename = os.path.join(self.pathResults, "%s-qoi%s_%s.pkl" % (self.radix, self.qoi, "mc")) fd = open(filename, "w") pkl.dump({'surrogate': 'mc', 'num_dims': self.numDims, 'sampling_strategy': "latin_hypercube", 'num_model_evaluations': num_samples, 'time_steps': self.toi, 'setting': self.setting, 'qoi': self.qoi, 'results': stats}, fd) fd.close()
def testSettings(self): builder = ParameterBuilder() dp = builder.defineDeterministicParameters() up = builder.defineUncertainParameters() # ============================================ # 1) up.new().isCalled('v')\ .withDistribution(Uniform(0, 1))\ .withRosenblattTransformation() # -------------------------------------------- # 2) up.new().isCalled('density')\ .withDistribution(Uniform(-1, 1))\ .hasValue(0.0) # -------------------------------------------- # 3) up.new().isCalled('K')\ .withDistribution(TNormal(0, 1, -3, 2))\ .hasValue(-3) # -------------------------------------------- # 4) up.new().isCalled('theta')\ .withDistribution(TNormal(0, 1, -2, 2))\ .withLinearTransformation() # -------------------------------------------- # 5) up.new().isCalled('blub')\ .withUniformDistribution(-1, 1) # -------------------------------------------- # 6) dp.new().isCalled('radius').hasValue(2) # ============================================ params = builder.andGetResult() # test dimensions assert params.getDim() == 6 assert params.getStochasticDim() == 3 assert len(params.activeParams()) == 3 assert params.getStochasticDim() == len(params.activeParams()) assert params.getDim() - len(params.uncertainParams()) == \ len(params.deterministicParams()) assert params.getStochasticDim() == len(params.getDistributions()) - 2 jsonStr = params.getJointTransformation().toJson() jsonObject = json.loads(jsonStr) trans = Transformation.fromJson(jsonObject) # test transformations ap = params.activeParams() assert params.getStochasticDim() == len(ap) sampler = MCSampler.withNaiveSampleGenerator(params) for sample in sampler.nextSamples(100): for x in sample.getActiveUnit(): assert 0 <= x <= 1 bounds = params.getBounds() q = sample.getExpandedProbabilistic() for xlim1, xlim2, x in np.vstack((bounds.T, q)).T: assert xlim1 <= x <= xlim2 params.removeParam(0) assert params.getStochasticDim() == len(ap) - 1 sampler = MCSampler.withNaiveSampleGenerator(params) for sample in sampler.nextSamples(100): for x in sample.getActiveUnit(): assert 0 <= x <= 1 bounds = params.getBounds() q = sample.getExpandedProbabilistic() for xlim1, xlim2, x in np.vstack((bounds.T, q)).T: assert xlim1 <= x <= xlim2