Changeset 472
- Timestamp:
- 07/06/11 16:05:45 (5 years ago)
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UQ/math/examples2/TEST4d_OUQ_surrogate_diam.py
r471 r472 68 68 69 69 from mystic.math.measures import split_param 70 from mystic.math.dirac_measure import unflatten, flatten 71 from mystic.math.dirac_measure import compose, decompose 70 from mystic.math.dirac_measure import product_measure 72 71 from mystic.math import almostEqual 73 72 from numpy import inf … … 85 84 86 85 # generate secondary constraints function 87 def additional_constraints(c): #XXX: move within 'def constraints'?86 def more_constraints(c): #XXX: move within 'def constraints'? 88 87 ##################### begin function-specific ##################### 89 88 # E = float(c[0].var) # var(h) … … 104 103 return c 105 104 106 # wrapper to fit format for 'impose_expectation'107 def cnstr(x, w): #XXX: move within 'def constraints'?108 c = compose(x,w)109 c = additional_constraints(c)110 return decompose(c)[0]111 112 105 # generate primary constraints function 113 106 def constraints(rv): 114 c = unflatten(rv, npts) 107 c = product_measure() 108 c.load(rv, npts) 115 109 # NOTE: bounds wi in [0,1] enforced by filtering 116 # impose norm on sets117 for setin c:118 if not almostEqual(float( set.mass), 1.0, tol=atol, rel=rtol):119 set.normalize()120 # impose expectation on collection110 # impose norm on each discrete measure 111 for measure in c: 112 if not almostEqual(float(measure.mass), 1.0, tol=atol, rel=rtol): 113 measure.normalize() 114 # impose expectation on product measure 121 115 ##################### begin function-specific ##################### 122 116 E = float(c.get_expect(model)) 123 117 if not (E <= float(target[0] + error[0])) \ 124 118 or not (float(target[0] - error[0]) <= E): 125 c.set_expect((target[0],error[0]), model, (x_lb,x_ub), cnstr)126 127 # c = additional_constraints(c) #XXX: impose constraints again (necessary ?)119 c.set_expect((target[0],error[0]), model, (x_lb,x_ub), more_constraints) 120 121 # c = more_constraints(c) #XXX: impose constraints again (necessary ?) 128 122 ###################### end function-specific ###################### 129 123 # extract weights and positions 130 return flatten(c)124 return c.flatten() 131 125 132 126 # generate maximizing function 133 127 def cost(rv): 134 c = unflatten(rv, npts) 128 c = product_measure() 129 c.load(rv, npts) 135 130 #XXX: apply 'filters' to catch errors in constraints solver (necessary ?) 136 131 ##################### begin function-specific ##################### … … 242 237 243 238 from numpy import array 244 from mystic.math.dirac_measure import unflatten 245 c = unflatten(solved,npts) 239 from mystic.math.dirac_measure import product_measure 240 c = product_measure() 241 c.load(solved,npts) 246 242 print "solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords)) 247 243 print "solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords)) -
branches/UQ/math/examples2/TEST_OUQ_surrogate_diam.py
r471 r472 68 68 69 69 from mystic.math.measures import split_param 70 from mystic.math.dirac_measure import unflatten, flatten70 from mystic.math.dirac_measure import product_measure 71 71 from mystic.math import almostEqual 72 72 from numpy import inf … … 85 85 # generate primary constraints function 86 86 def constraints(rv): 87 c = unflatten(rv, npts) 87 c = product_measure() 88 c.load(rv, npts) 88 89 # NOTE: bounds wi in [0,1] enforced by filtering 89 # impose norm on sets90 for setin c:91 if not almostEqual(float( set.mass), 1.0, tol=atol, rel=rtol):92 set.normalize()93 # impose expectation on collection90 # impose norm on each discrete measure 91 for measure in c: 92 if not almostEqual(float(measure.mass), 1.0, tol=atol, rel=rtol): 93 measure.normalize() 94 # impose expectation on product measure 94 95 ##################### begin function-specific ##################### 95 96 E = float(c.get_expect(model)) … … 99 100 ###################### end function-specific ###################### 100 101 # extract weights and positions 101 return flatten(c)102 return c.flatten() 102 103 103 104 # generate maximizing function 104 105 def cost(rv): 105 c = unflatten(rv, npts) 106 c = product_measure() 107 c.load(rv, npts) 106 108 E = float(c.get_expect(model)) 107 109 if E > (target[0] + error[0]) or E < (target[0] - error[0]): … … 187 189 188 190 from numpy import array 189 from mystic.math.dirac_measure import unflatten 190 c = unflatten(solved,npts) 191 from mystic.math.dirac_measure import product_measure 192 c = product_measure() 193 c.load(solved,npts) 191 194 print "solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords)) 192 195 print "solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords)) -
mystic/_math/dirac_measure.py
r457 r472 40 40 41 41 settings: 42 s.weights = [w1, w2, ..., wn] -- set weights43 s.coords = [x1, x2, ..., xn] -- set positions44 s.normalize() -- normalize sthe weights to 1.042 s.weights = [w1, w2, ..., wn] -- set the weights 43 s.coords = [x1, x2, ..., xn] -- set the positions 44 s.normalize() -- normalize the weights to 1.0 45 45 s.mean(R) -- set the mean 46 46 s.range(R) -- set the range … … 125 125 c.coords -- returns list of position tuples 126 126 c.mass -- returns list of weight norms 127 c.pts -- returns number of points for each discrete measure 128 c.wts -- returns list of weights for each discrete measure 129 c.pos -- returns list of positions for each discrete measure 127 130 128 131 settings: 129 c.coords = [(x1,y1,z1),...] -- set positions (propagates to each set member)132 c.coords = [(x1,y1,z1),...] -- set the positions (tuples in product measure) 130 133 131 134 methods: … … 133 136 c.get_expect(f) -- calculate the expectation 134 137 c.set_expect((center,delta), f) -- impose expectation by adjusting positions 138 c.flatten() -- convert measure to a flat list of parameters 139 c.load(params, pts) -- 'fill' the measure from a flat list of parameters 135 140 136 141 notes: … … 141 146 """ 142 147 148 def __pts(self): 149 return [i.npts for i in self] 150 151 def __wts(self): 152 return [i.weights for i in self] 153 154 def __pos(self): 155 return [i.coords for i in self] 156 143 157 def __n(self): 144 npts = 1 145 for i in self: 146 npts *= i.npts 147 return npts 158 from numpy import product 159 return product(self.pts) 148 160 149 161 def __weights(self): 150 162 from mystic.math.measures import _pack 151 weights = [i.weights for i in self]152 weights = _pack( weights)163 from numpy import product 164 weights = _pack(self.wts) 153 165 _weights = [] 154 166 for wts in weights: 155 weight = 1.0 156 for w in wts: 157 weight *= w 158 _weights.append(weight) 167 _weights.append(product(wts)) 159 168 return _weights 160 169 161 170 def __positions(self): 162 171 from mystic.math.measures import _pack 163 coords = [i.coords for i in self] 164 coords = _pack(coords) 165 return coords 172 return _pack(self.pos) 166 173 167 174 def __set_positions(self, coords): 168 175 from mystic.math.measures import _unpack 169 npts = [i.npts for i in self] 170 coords = _unpack(coords,npts) 176 coords = _unpack(coords, self.pts) 171 177 for i in range(len(coords)): 172 178 self[i].coords = coords[i] … … 198 204 f -- a function that takes a list and returns a number 199 205 bounds -- tuple of lists of bounds (lower_bounds, upper_bounds) 200 constraints -- a function that takes a nested list of N x 1D discrete 201 measure positions and weights x' = constraints(x, w) 206 constraints -- a function that takes a product_measure c' = constraints(c) 202 207 """ 203 208 #self.__center = m 204 209 #self.__delta = D 205 npts = [i.npts for i in self] 206 self.coords = impose_expectation((m,D), f, npts, bounds, self.weights, \ 207 constraints=constraints) 210 if constraints: # then need to adjust interface for 'impose_expectation' 211 def cnstr(x, w): 212 c = compose(x,w) 213 c = constraints(c) 214 return decompose(c)[0] 215 else: cnstr = constraints # 'should' be None 216 self.coords = impose_expectation((m,D), f, self.pts, bounds, self.weights, \ 217 constraints=cnstr) 208 218 return 209 219 … … 258 268 return transpose(pts) #XXX: assumes 'coords' is a list of floats 259 269 270 def load(self, params, pts): 271 """load a list of parameters corresponding to N x 1D discrete measures 272 273 Inputs: 274 params -- a list of parameters (see 'notes') 275 pts -- number of points in each of the underlying discrete measures 276 277 Notes: 278 To append len(pts) new discrete measures to product measure c, where 279 pts = (M, N, ...) 280 params = [wt_x1, ..., wt_xM, \ 281 x1, ..., xM, \ 282 wt_y1, ..., wt_yN, \ 283 y1, ..., yN, \ 284 ...] 285 Thus, the provided list is M weights and the corresponding M positions, 286 followed by N weights and the corresponding N positions, with this 287 pattern followed for each new dimension desired for the product measure. 288 """ 289 c = unflatten(params, pts) 290 for i in range(len(c)): 291 self.append(c[i]) 292 return 293 294 def flatten(self): 295 """flatten the product_measure into a list of parameters 296 297 Returns: 298 params -- a list of parameters (see 'notes') 299 300 Notes: 301 For a product measure c where c.pts = (M, N, ...), then 302 params = [wt_x1, ..., wt_xM, \ 303 x1, ..., xM, \ 304 wt_y1, ..., wt_yN, \ 305 y1, ..., yN, \ 306 ...] 307 Thus, the returned list is M weights and the corresponding M positions, 308 followed by N weights and the corresponding N positions, with this 309 pattern followed for each dimension of the product measure. 310 """ 311 return flatten(self) 312 260 313 #__center = None 261 314 #__delta = None … … 269 322 #expect = property(__expect, __set_expect ) 270 323 mass = property(__mass ) 324 pts = property(__pts ) 325 wts = property(__wts ) 326 pos = property(__pos ) 271 327 pass 272 328 … … 299 355 300 356 301 def compose(samples, weights):302 """ Generate a product_measure objectfrom a nested list of N x 1D357 def _list_of_measures(samples, weights): 358 """generate a list of N x 1D discrete measures from a nested list of N x 1D 303 359 discrete measure positions and a nested list of N x 1D weights.""" 304 360 total = [] … … 308 364 next.append(point( samples[i][j], weights[i][j] )) 309 365 total.append(next) 366 return total 367 368 369 def compose(samples, weights): 370 """Generate a product_measure object from a nested list of N x 1D 371 discrete measure positions and a nested list of N x 1D weights.""" 372 total = _list_of_measures(samples, weights) 310 373 c = product_measure(total) 311 374 return c … … 316 379 N x 1D discrete measure positions and a nested list of N x 1D weights.""" 317 380 from mystic.math.measures import _nested_split 318 npts = [set.npts for set in c] 319 w, x = _nested_split(flatten(c), npts) 381 w, x = _nested_split(flatten(c), c.pts) 320 382 return x, w 321 383 -
mystic/tests/test_dirac_measure.py
r468 r472 196 196 print "randomly selected support:\n %s" % c.support(10) 197 197 198 print "npts: %s " % c.npts198 print "npts: %s (i.e. %s)" % (c.npts, c.pts) 199 199 print "weights: %s" % c.weights 200 200 coords = c.coords … … 247 247 # flatten and unflatten 248 248 from mystic.math.dirac_measure import flatten, unflatten 249 npts = [s.npts for s in c] 250 d = unflatten(flatten(c), npts) 249 d = unflatten(flatten(c), c.pts) 251 250 252 251 # check if the same … … 254 253 assert c.weights == d.weights 255 254 assert c.coords == d.coords 255 256 # flatten() and load(...) 257 e = collection() 258 e.load(c.flatten(), c.pts) 259 260 # check if the same 261 assert c.npts == e.npts 262 assert c.weights == e.weights 263 assert c.coords == e.coords 256 264 257 265 # decompose and compose
Note: See TracChangeset
for help on using the changeset viewer.