def fEx(p, sampleType, n, qInfo, fExName): """ Generate synthetic training data """ # (a) xTrain nSamp = n[0] * n[1] xi = [] q = [] qBound = [] if sampleType[0] == 'LHS' and sampleType[1] == 'LHS': if distType == ['Unif'] * p: qBound = qInfo xi = sampling.LHS_sampling(nSamp, [[-1, 1]] * p) xTrain = np.zeros((nSamp, p)) for i in range(p): xTrain[:, i] = pce.mapFromUnit(xi[:, i], qBound[i]) fEx_ = analyticTestFuncs.fEx2D(xTrain[:, 0], xTrain[:, 1], fExName, 'comp') else: raise ValueError( "LHS works only when all q have 'Unif' distribution.") else: for i in range(p): samps = sampling.trainSample(sampleType=sampleType[i], GQdistType=distType[i], qInfo=qInfo[i], nSamp=n[i]) q.append(samps.q) xTrain = reshaper.vecs2grid(q) fEx_ = analyticTestFuncs.fEx2D(q[0], q[1], fExName, 'tensorProd') return xTrain, fEx_
def trainDataGen(p,sampleType,n,qBound,fExName,noiseType): """ Generate Training Data """ # (a) xTrain if sampleType=='grid': nSamp=n[0]*n[1] gridList=[] for i in range(p): #grid_=torch.linspace(qBound[i][0],qBound[i][1],n[i]) #torch grid_=np.linspace(qBound[i][0],qBound[i][1],n[i]) gridList.append(grid_) xTrain=reshaper.vecs2grid(gridList) # xTrain = gpytorch.utils.grid.create_data_from_grid(gridList) #torch elif sampleType=='random': nSamp=n #number of random samples xTrain=sampling.LHS_sampling(n,qBound) # (b) Observation noise #noiseSdev=torch.ones(nTot).mul(0.1) #torch noiseSdev=noiseGen(nSamp,noiseType,xTrain,fExName) #yTrain = torch.sin(mt.pi*xTrain[:,0])*torch.cos(.25*mt.pi*xTrain[:,1])+ # torch.randn_like(xTrain[:,0]).mul(0.1) #torch # (c) Training response yTrain=analyticTestFuncs.fEx2D(xTrain[:,0],xTrain[:,1],fExName,'comp').val yTrain_noiseFree=yTrain yTrain=yTrain_noiseFree+noiseSdev*np.random.randn(nSamp) return xTrain,yTrain,noiseSdev,yTrain_noiseFree
def trainDataGen(p, sampleType, n, qBound, fExName, noiseType): """ Generate Training Data """ # (a) xTrain if sampleType == 'grid': nSamp = n[0] * n[1] gridList = [] for i in range(p): grid_ = np.linspace(qBound[i][0], qBound[i][1], n[i]) gridList.append(grid_) xTrain = reshaper.vecs2grid(gridList) elif sampleType == 'random': nSamp = n # number of random samples xTrain = sampling.LHS_sampling(n, qBound) # (b) Observation noise noiseSdev = noiseGen(nSamp, noiseType, xTrain, fExName) # (c) Training response yTrain = analyticTestFuncs.fEx2D(xTrain[:, 0], xTrain[:, 1], fExName, 'comp').val yTrain_noiseFree = yTrain yTrain = yTrain_noiseFree + noiseSdev * np.random.randn(nSamp) return xTrain, yTrain, noiseSdev, yTrain_noiseFree
def psobol_Ishigami_test(): """ Test for psobol for 3 uncertain parameters. Sobol indices are computed for f(q1,q2,q3)=Ishigami available as analyticTestFuncs.fEx3D('Ishigami'). The resulting psobol indices can be compared to the standard Sobol indices in order to verify the implementation of psobol. """ # -------------------------- #------- SETTINGS # definition of the parameters qBound = [ [-pi, pi], # admissible range of parameters [-pi, pi], [-pi, pi] ] # options for Training data # n=[100, 70, 80] #number of samples for q1, q2, q3 n = 500 # number of LHS random samples a = 7 # parameters in Ishigami function b = 0.1 noise_sdev = 0.2 # standard-deviation of observation noise # options for Sobol indices nMC = 500 # number of MC samples to compute psobol indices # number of test points in each parameter dimension to compute integrals in Sobol indices nTest = [20, 21, 22] # options for GPR nIter_gpr_ = 100 # number of iterations in optimization of GPR hyperparameters lr_gpr_ = 0.05 # learning rate in the optimization of the hyperparameters convPlot_gpr_ = True # plot convergence of optimization of GPR hyperparameters # -------------------------- p = len(qBound) # (1) Generate training data qTrain = sampling.LHS_sampling(n, qBound) # LHS random samples fEx_ = analyticTestFuncs.fEx3D(qTrain[:, 0], qTrain[:, 1], qTrain[:, 2], 'Ishigami', 'comp', { 'a': a, 'b': b }) yTrain = fEx_.val + noise_sdev * np.random.randn(n) noiseSdev = noise_sdev * np.ones(n) # (2) Create the test samples and associated PDF qTest = [] pdf = [] # qBound=[[-1,1],[-1,1],[-1,1]] # for i in range(3): # qTrain[:,i]=(qTrain[:,i]+pi)/(2*pi)*2-1 for i in range(p): qTest.append(np.linspace(qBound[i][0], qBound[i][1], nTest[i])) pdf.append(np.ones(nTest[i]) / (qBound[i][1] - qBound[i][0])) # (3) Assemble the psobolOpts dict psobolDict = { 'nMC': nMC, 'qTest': qTest, 'pdf': pdf, 'nIter_gpr': nIter_gpr_, 'lr_gpr': lr_gpr_, 'convPlot_gpr': convPlot_gpr_ } # (4) Construct probabilistic Sobol indices psobol_ = psobol(qTrain, yTrain, noiseSdev, psobolDict) Si_samps = psobol_.Si_samps Sij_samps = psobol_.Sij_samps STi_samps = psobol_.STi_samps SijName = psobol_.SijName # (a) Compute mean and sdev of the estimated indices for i in range(p): print('Main Sobol index S%d: mean=%g, sdev=%g', str(i + 1), np.mean(Si_samps[i]), np.std(Si_samps[i])) print('Total Sobol index ST%d: mean=%g, sdev=%g', str(i + 1), np.mean(STi_samps[i]), np.std(STi_samps[i])) print('Interacting Sobol indices S12: mean=%g, sdev=%g', np.mean(Sij_samps[0]), np.std(Sij_samps[0])) print('Interacting Sobol indices S13: mean=%g, sdev=%g', np.mean(Sij_samps[1]), np.std(Sij_samps[1])) print('Interacting Sobol indices S23: mean=%g, sdev=%g', np.mean(Sij_samps[2]), np.std(Sij_samps[2])) # (b) plot histogram and fitted pdf to different Sobol indices statsUQit.pdfFit_uniVar(Si_samps[0], True, []) statsUQit.pdfFit_uniVar(Si_samps[1], True, []) statsUQit.pdfFit_uniVar(Sij_samps[0], True, []) # (c) plot samples of the Sobol indices plt.figure(figsize=(10, 5)) for i in range(p): plt.plot(Si_samps[i], '-', label='S' + str(i + 1)) plt.plot(STi_samps[i], '--', label='ST' + str(i + 1)) plt.plot(Si_samps[i], ':', label='S' + str(12)) plt.legend(loc='best', fontsize=14) plt.xlabel('sample', fontsize=14) plt.ylabel('Sobol indices', fontsize=14) plt.show()
def pce_2d_test(): """ Test PCE for 2D uncertain parameter """ #---- SETTINGS------------ #Parameters specifications distType = ['Norm', 'Norm'] #distribution type of the parameters q1, q2 qInfo = [ [-2, 1], #info on parameters [-2, 0.4] ] nQ = [7, 6] #number of training samples of parameters nTest = [121, 120] #number of test points in parameter spaces to evaluate PCE #PCE Options truncMethod = 'TO' #'TP'=Tensor Product #'TO'=Total Order sampleType = [ 'GQ', 'GQ' ] #'GQ'=Gauss Quadrature nodes ('Projection' or 'Regression') #For other type of samples, see sampling.py, trainSample => only 'Regression' can be used #'LHS': Latin Hypercube Sampling (only when all distType='Unif') fType = 'type1' #Type of the exact model response, 'type1', 'type2', 'type3', 'Rosenbrock' pceSolveMethod = 'Regression' #'Regression': for any combination of sampling and truncation methods #'Projection': only for 'GQ'+'TP' if truncMethod == 'TO': LMax = 8 #max polynomial order in each parameter dimention #------------------------ p = len(distType) #Assemble the pceDict pceDict = { 'p': p, 'truncMethod': truncMethod, 'sampleType': sampleType, 'pceSolveMethod': pceSolveMethod, 'distType': distType } if truncMethod == 'TO': pceDict.update({'LMax': LMax, 'pceSolveMethod': 'Regression'}) #Generate the training data xi = [] q = [] qBound = [] if sampleType[0] == 'LHS' and sampleType[1] == 'LHS': if distType == ['Unif'] * p: qBound = qInfo xi = sampling.LHS_sampling(nQ[0] * nQ[1], [[-1, 1]] * p) for i in range(p): q.append(pce.mapFromUnit(xi[:, i], qBound[i])) fEx_ = analyticTestFuncs.fEx2D(q[0], q[1], fType, 'comp') xiGrid = xi else: raise ValueError( "LHS works only when all q have 'Unif' distribution.") else: for i in range(p): samps = sampling.trainSample(sampleType=sampleType[i], GQdistType=distType[i], qInfo=qInfo[i], nSamp=nQ[i]) q.append(samps.q) xi.append(samps.xi) qBound.append(samps.qBound) fEx_ = analyticTestFuncs.fEx2D(q[0], q[1], fType, 'tensorProd') xiGrid = reshaper.vecs2grid(xi) fVal = fEx_.val #Construct the PCE pce_ = pce(fVal=fVal, xi=xiGrid, pceDict=pceDict, nQList=nQ) fMean = pce_.fMean fVar = pce_.fVar pceCoefs = pce_.coefs kSet = pce_.kSet #Plot the convergence indicator of the PCE convPlot(coefs=pceCoefs, distType=distType, kSet=kSet) #Generate test samples for the parameters and evaluate the exact response surface at them qTest = [] xiTest = [] for i in range(p): testSamps = sampling.testSample('unifSpaced', GQdistType=distType[i], qInfo=qInfo[i], qBound=qBound[i], nSamp=nTest[i]) qTest_ = testSamps.q xiTest_ = testSamps.xi qTest.append(qTest_) xiTest.append(xiTest_) fTest = analyticTestFuncs.fEx2D(qTest[0], qTest[1], fType, 'tensorProd').val #Evaluate PCE at the test samples pcePred_ = pceEval(coefs=pceCoefs, xi=xiTest, distType=distType, kSet=kSet) fPCE = pcePred_.pceVal #Use MC method to directly estimate reference values for the mean and varaiance of f(q) fEx_.moments(distType, qInfo) fMean_mc = fEx_.mean fVar_mc = fEx_.var #Compare the PCE estimates for moments of f(q) with the reference values from MC print(writeUQ.printRepeated('-', 70)) print('------------ MC -------- PCE --------- Error % ') print('Mean of f(q) = %g\t%g\t%g' % (fMean_mc, fMean, (fMean - fMean_mc) / fMean_mc * 100.)) print('Var of f(q) = %g\t%g\t%g' % (fVar_mc, fVar, (fVar - fVar_mc) / fVar_mc * 100.)) print(writeUQ.printRepeated('-', 70)) #Plot the exact and PCE response surfaces as contours in the parameters space # Create 2D grid from the test samples and plot the contours of response surface over it fTestGrid = fTest.reshape(nTest, order='F') fErrorGrid = (abs(fTestGrid - fPCE)) # 2D grid from the sampled parameters if sampleType[0] == 'LHS' and sampleType[1] == 'LHS': qGrid = reshaper.vecsGlue(q[0], q[1]) else: qGrid = reshaper.vecs2grid(q) plt.figure(figsize=(21, 8)) plt.subplot(1, 3, 1) ax = plt.gca() CS1 = plt.contour(qTest[0], qTest[1], fTestGrid.T, 40) plt.clabel(CS1, inline=True, fontsize=13, colors='k', fmt='%0.2f', rightside_up=True, manual=False) plt.plot(qGrid[:, 0], qGrid[:, 1], 'o', color='r', markersize=7) plt.xlabel(r'$q_1$') plt.ylabel(r'$q_2$') plt.title('Exact Response') plt.subplot(1, 3, 2) ax = plt.gca() CS2 = plt.contour(qTest[0], qTest[1], fPCE.T, 40) plt.clabel(CS2, inline=True, fontsize=13, colors='k', fmt='%0.2f', rightside_up=True, manual=False) plt.plot(qGrid[:, 0], qGrid[:, 1], 'o', color='r', markersize=7) plt.xlabel(r'$q_1$') plt.ylabel(r'$q_2$') plt.title('PCE Response') plt.subplot(1, 3, 3) ax = plt.gca() CS3 = plt.contour(qTest[0], qTest[1], fErrorGrid.T, 40) plt.clabel(CS3, inline=True, fontsize=13, colors='k', fmt='%0.2f', rightside_up=True, manual=False) plt.xlabel(r'$q_1$') plt.ylabel(r'$q_2$') plt.plot(qGrid[:, 0], qGrid[:, 1], 'o', color='r', markersize=7) plt.title('|Exact-Surrogate|') plt.show()