Changeset 820
- Timestamp:
- 08/12/15 22:13:07 (9 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/_math/measures.py
r803 r820 374 374 375 375 ##### weight shift methods ##### 376 def impose_weight_norm(samples, weights, mass= 1.0):376 def impose_weight_norm(samples, weights, mass=None): 377 377 """normalize the weights for a list of (weighted) points 378 378 (this function is 'mean-preserving') … … 381 381 samples -- a list of sample points 382 382 weights -- a list of sample weights 383 mass -- target sumof normalized weights383 mass -- target of normalized weights 384 384 """ 385 385 m = mean(samples, weights) … … 388 388 389 389 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 sumof normalized weights390 def normalize(weights, mass=None, zsum=False, zmass=1.0, l=1): 391 """normalize a list of points (e.g. normalize to 1.0) 392 393 Inputs: 394 weights -- a list of sample weights 395 mass -- target of normalized weights 396 396 zsum -- use counterbalance when mass = 0.0 397 397 zmass -- member scaling when mass = 0.0 398 """ 398 l -- integer power for the norm (i.e. l=1 is the L1 norm) 399 400 Note: if mass is None, use mass = sum(weights)/sum(abs(weights)) 401 """ 402 l = int(l) 399 403 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) 401 411 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 405 419 if float(mass) or not zsum: 406 420 return list(mass * weights / w) #FIXME: not "mean-preserving" 407 421 # force selected member to satisfy sum = 0.0 408 422 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) 410 427 mass = zmass 411 428 return list(mass * weights / w) #FIXME: not "mean-preserving"
Note: See TracChangeset
for help on using the changeset viewer.