Changeset 820


Ignore:
Timestamp:
08/12/15 22:13:07 (9 months ago)
Author:
mmckerns
Message:

extend mystic.math.measures from L1 norm to Ln norm; set defailt mass=None

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mystic/_math/measures.py

    r803 r820  
    374374 
    375375##### weight shift methods ##### 
    376 def impose_weight_norm(samples, weights, mass=1.0): 
     376def impose_weight_norm(samples, weights, mass=None): 
    377377  """normalize the weights for a list of (weighted) points 
    378378  (this function is 'mean-preserving') 
     
    381381    samples -- a list of sample points 
    382382    weights -- a list of sample weights 
    383     mass -- target sum of normalized weights 
     383    mass -- target of normalized weights 
    384384""" 
    385385  m = mean(samples, weights) 
     
    388388 
    389389 
    390 def normalize(weights, mass=1.0, zsum=False, zmass=1.0): 
    391   """normalize a list of points to unity (i.e. normalize to 1.0) 
    392  
    393 Inputs: 
    394     weights -- a list of sample weights 
    395     mass -- target sum of normalized weights 
     390def normalize(weights, mass=None, zsum=False, zmass=1.0, l=1): 
     391  """normalize a list of points (e.g. normalize to 1.0) 
     392 
     393Inputs: 
     394    weights -- a list of sample weights 
     395    mass -- target of normalized weights 
    396396    zsum -- use counterbalance when mass = 0.0 
    397397    zmass -- member scaling when mass = 0.0 
    398 """ 
     398    l -- integer power for the norm (i.e. l=1 is the L1 norm) 
     399 
     400Note: if mass is None, use mass = sum(weights)/sum(abs(weights)) 
     401""" 
     402  l = int(l) 
    399403  weights = asarray(list(weights)) #XXX: faster to use x = array(x, copy=True) ? 
    400   w = float(sum(weights)) 
     404  if mass is None: 
     405    mass = sum(weights)/sum(abs(weights)) #XXX: correct? 
     406    if not mass: mass = None 
     407  if not l: 
     408    w = float(len(weights[weights != 0.0])) # total number of nonzero elements 
     409  else: 
     410    w = float(sum(weights**l))**(1./l) 
    401411  if not w:  #XXX: is this the best behavior? 
    402     from numpy import inf, nan 
    403     weights[weights == 0.0] = nan 
    404     return list(weights * inf)  # protect against ZeroDivision 
     412    if mass is None: 
     413      w = sum(abs(weights)); mass = 1.0 # XXX: correct? 
     414    else: 
     415      from numpy import inf, nan 
     416      weights[weights == 0.0] = nan 
     417      return list(weights * inf)  # protect against ZeroDivision 
     418  if mass is None: mass = 1.0 
    405419  if float(mass) or not zsum: 
    406420    return list(mass * weights / w)  #FIXME: not "mean-preserving" 
    407421  # force selected member to satisfy sum = 0.0 
    408422  zsum = -1 
    409   weights[zsum] = -(w - weights[zsum]) 
     423  if not l: 
     424    weights[:] = 0.0 #XXX: correct? 
     425  else: 
     426    weights[zsum] = (-(w**l - weights[zsum]**l))**(1./l) 
    410427  mass = zmass 
    411428  return list(mass * weights / w)  #FIXME: not "mean-preserving" 
Note: See TracChangeset for help on using the changeset viewer.