def test_cuboids(lb, ub, RVstart, RVend, cuboid_volume): probmass = [] subdiams = [] tot_diam = [] solved_p = [] # subdivisions for i in range(len(lb)): if DEBUG: print("\n") print(" lower bounds: %s" % lb[i]) print(" upper bounds: %s" % ub[i]) if i in NEW_SLICES or not NEW_SLICES: subcuboid_volume = volume(lb[i], ub[i]) sub_prob_mass = prob_mass(subcuboid_volume, cuboid_volume) probmass.append(sub_prob_mass) if DEBUG: print(" probability mass: %s" % sub_prob_mass) solved, diameter, subdiameters = UQ(RVstart, RVend, lb[i], ub[i]) solved_p.append(solved) subdiams.append(subdiameters) tot_diam.append(diameter) else: probmass.append(PROBABILITY_MASS[i]) if DEBUG: print(" probability mass: %s" % PROBABILITY_MASS[i]) solved_p.append(SOLVED_PARAMETERS[i]) subdiams.append(SUB_DIAMETERS[i]) tot_diam.append(TOTAL_DIAMETERS[i]) return solved_p, subdiams, tot_diam, probmass
def test_cuboids(lb, ub, RVstart, RVend, cuboid_volume): probmass = [] subdiams = [] tot_diam = [] solved_p = [] # subdivisions for i in range(len(lb)): if DEBUG: print "\n" print " lower bounds: %s" % lb[i] print " upper bounds: %s" % ub[i] if i in NEW_SLICES or not NEW_SLICES: subcuboid_volume = volume(lb[i], ub[i]) sub_prob_mass = prob_mass(subcuboid_volume, cuboid_volume) probmass.append(sub_prob_mass) if DEBUG: print " probability mass: %s" % sub_prob_mass solved, diameter, subdiameters = UQ(RVstart, RVend, lb[i], ub[i]) solved_p.append(solved) subdiams.append(subdiameters) tot_diam.append(diameter) else: probmass.append(PROBABILITY_MASS[i]) if DEBUG: print " probability mass: %s" % PROBABILITY_MASS[i] solved_p.append(SOLVED_PARAMETERS[i]) subdiams.append(SUB_DIAMETERS[i]) tot_diam.append(TOTAL_DIAMETERS[i]) return solved_p, subdiams, tot_diam, probmass
def monte_carlo_integrate(f, lb, ub, n=10000): #XXX ok default for n? """ Returns the integral of an m-dimensional function f from lb to ub using a Monte Carlo integration of n points Inputs: f -- a function that takes a list and returns a number. lb -- a list of lower bounds ub -- a list of upper bounds n -- the number of points to sample [Default is n=10000] References: This code is adapted from A Primer on Scientific Programming with Python by Hans Petter Langtangen, page 443-445. Also, see http://en.wikipedia.org/wiki/Monte_Carlo_integration and http://math.fullerton.edu/mathews/n2003/MonteCarloMod.html """ from mystic.math.stats import volume from mystic.math.samples import random_samples vol = volume(lb, ub) x = [random_samples(lb, ub, npts=1) for k in range(1, n + 1)] r = map(f, x) #FIXME: , nnodes=nnodes, launcher=launcher) s = sum(r)[0] I = (vol / k) * s return float(I)
def monte_carlo_integrate(f, lb, ub, n=10000): #XXX ok default for n? """ Returns the integral of an m-dimensional function f from lb to ub using a Monte Carlo integration of n points Inputs: f -- a function that takes a list and returns a number. lb -- a list of lower bounds ub -- a list of upper bounds n -- the number of points to sample [Default is n=10000] References: 1. "A Primer on Scientific Programming with Python", by Hans Petter Langtangen, page 443-445, 2014. 2. http://en.wikipedia.org/wiki/Monte_Carlo_integration 3. http://math.fullerton.edu/mathews/n2003/MonteCarloMod.html """ from mystic.math.stats import volume from mystic.math.samples import random_samples vol = volume(lb, ub) x = [random_samples(lb, ub, npts=1) for k in range(1, n+1)] r = map(f, x) #FIXME: , nnodes=nnodes, launcher=launcher) s = sum(r)[0] I = (vol/n)*s return float(I)
def integrated_mean(f, lb, ub): """ calculate the integrated mean of a function f Inputs: f -- a function that takes a list and returns a number lb -- a list of lower bounds ub -- a list of upper bounds """ expectation = integrate(f, lb, ub) from mystic.math.stats import mean, volume vol = volume(lb, ub) return mean(expectation, vol)
def integrated_mean(f, lb, ub): """ calculate the integrated mean of a function f Inputs: f -- a function that takes a list and returns a number lb -- a list of lower bounds ub -- a list of upper bounds """ expectation = integrate(f, lb, ub) from mystic.math.stats import mean,volume vol = volume(lb, ub) return mean(expectation, vol)
print(" model: f(x) = %s(x)" % function_name) RVmax = len(lower_bounds) param_string = "[" for i in range(RVmax): param_string += "'x%s'" % str(i + 1) if i == (RVmax - 1): param_string += "]" else: param_string += ", " print(" parameters: %s" % param_string) # get diameter for entire cuboid lb, ub = [lower_bounds], [upper_bounds] cuboid_volume = volume(lb[0], ub[0]) params0, subdiams0, diam0, probmass0 = test_cuboids(lb,ub,RVstart,RVend,\ cuboid_volume) SOLVED_PARAMETERS, SUB_DIAMETERS = params0, subdiams0 TOTAL_DIAMETERS, PROBABILITY_MASS = diam0, probmass0 if not DEBUG: failure, success = sample(model, lb[0], ub[0]) pof = float(failure) / float(failure + success) print("Exact PoF: %s" % pof) for i in range(len(lb)): print("\n") print(" lower bounds: %s" % lb[i]) print(" upper bounds: %s" % ub[i]) for solved in params0[0]:
print " model: f(x) = %s(x)" % function_name RVmax = len(lower_bounds) param_string = "[" for i in range(RVmax): param_string += "'x%s'" % str(i + 1) if i == (RVmax - 1): param_string += "]" else: param_string += ", " print " parameters: %s" % param_string # get diameter for entire cuboid lb, ub = [lower_bounds], [upper_bounds] cuboid_volume = volume(lb[0], ub[0]) params0, subdiams0, diam0, probmass0 = test_cuboids(lb, ub, RVstart, RVend, cuboid_volume) SOLVED_PARAMETERS, SUB_DIAMETERS = params0, subdiams0 TOTAL_DIAMETERS, PROBABILITY_MASS = diam0, probmass0 if not DEBUG: failure, success = sample(model, lb[0], ub[0]) pof = float(failure) / float(failure + success) print "Exact PoF: %s" % pof for i in range(len(lb)): print "\n" print " lower bounds: %s" % lb[i] print " upper bounds: %s" % ub[i] for solved in params0[0]: print "solved: %s" % solved
print(" model: f(x) = %s(x)" % function_name) param_string = "[" for i in range(RVmax): param_string += "'x%s'" % str(i + 1) if i == (RVmax - 1): param_string += "]" else: param_string += ", " print(" parameters: %s" % param_string) print(" lower bounds: %s" % lower_bounds) print(" upper bounds: %s" % upper_bounds) # print(" ...") #cuboid_volume = volume(lower_bounds,upper_bounds) cuboid_volume = volume(lbounds, ubounds) probability_mass = prob_mass(cuboid_volume, cuboid_volume) print(" probability mass: %s" % probability_mass) expectation = expectation_value(model, lower_bounds, upper_bounds) print(" expectation: %s" % expectation) mean_value = mean(expectation, cuboid_volume) print(" mean value: %s" % mean_value) diameter = UQ(RVstart, RVend, lower_bounds, upper_bounds) mcdiarmid = mcdiarmid_bound(mean_value, sqrt(diameter)) print("McDiarmid bound: %s" % mcdiarmid) #if RVstart != 0 or RVend != 2: # abort when not a 3-D problem # break #FIXME: break? or exit? or pass/else? or ??? weighted_bound = [] sanity = []
print " model: f(x) = %s(x)" % function_name param_string = "[" for i in range(RVmax): param_string += "'x%s'" % str(i+1) if i == (RVmax - 1): param_string += "]" else: param_string += ", " print " parameters: %s" % param_string print " lower bounds: %s" % lower_bounds print " upper bounds: %s" % upper_bounds # print " ..." #cuboid_volume = volume(lower_bounds,upper_bounds) cuboid_volume = volume(lbounds,ubounds) probability_mass = prob_mass(cuboid_volume,cuboid_volume) print " probability mass: %s" % probability_mass expectation = expectation_value(model,lower_bounds,upper_bounds) print " expectation: %s" % expectation mean_value = mean(expectation,cuboid_volume) print " mean value: %s" % mean_value diameter = UQ(RVstart,RVend,lower_bounds,upper_bounds) mcdiarmid = mcdiarmid_bound(mean_value,sqrt(diameter)) print "McDiarmid bound: %s" % mcdiarmid #if RVstart != 0 or RVend != 2: # abort when not a 3-D problem # break #FIXME: break? or exit? or pass/else? or ??? weighted_bound = [] sanity = []