Changeset 472


Ignore:
Timestamp:
07/06/11 16:05:45 (5 years ago)
Author:
mmckerns
Message:

new methods for product measure: load, flatten, pts, wts, pos;
more natural interface for constraints in "set_expect"

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/UQ/math/examples2/TEST4d_OUQ_surrogate_diam.py

    r471 r472  
    6868 
    6969  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 
    7271  from mystic.math import almostEqual 
    7372  from numpy import inf 
     
    8584 
    8685  # generate secondary constraints function 
    87   def additional_constraints(c): #XXX: move within 'def constraints'? 
     86  def more_constraints(c): #XXX: move within 'def constraints'? 
    8887    ##################### begin function-specific ##################### 
    8988#   E = float(c[0].var)  # var(h) 
     
    104103    return c 
    105104 
    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  
    112105  # generate primary constraints function 
    113106  def constraints(rv): 
    114     c = unflatten(rv, npts) 
     107    c = product_measure() 
     108    c.load(rv, npts) 
    115109    # NOTE: bounds wi in [0,1] enforced by filtering 
    116     # impose norm on sets 
    117     for set in c: 
    118       if not almostEqual(float(set.mass), 1.0, tol=atol, rel=rtol): 
    119         set.normalize() 
    120     # impose expectation on collection 
     110    # 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 
    121115    ##################### begin function-specific ##################### 
    122116    E = float(c.get_expect(model)) 
    123117    if not (E <= float(target[0] + error[0])) \ 
    124118    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 ?) 
    128122    ###################### end function-specific ###################### 
    129123    # extract weights and positions 
    130     return flatten(c) 
     124    return c.flatten() 
    131125 
    132126  # generate maximizing function 
    133127  def cost(rv): 
    134     c = unflatten(rv, npts) 
     128    c = product_measure() 
     129    c.load(rv, npts) 
    135130    #XXX: apply 'filters' to catch errors in constraints solver (necessary ?) 
    136131    ##################### begin function-specific ##################### 
     
    242237 
    243238  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) 
    246242  print "solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords)) 
    247243  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  
    6868 
    6969  from mystic.math.measures import split_param 
    70   from mystic.math.dirac_measure import unflatten, flatten 
     70  from mystic.math.dirac_measure import product_measure 
    7171  from mystic.math import almostEqual 
    7272  from numpy import inf 
     
    8585  # generate primary constraints function 
    8686  def constraints(rv): 
    87     c = unflatten(rv, npts) 
     87    c = product_measure() 
     88    c.load(rv, npts) 
    8889    # NOTE: bounds wi in [0,1] enforced by filtering 
    89     # impose norm on sets 
    90     for set in c: 
    91       if not almostEqual(float(set.mass), 1.0, tol=atol, rel=rtol): 
    92         set.normalize() 
    93     # impose expectation on collection 
     90    # 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 
    9495    ##################### begin function-specific ##################### 
    9596    E = float(c.get_expect(model)) 
     
    99100    ###################### end function-specific ###################### 
    100101    # extract weights and positions 
    101     return flatten(c) 
     102    return c.flatten() 
    102103 
    103104  # generate maximizing function 
    104105  def cost(rv): 
    105     c = unflatten(rv, npts) 
     106    c = product_measure() 
     107    c.load(rv, npts) 
    106108    E = float(c.get_expect(model)) 
    107109    if E > (target[0] + error[0]) or E < (target[0] - error[0]): 
     
    187189 
    188190  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) 
    191194  print "solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords)) 
    192195  print "solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords)) 
  • mystic/_math/dirac_measure.py

    r457 r472  
    4040 
    4141 settings: 
    42   s.weights = [w1, w2, ..., wn]  --  set weights 
    43   s.coords = [x1, x2, ..., xn]  --  set positions 
    44   s.normalize()  --  normalizes the weights to 1.0 
     42  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 
    4545  s.mean(R)  --  set the mean 
    4646  s.range(R)  --  set the range 
     
    125125  c.coords  --  returns list of position tuples 
    126126  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 
    127130 
    128131 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) 
    130133 
    131134 methods: 
     
    133136  c.get_expect(f)  --  calculate the expectation 
    134137  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 
    135140 
    136141 notes: 
     
    141146""" 
    142147 
     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 
    143157  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) 
    148160 
    149161  def __weights(self): 
    150162    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) 
    153165    _weights = [] 
    154166    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)) 
    159168    return _weights 
    160169 
    161170  def __positions(self): 
    162171    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) 
    166173 
    167174  def __set_positions(self, coords): 
    168175    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) 
    171177    for i in range(len(coords)): 
    172178      self[i].coords = coords[i] 
     
    198204    f -- a function that takes a list and returns a number 
    199205    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) 
    202207""" 
    203208   #self.__center = m 
    204209   #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)  
    208218    return 
    209219 
     
    258268    return transpose(pts)  #XXX: assumes 'coords' is a list of floats 
    259269 
     270  def load(self, params, pts): 
     271    """load a list of parameters corresponding to N x 1D discrete measures 
     272 
     273Inputs: 
     274    params -- a list of parameters (see 'notes') 
     275    pts -- number of points in each of the underlying discrete measures 
     276 
     277Notes: 
     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 
     297Returns: 
     298    params -- a list of parameters (see 'notes') 
     299 
     300Notes: 
     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 
    260313 #__center = None 
    261314 #__delta = None 
     
    269322 #expect = property(__expect, __set_expect ) 
    270323  mass = property(__mass ) 
     324  pts = property(__pts ) 
     325  wts = property(__wts ) 
     326  pos = property(__pos ) 
    271327  pass 
    272328 
     
    299355 
    300356 
    301 def compose(samples, weights): 
    302   """Generate a product_measure object from a nested list of N x 1D 
     357def _list_of_measures(samples, weights): 
     358  """generate a list of N x 1D discrete measures from a nested list of N x 1D 
    303359discrete measure positions and a nested list of N x 1D weights.""" 
    304360  total = [] 
     
    308364      next.append(point( samples[i][j], weights[i][j] )) 
    309365    total.append(next) 
     366  return total 
     367 
     368 
     369def compose(samples, weights): 
     370  """Generate a product_measure object from a nested list of N x 1D 
     371discrete measure positions and a nested list of N x 1D weights.""" 
     372  total = _list_of_measures(samples, weights) 
    310373  c = product_measure(total) 
    311374  return c 
     
    316379N x 1D discrete measure positions and a nested list of N x 1D weights.""" 
    317380  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) 
    320382  return x, w 
    321383 
  • mystic/tests/test_dirac_measure.py

    r468 r472  
    196196  print "randomly selected support:\n %s" % c.support(10) 
    197197 
    198   print "npts: %s" % c.npts 
     198  print "npts: %s (i.e. %s)" % (c.npts, c.pts) 
    199199  print "weights: %s" % c.weights 
    200200  coords = c.coords 
     
    247247  # flatten and unflatten 
    248248  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) 
    251250 
    252251  # check if the same 
     
    254253  assert c.weights == d.weights 
    255254  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 
    256264 
    257265  # decompose and compose 
Note: See TracChangeset for help on using the changeset viewer.