Changeset 777


Ignore:
Timestamp:
01/03/15 09:21:20 (17 months ago)
Author:
mmckerns
Message:

add std, impose_std, impose_reweighted_std to measures; with_std to constraints

Location:
mystic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mystic/_math/measures.py

    r776 r777  
    174174  return mean(svar, weights) 
    175175 
     176def std(samples, weights=None): #, _mean=None): 
     177  """calculate the (weighted) standard deviation for a list of points 
     178 
     179Inputs: 
     180    samples -- a list of sample points 
     181    weights -- a list of sample weights 
     182""" 
     183  from numpy import sqrt 
     184  return sqrt(variance(samples, weights)) # _mean) 
     185 
    176186 
    177187##### coordinate shift methods ##### 
     
    218228#       ...so likely, must scale the weights... or scale each point differently 
    219229 
     230def impose_std(s, samples, weights=None): 
     231  """impose a standard deviation on a list of (weighted) points 
     232  (this function is 'mean-preserving') 
     233 
     234Inputs: 
     235    s -- the target standard deviation 
     236    samples -- a list of sample points 
     237    weights -- a list of sample weights 
     238""" 
     239  return impose_variance(s**2, samples, weights) 
    220240 
    221241def impose_spread(r, samples, weights=None): #FIXME: fails if len(samples) = 1 
     
    482502    return samples, wts  # "mean-preserving" 
    483503 
     504def impose_reweighted_std(s, samples, weights=None, solver=None): 
     505    """impose a standard deviation on a list of points, using reweighting""" 
     506    return impose_reweighted_variance(s**2, samples, weights, solver) 
    484507 
    485508##### misc methods ##### 
  • mystic/mystic/constraints.py

    r776 r777  
    146146        return factory 
    147147    return decorate 
     148 
     149 
     150def with_std(target): 
     151    """bind a standard deviation constraint to a given constraints function. 
     152 
     153Inputs: 
     154    target -- the target standard deviation 
     155 
     156A constraints function takes an iterable x as input, returning a modified x. 
     157This function is an "outer" coupling of "impose_std" onto another 
     158constraints function c(x), such that:  x' = impose_std(target, c(x)). 
     159 
     160    For example: 
     161    >>> @with_std(1.0) 
     162    ... def constraint(x): 
     163    ...   x[-1] = x[0] 
     164    ...   return x 
     165    ...  
     166    >>> x = constraint([1,2,3]) 
     167    >>> print x 
     168    [0.6262265521467858, 2.747546895706428, 0.6262265521467858] 
     169    >>> std(x) 
     170    0.99999999999999956 
     171    """ 
     172    return with_variance(target**2) 
    148173 
    149174 
Note: See TracChangeset for help on using the changeset viewer.