Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
 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
Esempio n. 4
0
 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
Esempio n. 5
0
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)]
Esempio n. 7
0
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
Esempio n. 8
0
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
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
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)]
Esempio n. 12
0
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)]
Esempio n. 13
0
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
Esempio n. 14
0
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
Esempio n. 15
0
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
Esempio n. 16
0
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
Esempio n. 17
0
 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
Esempio n. 18
0
 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
Esempio n. 19
0
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]
Esempio n. 20
0
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]
Esempio n. 21
0
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]
Esempio n. 22
0
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]
Esempio n. 23
0
 def mean_constraint(x, mean=0.0):
     return impose_mean(mean, x)
Esempio n. 24
0
 def __set_mean(self, m):
   self.positions = impose_mean(m, self.positions, self.weights)
   return
Esempio n. 25
0
 def mean_of_squared(x, target):
     return impose_mean(target, [i**2 for i in x])
Esempio n. 26
0
 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
Esempio n. 27
0
 def mean_constraint(x, mean=0.0):
   return impose_mean(mean, x)
Esempio n. 28
0
 def impose_constraints(x, mean, weights=None):
   return impose_mean(mean, x, weights)
Esempio n. 29
0
 def mean_of_squared(x, target):
   return impose_mean(target, [i**2 for i in x])
Esempio n. 30
0
 def impose_constraints(x, mean=0.0, spread=1.0):
   x = impose_mean(mean, x)
   x = impose_spread(spread, x)
   return x
Esempio n. 31
0
 def __set_mean(self, m):
   self.positions = impose_mean(m, self.positions, self.weights)
   return
Esempio n. 32
0
 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
Esempio n. 33
0
 def impose_constraints(x, mean=0.0, spread=1.0):
     x = impose_mean(mean, x)
     x = impose_spread(spread, x)
     return x
Esempio n. 34
0
 def impose_constraints(x, mean, weights=None):
     return impose_mean(mean, x, weights)