def bounded_mean(mean_x, samples, xmin, xmax, wts=None): from mystic.math.measures import impose_mean, impose_spread from mystic.math.measures import spread, mean from numpy import asarray a = impose_mean(mean_x, samples, wts) if min(a) < xmin: # maintain the bound #print "violate lo(a)" s = spread(a) - 2*(xmin - min(a)) #XXX: needs compensation (as below) ? a = impose_mean(mean_x, impose_spread(s, samples, wts), wts) if max(a) > xmax: # maintain the bound #print "violate hi(a)" s = spread(a) + 2*(xmax - max(a)) #XXX: needs compensation (as below) ? a = impose_mean(mean_x, impose_spread(s, samples, wts), wts) return asarray(a)
def bounded_mean(mean_x, samples, xmin, xmax, wts=None): from mystic.math.measures import impose_mean, impose_spread from mystic.math.measures import spread, mean from numpy import asarray a = impose_mean(mean_x, samples, wts) if min(a) < xmin: # maintain the bound #print "violate lo(a)" s = spread(a) - 2*(xmin - min(a)) #XXX: needs compensation (as below) ? a = impose_mean(mean_x, impose_spread(s, samples, wts), wts) if max(a) > xmax: # maintain the bound #print "violate hi(a)" s = spread(a) + 2*(xmax - max(a)) #XXX: needs compensation (as below) ? a = impose_mean(mean_x, impose_spread(s, samples, wts), wts) return asarray(a)
def constraints(x): # constrain the last x_i to be the same value as the first x_i x[-1] = x[0] # constrain x such that mean(x) == target if not almostEqual(mean(x), target): x = impose_mean(target, x) return x
def constraints(x): # constrain the last x_i to be the same value as the first x_i x[-1] = x[0] # constrain x such that mean(x) == target if not almostEqual(mean(x), target): x = impose_mean(target, x) return x
def test_proxified_constraint(): from mystic.math.measures import impose_mean @inner_proxy(inner=impose_mean) def mean_then_squared(x): #XXX: proxy doesn't preserve function signature return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) assert mean_then_squared(5,x) == [i**2 for i in impose_mean(5,x)]
def test_proxified_constraint(): from mystic.math.measures import impose_mean @inner_proxy(inner=impose_mean) def mean_then_squared(x): #XXX: proxy doesn't preserve function signature return [i**2 for i in x] from numpy import array x = array([1, 2, 3, 4, 5]) assert mean_then_squared(5, x) == [i**2 for i in impose_mean(5, x)]
def test_with_constraint(): from mystic.math.measures import mean, impose_mean @with_constraint(inner, kwds={'target': 5.0}) def mean_of_squared(x, target): return impose_mean(target, [i**2 for i in x]) from numpy import array x = array([1, 2, 3, 4, 5]) y = impose_mean(5, [i**2 for i in x]) assert mean(y) == 5.0 assert mean_of_squared(x) == y
def test_with_mean(): from mystic.math.measures import mean, impose_mean @with_mean(5.0) def mean_of_squared(x): return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) y = impose_mean(5, [i**2 for i in x]) assert mean(y) == 5.0 assert mean_of_squared(x) == y
def test_with_constraint(): from mystic.math.measures import mean, impose_mean @with_constraint(inner, kwds={'target':5.0}) def mean_of_squared(x, target): return impose_mean(target, [i**2 for i in x]) from numpy import array x = array([1,2,3,4,5]) y = impose_mean(5, [i**2 for i in x]) assert mean(y) == 5.0 assert mean_of_squared(x) == y
def test_with_mean(): from mystic.math.measures import mean, impose_mean @with_mean(5.0) def mean_of_squared(x): return [i**2 for i in x] from numpy import array x = array([1, 2, 3, 4, 5]) y = impose_mean(5, [i**2 for i in x]) assert mean(y) == 5.0 assert mean_of_squared(x) == y
def test_inner_constraint(): from mystic.math.measures import impose_mean def impose_constraints(x, mean, weights=None): return impose_mean(mean, x, weights) @inner(inner=impose_constraints, kwds={'mean': 5.0}) def mean_then_squared(x): return [i**2 for i in x] from numpy import array x = array([1, 2, 3, 4, 5]) assert mean_then_squared(x) == [i**2 for i in impose_mean(5, x)]
def test_inner_constraint(): from mystic.math.measures import impose_mean def impose_constraints(x, mean, weights=None): return impose_mean(mean, x, weights) @inner(inner=impose_constraints, kwds={'mean':5.0}) def mean_then_squared(x): return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) assert mean_then_squared(x) == [i**2 for i in impose_mean(5,x)]
def test_with_mean_spread(): from mystic.math.measures import mean, spread, impose_mean, impose_spread @with_spread(50.0) @with_mean(5.0) def constrained_squared(x): return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) y = impose_spread(50.0, impose_mean(5.0,[i**2 for i in x])) assert almostEqual(mean(y), 5.0, tol=1e-15) assert almostEqual(spread(y), 50.0, tol=1e-15) assert constrained_squared(x) == y
def test_with_mean_spread(): from mystic.math.measures import mean, spread, impose_mean, impose_spread @with_spread(50.0) @with_mean(5.0) def constrained_squared(x): return [i**2 for i in x] from numpy import array x = array([1, 2, 3, 4, 5]) y = impose_spread(50.0, impose_mean(5.0, [i**2 for i in x])) assert almostEqual(mean(y), 5.0, tol=1e-15) assert almostEqual(spread(y), 50.0, tol=1e-15) assert constrained_squared(x) == y
def test_outer_constraint(): from mystic.math.measures import impose_mean, mean def impose_constraints(x, mean, weights=None): return impose_mean(mean, x, weights) @outer(outer=impose_constraints, kwds={'mean': 5.0}) def mean_of_squared(x): return [i**2 for i in x] from numpy import array x = array([1, 2, 3, 4, 5]) y = impose_mean(5, [i**2 for i in x]) assert mean(y) == 5.0 assert mean_of_squared(x) == y
def test_outer_constraint(): from mystic.math.measures import impose_mean, mean def impose_constraints(x, mean, weights=None): return impose_mean(mean, x, weights) @outer(outer=impose_constraints, kwds={'mean':5.0}) def mean_of_squared(x): return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) y = impose_mean(5, [i**2 for i in x]) assert mean(y) == 5.0 assert mean_of_squared(x) == y
def constrain(rv): "constrain: y >= m and sum(wi)_{k} = 1 for each k in K" pm = scenario() pm.load(rv, pts) # here rv is param: w,x,y #impose: sum(wi)_{k} = 1 for each k in K norm = 1.0 for i in range(len(pm)): w = pm[i].weights w[-1] = norm - sum(w[:-1]) pm[i].weights = w #impose: y >= m values, weights = pm.values, pm.weights y = float(mean(values, weights)) if not (y >= float(target[0])): pm.values = impose_mean(target[0]+target[1], values, weights) rv = pm.flatten(all=True) return rv
def constrain(rv): "constrain: y >= m and sum(wi)_{k} = 1 for each k in K" pm = scenario() pm.load(rv, pts) # here rv is param: w,x,y #impose: sum(wi)_{k} = 1 for each k in K norm = 1.0 for i in range(len(pm)): w = pm[i].weights w[-1] = norm - sum(w[:-1]) pm[i].weights = w #impose: y >= m values, weights = pm.values, pm.weights y = float(mean(values, weights)) if not (y >= float(target[0])): pm.values = impose_mean(target[0]+target[1], values, weights) rv = pm.flatten(all=True) return rv
def test_inner_constraints(): from mystic.math.measures import impose_mean, impose_spread def impose_constraints(x, mean=0.0, spread=1.0): x = impose_mean(mean, x) x = impose_spread(spread, x) return x @inner(inner=impose_constraints, kwds={'mean':5.0, 'spread':50.0}) def constrained_squared(x): return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) y = impose_spread(50.0, impose_mean(5.0,x)) assert constrained_squared(x) == [i**2 for i in y]
def test_proxified_constraints(): from mystic.math.measures import impose_mean, impose_spread def impose_constraints(x, mean=0.0, spread=1.0): x = impose_mean(mean, x) x = impose_spread(spread, x) return x @inner_proxy(inner=impose_constraints) def constrained_squared(x): #XXX: proxy doesn't preserve function signature return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) y = impose_spread(50.0, impose_mean(5.0,x)) assert constrained_squared(x, 5.0, 50.0) == [i**2 for i in y]
def test_proxified_constraints(): from mystic.math.measures import impose_mean, impose_spread def impose_constraints(x, mean=0.0, spread=1.0): x = impose_mean(mean, x) x = impose_spread(spread, x) return x @inner_proxy(inner=impose_constraints) def constrained_squared(x): #XXX: proxy doesn't preserve function signature return [i**2 for i in x] from numpy import array x = array([1,2,3,4,5]) y = impose_spread(50.0, impose_mean(5.0,x)) assert constrained_squared(x, 5.0, 50.0) == [i**2 for i in y]
def test_inner_constraints(): from mystic.math.measures import impose_mean, impose_spread def impose_constraints(x, mean=0.0, spread=1.0): x = impose_mean(mean, x) x = impose_spread(spread, x) return x @inner(inner=impose_constraints, kwds={'mean': 5.0, 'spread': 50.0}) def constrained_squared(x): return [i**2 for i in x] from numpy import array x = array([1, 2, 3, 4, 5]) y = impose_spread(50.0, impose_mean(5.0, x)) assert constrained_squared(x) == [i**2 for i in y]
def mean_constraint(x, mean=0.0): return impose_mean(mean, x)
def __set_mean(self, m): self.positions = impose_mean(m, self.positions, self.weights) return
def mean_of_squared(x, target): return impose_mean(target, [i**2 for i in x])
def set_mean_value(self, m): # set mean of y's """set the mean for the associated values of a scenario""" from mystic.math.measures import impose_mean self.values = impose_mean(m, self.values, self.weights) return
def mean_constraint(x, mean=0.0): return impose_mean(mean, x)
def impose_constraints(x, mean, weights=None): return impose_mean(mean, x, weights)
def mean_of_squared(x, target): return impose_mean(target, [i**2 for i in x])
def impose_constraints(x, mean=0.0, spread=1.0): x = impose_mean(mean, x) x = impose_spread(spread, x) return x
def __set_mean(self, m): self.positions = impose_mean(m, self.positions, self.weights) return
def set_mean_value(self, m): # set mean of y's """set the mean for the associated values of a scenario""" from mystic.math.measures import impose_mean self.values = impose_mean(m, self.values, self.weights) return
def impose_constraints(x, mean=0.0, spread=1.0): x = impose_mean(mean, x) x = impose_spread(spread, x) return x
def impose_constraints(x, mean, weights=None): return impose_mean(mean, x, weights)