- Timestamp:
- 06/04/13 11:04:45 (3 years ago)
- Location:
- branches/UQ/math/examples
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UQ/math/examples/MM_surrogate_diam.py
r675 r676 65 65 from mystic.strategy import Best1Exp 66 66 from mystic.monitors import VerboseMonitor, Monitor 67 from mystic.tools import getch,random_seed67 from mystic.tools import random_seed 68 68 69 69 random_seed(123) -
branches/UQ/math/examples/MPI2_helper.py
r471 r676 10 10 11 11 12 13 def func_pickle(func, suffix='.pik'): 12 def func_pickle(func, suffix='.pik', dir='.'): 14 13 """ standard pickle.dump of function to a NamedTemporaryFile """ 15 import dill as pickle 16 import tempfile 17 file = tempfile.NamedTemporaryFile(suffix=suffix, dir='.') 18 pickle.dump(func, file) 19 file.flush() 20 return file 21 22 23 from numpy import asarray 24 25 def ndim_meshgrid(*arrs): 26 """n-dimensional analogue to numpy.meshgrid""" 27 arrs = tuple(reversed(arrs)) #edit 28 lens = map(len, arrs) 29 dim = len(arrs) 30 31 sz = 1 32 for s in lens: 33 sz*=s 34 35 ans = [] 36 for i, arr in enumerate(arrs): 37 slc = [1]*dim 38 slc[i] = lens[i] 39 arr2 = asarray(arr).reshape(slc) 40 for j, sz in enumerate(lens): 41 if j!=i: 42 arr2 = arr2.repeat(sz, axis=j) 43 ans.append(arr2) 44 45 return tuple(ans) 46 47 48 def gridpts(q): 49 """ 50 takes a list of lists of equal length q = [[1,2],[3,4]] 51 and produces a list of gridpoints g = [[1,3],[1,4],[2,3],[2,4]] 52 """ 53 q = list(reversed(q)) 54 w = ndim_meshgrid(*q) 55 for i in range(len(q)): 56 q[i] = list( w[i].reshape(w[i].size) ) 57 q = zip(*q) 58 return [list(i) for i in q] 14 from dill.temp import dump 15 return dump(func, suffix=suffix, dir=dir) 59 16 60 17 … … 66 23 from mystic.termination import NormalizedChangeOverGeneration as NCOG 67 24 from mystic.monitors import VerboseMonitor, Monitor 68 from mystic.tools import getch69 25 70 26 maxiter = 1000 -
branches/UQ/math/examples/MPI2_surrogate_diam_batchgrid.py
r675 r676 72 72 73 73 # build a grid of starting points 74 from MPI2_helper import gridpts, local_optimize 74 from MPI2_helper import local_optimize 75 from mystic.math.grid import gridpts 75 76 initial_values = gridpts(bins) 76 77 -
branches/UQ/math/examples/MPI_surrogate_diam.py
r675 r676 65 65 from mystic.strategy import Best1Exp 66 66 from mystic.monitors import VerboseMonitor, Monitor 67 from mystic.tools import getch,random_seed67 from mystic.tools import random_seed 68 68 69 69 random_seed(123) -
branches/UQ/math/examples/MPI_surrogate_diam_batchgrid.py
r675 r676 1 1 #!/usr/bin/env python 2 2 3 def func_pickle(func, suffix='.pik' ):3 def func_pickle(func, suffix='.pik', dir='.'): 4 4 """ standard pickle.dump of function to a NamedTemporaryFile """ 5 import dill as pickle 6 import tempfile 7 file = tempfile.NamedTemporaryFile(suffix=suffix, dir='.') 8 pickle.dump(func, file) 9 file.flush() 10 return file 11 12 13 from numpy import asarray 14 15 def ndim_meshgrid(*arrs): 16 """n-dimensional analogue to numpy.meshgrid""" 17 arrs = tuple(reversed(arrs)) #edit 18 lens = map(len, arrs) 19 dim = len(arrs) 20 21 sz = 1 22 for s in lens: 23 sz*=s 24 25 ans = [] 26 for i, arr in enumerate(arrs): 27 slc = [1]*dim 28 slc[i] = lens[i] 29 arr2 = asarray(arr).reshape(slc) 30 for j, sz in enumerate(lens): 31 if j!=i: 32 arr2 = arr2.repeat(sz, axis=j) 33 ans.append(arr2) 34 35 return tuple(ans) 36 37 38 def gridpts(q): 39 """ 40 takes a list of lists of equal length q = [[1,2],[3,4]] 41 and produces a list of gridpoints g = [[1,3],[1,4],[2,3],[2,4]] 42 """ 43 q = list(reversed(q)) 44 w = ndim_meshgrid(*q) 45 for i in range(len(q)): 46 q[i] = list( w[i].reshape(w[i].size) ) 47 q = zip(*q) 48 return [list(i) for i in q] 5 from dill.temp import dump 6 return dump(func, suffix=suffix, dir=dir) 7 8 from mystic.math.grid import gridpts 49 9 50 10 … … 114 74 from mystic.termination import NormalizedChangeOverGeneration as NCOG 115 75 from mystic.monitors import VerboseMonitor, Monitor 116 from mystic.tools import getch117 76 118 77 maxiter = 1000 -
branches/UQ/math/examples/MPI_surrogate_diam_scatter.py
r675 r676 1 1 #!/usr/bin/env python 2 2 3 def func_pickle(func, suffix='.pik' ):3 def func_pickle(func, suffix='.pik', dir='.'): 4 4 """ standard pickle.dump of function to a NamedTemporaryFile """ 5 import dill as pickle 6 import tempfile 7 file = tempfile.NamedTemporaryFile(suffix=suffix, dir='.') 8 pickle.dump(func, file) 9 file.flush() 10 return file 11 12 13 def random_samples(lb,ub,npts=10000): 14 "generate npts random samples between given lb & ub" 15 from numpy.random import random 16 dim = len(lb) 17 pts = random((dim,npts)) 18 for i in range(dim): 19 pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] 20 return pts 21 22 23 def samplepts(lb,ub,npts): 24 """ 25 takes upper and lower bounds (e.g. ub = [2,4], lb = [0,3]) 26 produces a list of sample points s = [[1,3],[1,4],[2,3],[2,4]] 27 """ 28 q = random_samples(lb,ub,npts) 29 q = [list(i) for i in q] 30 q = zip(*q) 31 return [list(i) for i in q] 5 from dill.temp import dump 6 return dump(func, suffix=suffix, dir=dir) 7 8 from mystic.math.grid import samplepts 32 9 33 10 … … 97 74 from mystic.termination import NormalizedChangeOverGeneration as NCOG 98 75 from mystic.monitors import VerboseMonitor, Monitor 99 from mystic.tools import getch100 76 101 77 maxiter = 1000 -
branches/UQ/math/examples/MSUB_surrogate_diam_batchgrid.py
r675 r676 1 1 #!/usr/bin/env python 2 2 3 def func_pickle(func, suffix='.pik' ):3 def func_pickle(func, suffix='.pik', dir='.'): 4 4 """ standard pickle.dump of function to a NamedTemporaryFile """ 5 import dill as pickle 6 import tempfile 7 file = tempfile.NamedTemporaryFile(suffix=suffix, dir='.') 8 pickle.dump(func, file) 9 file.flush() 10 return file 11 12 13 from numpy import asarray 14 15 def ndim_meshgrid(*arrs): 16 """n-dimensional analogue to numpy.meshgrid""" 17 arrs = tuple(reversed(arrs)) #edit 18 lens = map(len, arrs) 19 dim = len(arrs) 20 21 sz = 1 22 for s in lens: 23 sz*=s 24 25 ans = [] 26 for i, arr in enumerate(arrs): 27 slc = [1]*dim 28 slc[i] = lens[i] 29 arr2 = asarray(arr).reshape(slc) 30 for j, sz in enumerate(lens): 31 if j!=i: 32 arr2 = arr2.repeat(sz, axis=j) 33 ans.append(arr2) 34 35 return tuple(ans) 36 37 38 def gridpts(q): 39 """ 40 takes a list of lists of equal length q = [[1,2],[3,4]] 41 and produces a list of gridpoints g = [[1,3],[1,4],[2,3],[2,4]] 42 """ 43 q = list(reversed(q)) 44 w = ndim_meshgrid(*q) 45 for i in range(len(q)): 46 q[i] = list( w[i].reshape(w[i].size) ) 47 q = zip(*q) 48 return [list(i) for i in q] 5 from dill.temp import dump 6 return dump(func, suffix=suffix, dir=dir) 7 8 from mystic.math.grid import gridpts 49 9 50 10 -
branches/UQ/math/examples/QSUB2_surrogate_diam_batchgrid.py
r675 r676 72 72 73 73 # build a grid of starting points 74 from MPI2_helper import gridpts, local_optimize 74 from mystic.math.grid import gridpts 75 from MPI2_helper import local_optimize 75 76 from MPI2_helper import nnodes, queue, timelimit 76 77 initial_values = gridpts(bins) -
branches/UQ/math/examples/QSUB_surrogate_diam_batchgrid.py
r675 r676 1 1 #!/usr/bin/env python 2 2 3 def func_pickle(func, suffix='.pik' ):3 def func_pickle(func, suffix='.pik', dir='.'): 4 4 """ standard pickle.dump of function to a NamedTemporaryFile """ 5 import dill as pickle 6 import tempfile 7 file = tempfile.NamedTemporaryFile(suffix=suffix, dir='.') 8 pickle.dump(func, file) 9 file.flush() 10 return file 11 12 13 from numpy import asarray 14 15 def ndim_meshgrid(*arrs): 16 """n-dimensional analogue to numpy.meshgrid""" 17 arrs = tuple(reversed(arrs)) #edit 18 lens = map(len, arrs) 19 dim = len(arrs) 20 21 sz = 1 22 for s in lens: 23 sz*=s 24 25 ans = [] 26 for i, arr in enumerate(arrs): 27 slc = [1]*dim 28 slc[i] = lens[i] 29 arr2 = asarray(arr).reshape(slc) 30 for j, sz in enumerate(lens): 31 if j!=i: 32 arr2 = arr2.repeat(sz, axis=j) 33 ans.append(arr2) 34 35 return tuple(ans) 36 37 38 def gridpts(q): 39 """ 40 takes a list of lists of equal length q = [[1,2],[3,4]] 41 and produces a list of gridpoints g = [[1,3],[1,4],[2,3],[2,4]] 42 """ 43 q = list(reversed(q)) 44 w = ndim_meshgrid(*q) 45 for i in range(len(q)): 46 q[i] = list( w[i].reshape(w[i].size) ) 47 q = zip(*q) 48 return [list(i) for i in q] 5 from dill.temp import dump 6 return dump(func, suffix=suffix, dir=dir) 7 8 from mystic.math.grid import gridpts 49 9 50 10 … … 118 78 from mystic.termination import NormalizedChangeOverGeneration as NCOG 119 79 from mystic.monitors import VerboseMonitor, Monitor 120 from mystic.tools import getch121 80 122 81 maxiter = 1000 -
branches/UQ/math/examples/TEST_surrogate_McD.py
r471 r676 66 66 from mystic.strategy import Best1Exp 67 67 from mystic.monitors import VerboseMonitor, Monitor 68 from mystic.tools import getch,random_seed68 from mystic.tools import random_seed 69 69 70 70 random_seed(123) … … 128 128 # probability mass, expectation, mean, diameter, McDiarmid 129 129 ####################################################################### 130 def volume(lb,ub): 131 """volume for a uniform distribution in n-dimensions""" 132 vol = 1 133 for i in range(len(ub)): 134 vol *= abs(ub[i] - lb[i]) 135 return vol 136 137 def prob_mass(volume,norm): 138 """probability mass ...""" 139 return volume / norm #XXX: norm != 0 140 141 from scipy.integrate import quad, dblquad, tplquad 142 def expectation_value(f,lb,ub): #XXX: should be generalized to n-dimensions 143 """expectation value for an n-dimensional function; n in [1,2,3]""" 144 if len(lb) == 3: 145 def func(z,y,x): return f([x,y,z]) 146 def qf(x,y): return lb[2] 147 def rf(x,y): return ub[2] 148 def gf(x): return lb[1] 149 def hf(x): return ub[1] 150 expectation,confidence = tplquad(func,lb[0],ub[0],gf,hf,qf,rf) 151 return expectation 152 if len(lb) == 2: 153 def func(y,x): return f([x,y]) 154 def gf(x): return lb[1] 155 def hf(x): return ub[1] 156 expectation,confidence = dblquad(func,lb[0],ub[0],gf,hf) 157 return expectation 158 if len(lb) == 1: 159 expectation,confidence = quad(f,lb[0],ub[0]) 160 return expectation 161 #raise Exception, "Function must be either 1-D, 2-D, or 3-D" 162 #### FIXME: instead of exception above, use hack for > 3D 163 print "WARNING: Dimensions > 3-D are assumed as constant at lower bound" 164 def func(z,y,x): return f([x,y,z]+lb[3:]) 165 def qf(x,y): return lb[2] 166 def rf(x,y): return ub[2] 167 def gf(x): return lb[1] 168 def hf(x): return ub[1] 169 expectation,confidence = tplquad(func,lb[0],ub[0],gf,hf,qf,rf) 170 return expectation 171 #### END HACK 172 173 def mean(expectation,volume): 174 """mean ...""" 175 return expectation / volume #XXX: volume != 0 176 177 def mcdiarmid_bound(mean,diameter): 178 """McDiarmid ...""" 179 from math import exp 180 if not diameter: return 1.0 #XXX: define e^(0/0) = 1 181 return exp(-2.0 * (max(0,mean))**2 / diameter**2) 130 from mystic.math.stats import volume, prob_mass, mean, mcdiarmid_bound 131 from mystic.math.integrate import integrate as expectation_value 182 132 183 133 -
branches/UQ/math/examples/TEST_surrogate_cut.py
r471 r676 7 7 # (similar to concentration.in) 8 8 ####################################################################### 9 from TEST_surrogate_McD import volume, prob_mass10 from TEST_surrogate_McD import expectation_value, mean, mcdiarmid_bound11 9 from TEST_surrogate_diam import * # model, limit 12 13 def sample(f,lb,ub,npts=10000): 14 from numpy.random import random 15 pts = random((3,npts)) 16 for i in range(3): 17 pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] 18 19 failure = 0; success = 0 20 for i in range(npts): 21 if f([pts[0][i],pts[1][i],pts[2][i]]): 22 success += 1 23 else: 24 failure += 1 25 return failure,success 10 from mystic.math.stats import volume, prob_mass, mean, mcdiarmid_bound 11 from mystic.math.integrate import integrate as expectation_value 12 from mystic.math.samples import sample 26 13 27 14 … … 35 22 from mystic.strategy import Best1Exp 36 23 from mystic.monitors import VerboseMonitor, Monitor 37 from mystic.tools import getch,random_seed24 from mystic.tools import random_seed 38 25 39 26 random_seed(123) -
branches/UQ/math/examples/TEST_surrogate_diam.py
r471 r676 63 63 from mystic.strategy import Best1Exp 64 64 from mystic.monitors import VerboseMonitor, Monitor 65 from mystic.tools import getch,random_seed65 from mystic.tools import random_seed 66 66 67 67 random_seed(123) -
branches/UQ/math/examples/TEST_surrogate_diam_batchgrid.py
r471 r676 1 1 #!/usr/bin/env python 2 2 3 from numpy import asarray 4 5 def ndim_meshgrid(*arrs): 6 """n-dimensional analogue to numpy.meshgrid""" 7 arrs = tuple(reversed(arrs)) #edit 8 lens = map(len, arrs) 9 dim = len(arrs) 10 11 sz = 1 12 for s in lens: 13 sz*=s 14 15 ans = [] 16 for i, arr in enumerate(arrs): 17 slc = [1]*dim 18 slc[i] = lens[i] 19 arr2 = asarray(arr).reshape(slc) 20 for j, sz in enumerate(lens): 21 if j!=i: 22 arr2 = arr2.repeat(sz, axis=j) 23 ans.append(arr2) 24 25 return tuple(ans) 26 27 28 def gridpts(q): 29 """ 30 takes a list of lists of equal length q = [[1,2],[3,4]] 31 and produces a list of gridpoints g = [[1,3],[1,4],[2,3],[2,4]] 32 """ 33 q = list(reversed(q)) 34 w = ndim_meshgrid(*q) 35 for i in range(len(q)): 36 q[i] = list( w[i].reshape(w[i].size) ) 37 q = zip(*q) 38 return [list(i) for i in q] 3 from mystic.math.grid import gridpts 39 4 40 5 … … 99 64 from mystic.termination import NormalizedChangeOverGeneration as NCOG 100 65 from mystic.monitors import VerboseMonitor, Monitor 101 from mystic.tools import getch102 66 103 67 #stepmon = VerboseMonitor(100) -
branches/UQ/math/examples/TEST_surrogate_diam_scatter.py
r471 r676 1 1 #!/usr/bin/env python 2 2 3 def random_samples(lb,ub,npts=10000): 4 "generate npts random samples between given lb & ub" 5 from numpy.random import random 6 dim = len(lb) 7 pts = random((dim,npts)) 8 for i in range(dim): 9 pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] 10 return pts 11 12 13 def samplepts(lb,ub,npts): 14 """ 15 takes upper and lower bounds (e.g. ub = [2,4], lb = [0,3]) 16 produces a list of sample points s = [[1,3],[1,4],[2,3],[2,4]] 17 """ 18 q = random_samples(lb,ub,npts) 19 q = [list(i) for i in q] 20 q = zip(*q) 21 return [list(i) for i in q] 3 from mystic.math.grid import samplepts 22 4 23 5 … … 82 64 from mystic.termination import NormalizedChangeOverGeneration as NCOG 83 65 from mystic.monitors import VerboseMonitor, Monitor 84 from mystic.tools import getch85 66 86 67 #stepmon = VerboseMonitor(100) -
branches/UQ/math/examples/TEST_surrogate_samples.py
r471 r676 9 9 # (similar to concentration.in) 10 10 ####################################################################### 11 from TEST_surrogate_McD import volume, prob_mass12 from TEST_surrogate_McD import expectation_value, mean, mcdiarmid_bound13 11 from TEST_surrogate_diam import * # model, limit 14 15 def random_samples(lb,ub,npts=10000): 16 "generate npts random samples between given lb & ub" 17 from numpy.random import random 18 dim = len(lb) 19 pts = random((dim,npts)) 20 for i in range(dim): 21 pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] 22 return pts 23 24 25 def sampled_pof(f,pts): 26 "use sampling to calculate 'exact' PoF" 27 failure = 0 28 for i in range(len(pts[0])): 29 Fx = f([pts[0][i],pts[1][i],pts[2][i]]) 30 if not Fx: 31 failure += 1 32 pof = float(failure) / float(len(pts[0])) 33 return pof 34 35 36 def sampled_pts(pts,lb,ub): 37 from numpy import inf 38 def identity(x): 39 return x 40 f = wrap_bounds(identity,lb,ub) 41 npts = 0 42 for i in range(len(pts[0])): 43 Fx = f([pts[0][i],pts[1][i],pts[2][i]]) 44 if Fx != -inf: # outside of bounds evaluates to -inf 45 npts += 1 46 return npts 47 48 49 def sampled_prob(pts,lb,ub): 50 prob = float(sampled_pts(pts,lb,ub)) / float(len(pts[0])) 51 return prob 52 12 from mystic.math.stats import volume, prob_mass, mean, mcdiarmid_bound 13 from mystic.math.integrate import integrate as expectation_value 14 from mystic.math.samples import random_samples, sampled_pts, sampled_prob 15 from mystic.math.samples import alpha, _pof_given_samples as sampled_pof 16 from mystic.tools import wrap_bounds 53 17 54 18 def sampled_mean(pts,lb,ub): … … 65 29 return ave 66 30 67 68 def wrap_bounds(f,lb,ub):69 from numpy import asarray, any, inf70 lb = asarray(lb)71 ub = asarray(ub)72 def function_wrapper(x): #x bounded on [lb,ub)73 if any((x < lb) | (x >= ub)): #if violates bounds, evaluate as -inf74 return -inf75 return f(x)76 return function_wrapper77 78 79 31 def minF(x): 80 32 return scale * model(x) 81 33 82 83 34 def maxF(x): 84 35 return -scale * model(x) 85 86 87 from math import log88 def alpha(n,diameter,epsilon=0.01):89 #return diameter * n**(-0.5) * (-log(epsilon))**(0.5)90 return diameter * (-log(epsilon) / (2.0 * n))**(0.5)91 36 92 37 … … 100 45 from mystic.strategy import Best1Exp 101 46 from mystic.monitors import VerboseMonitor, Monitor 102 from mystic.tools import getch,random_seed47 from mystic.tools import random_seed 103 48 104 49 random_seed(123) -
branches/UQ/math/examples/TEST_surrogate_smartcut.py
r471 r676 8 8 # (similar to concentration.in) 9 9 ####################################################################### 10 from TEST_surrogate_McD import volume, prob_mass11 from TEST_surrogate_McD import expectation_value, mean, mcdiarmid_bound12 10 from TEST_surrogate_diam import * # model, limit 13 14 def random_samples(lb,ub,npts): 15 "generate npts random samples between given lb & ub" 16 from numpy.random import random 17 dim = len(lb) 18 pts = random((dim,npts)) 19 for i in range(dim): 20 pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] 21 return pts 22 23 24 def sampled_pof(f,lb,ub,npts=10000): 25 "use sampling to calculate 'exact' PoF" 26 pts = random_samples(lb,ub,npts) 27 failure = 0 28 for i in range(npts): 29 if not f([pts[0][i],pts[1][i],pts[2][i]]): 30 failure += 1 31 pof = float(failure) / float(npts) 32 return pof 33 11 from mystic.math.stats import volume, prob_mass, mean, mcdiarmid_bound 12 from mystic.math.integrate import integrate as expectation_value 13 from mystic.math.samples import sampled_pof 34 14 35 15 def minF(x): 36 16 return scale * model(x) 37 38 17 39 18 def maxF(x): … … 50 29 from mystic.strategy import Best1Exp 51 30 from mystic.monitors import VerboseMonitor, Monitor 52 from mystic.tools import getch,random_seed31 from mystic.tools import random_seed 53 32 54 33 random_seed(123)
Note: See TracChangeset
for help on using the changeset viewer.