예제 #1
0
def test_flatten_unflatten():
    # build three sets (x,y,z)
    sx = set([point(1.0, 1.0), point(2.0, 1.0), point(3.0, 2.0)])
    sy = set([point(0.0, 1.0), point(3.0, 2.0)])
    sz = set([point(1.0, 1.0), point(2.0, 3.0)])

    # build a collection
    c = collection([sx, sy, sz])

    # flatten and unflatten
    from mystic.math.discrete import flatten, unflatten
    d = unflatten(flatten(c), c.pts)

    # check if the same
    assert c.npts == d.npts
    assert c.weights == d.weights
    assert c.positions == d.positions

    # flatten() and load(...)
    e = collection().load(c.flatten(), c.pts)

    # check if the same
    assert c.npts == e.npts
    assert c.weights == e.weights
    assert c.positions == e.positions

    # decompose and compose
    from mystic.math.discrete import decompose, compose
    b = compose(*decompose(c))

    # check if the same
    assert c.npts == b.npts
    assert c.weights == b.weights
    assert c.positions == b.positions
    return
예제 #2
0
def test_flatten_unflatten():
  # build three sets (x,y,z)
  sx = set([point(1.0,1.0), point(2.0,1.0), point(3.0,2.0)])
  sy = set([point(0.0,1.0), point(3.0,2.0)])
  sz = set([point(1.0,1.0), point(2.0,3.0)])

  # build a collection
  c = collection([sx,sy,sz])

  # flatten and unflatten
  from mystic.math.discrete import flatten, unflatten
  d = unflatten(flatten(c), c.pts)

  # check if the same
  assert c.npts == d.npts
  assert c.weights == d.weights
  assert c.positions == d.positions

  # flatten() and load(...)
  e = collection().load(c.flatten(), c.pts)

  # check if the same
  assert c.npts == e.npts
  assert c.weights == e.weights
  assert c.positions == e.positions

  # decompose and compose
  from mystic.math.discrete import decompose, compose
  b = compose(*decompose(c))

  # check if the same
  assert c.npts == b.npts
  assert c.weights == b.weights
  assert c.positions == b.positions
  return
예제 #3
0
def test_set_behavior():
  from mystic.math import almostEqual
  from numpy import inf

  # check basic behavior for set of two points
  s = set([point(1.0,1.0), point(3.0,2.0)]) #XXX: s + [pt, pt] => broken
  assert almostEqual(s.mean, 2.33333333)
  assert almostEqual(s.range, 2.0)
  assert almostEqual(s.mass, 3.0)

  # basic behavior for an admissable set
  s.normalize()
  s.mean = 1.0
  s.range = 1.0
  assert almostEqual(s.mean, 1.0)
  assert almostEqual(s.range, 1.0)
  assert almostEqual(s.mass, 1.0)

  # add and remove points
  # test special cases: SUM(weights)=0, RANGE(samples)=0, SUM(samples)=0
  s.append(point(1.0,-1.0))
  assert s.mean == -inf
  assert almostEqual(s.range, 1.0)
  assert almostEqual(s.mass, 0.0)

  '''
  _ave = s.mean
  s.mean = 1.0
  assert str(s.mean) == 'nan'
  assert str(s.range) == 'nan'
  assert almostEqual(s.mass, 0.0)
  s.normalize()
  assert str(s.mass) == 'nan'
  s.mean = _ave
  '''

  s.pop()
  s[0] = point(1.0,1.0)
  s[1] = point(-1.0,1.0)
  assert almostEqual(s.mean, 0.0)
  assert almostEqual(s.range, 2.0)
  assert almostEqual(s.mass, 2.0)
  s.normalize()
  s.mean = 1.0
  assert almostEqual(s.mean, 1.0)
  assert almostEqual(s.range, 2.0)
  assert almostEqual(s.mass, 1.0)

  s[0] = point(1.0,1.0)
  s[1] = point(1.0,1.0)
  assert almostEqual(s.mean, 1.0)
  assert almostEqual(s.range, 0.0)
  assert almostEqual(s.mass, 2.0)

  s.range = 1.0
  assert str(s.mean) == 'nan'
  assert str(s.range) == 'nan'
  assert almostEqual(s.mass, 2.0)
  return
예제 #4
0
def test_set_behavior():
  from mystic.math import almostEqual
  from numpy import inf

  # check basic behavior for set of two points
  s = set([point(1.0,1.0), point(3.0,2.0)]) #XXX: s + [pt, pt] => broken
  assert almostEqual(s.mean, 2.33333333)
  assert almostEqual(s.range, 2.0)
  assert almostEqual(s.mass, 3.0)

  # basic behavior for an admissable set
  s.normalize()
  s.mean = 1.0
  s.range = 1.0
  assert almostEqual(s.mean, 1.0)
  assert almostEqual(s.range, 1.0)
  assert almostEqual(s.mass, 1.0)

  # add and remove points
  # test special cases: SUM(weights)=0, RANGE(samples)=0, SUM(samples)=0
  s.append(point(1.0,-1.0))
  assert s.mean == -inf
  assert almostEqual(s.range, 1.0)
  assert almostEqual(s.mass, 0.0)

  '''
  _ave = s.mean
  s.mean = 1.0
  assert str(s.mean) == 'nan'
  assert str(s.range) == 'nan'
  assert almostEqual(s.mass, 0.0)
  s.normalize()
  assert str(s.mass) == 'nan'
  s.mean = _ave
  '''

  s.pop()
  s[0] = point(1.0,1.0)
  s[1] = point(-1.0,1.0)
  assert almostEqual(s.mean, 0.0)
  assert almostEqual(s.range, 2.0)
  assert almostEqual(s.mass, 2.0)
  s.normalize()
  s.mean = 1.0
  assert almostEqual(s.mean, 1.0)
  assert almostEqual(s.range, 2.0)
  assert almostEqual(s.mass, 1.0)

  s[0] = point(1.0,1.0)
  s[1] = point(1.0,1.0)
  assert almostEqual(s.mean, 1.0)
  assert almostEqual(s.range, 0.0)
  assert almostEqual(s.mass, 2.0)

  s.range = 1.0
  assert str(s.mean) == 'nan'
  assert str(s.range) == 'nan'
  assert almostEqual(s.mass, 2.0)
  return
예제 #5
0
def test_collection_behavior():
    from mystic.math import almostEqual
    from numpy import inf

    def f(x):
        return sum(x)  # a test function for expectation value

    # build three sets (x,y,z)
    sx = set([point(1.0, 1.0), point(2.0, 1.0), point(3.0, 2.0)])
    sy = set([point(0.0, 1.0), point(3.0, 2.0)])
    sz = set([point(1.0, 1.0), point(2.0, 3.0)])
    #NOTE: for marc_surr(x,y,z), we must have x > 0.0
    sx.normalize()
    sy.normalize()
    sz.normalize()
    assert sx.mass == sy.mass == sz.mass == 1.0

    # build a collection
    c = collection([sx, sy, sz])
    xpos = c[0].positions
    ypos = c[1].positions
    zpos = c[2].positions
    xwts = c[0].weights
    ywts = c[1].weights
    zwts = c[2].weights
    if disp:
        print("x_positions: %s" % xpos)
        print("y_positions: %s" % ypos)
        print("z_positions: %s" % zpos)
        print("x_weights: %s" % xwts)
        print("y_weights: %s" % ywts)
        print("z_weights: %s" % zwts)
    assert xpos == sx.positions
    assert ypos == sy.positions
    assert zpos == sz.positions
    assert xwts == sx.weights
    assert ywts == sy.weights
    assert zwts == sz.weights
    tol = .2
    supp = c.support(tol)
    positions = c.positions
    weights = c.weights
    assert supp == [p for (p, w) in zip(positions, weights) if w > tol]
    if disp:
        print("support points:\n %s" % supp)
        print("npts: %s (i.e. %s)" % (c.npts, c.pts))
        print("weights: %s" % weights)
        print("positions: %s" % positions)
    assert c.npts == sx.npts * sy.npts * sz.npts
    assert len(weights) == len(positions) == c.npts
    assert sx.positions in c.pos
    assert sy.positions in c.pos
    assert sz.positions in c.pos
    assert sx.weights in c.wts
    assert sy.weights in c.wts
    assert sz.weights in c.wts

    c_exp = c.expect(f)
    if disp:
        print("mass: %s" % c.mass)
        print("expect: %s" % c_exp)
    assert c.mass == [sx.mass, sy.mass, sz.mass]
    assert c_exp == expectation(f, c.positions, c.weights)

    #print("center: %s" % c.center)
    #print("delta: %s" % c.delta)

    # change the positions in the collection
    points = [list(i) for i in positions[::3]]
    for i in range(len(points)):
        points[i][0] = 0.5
    positions[::3] = points
    c.positions = positions

    _xpos = c[0].positions
    _ypos = c[1].positions
    _zpos = c[2].positions
    _cexp = c.expect(f)
    if disp:
        print("x_positions: %s" % _xpos)
        print("y_positions: %s" % _ypos)
        print("z_positions: %s" % _zpos)
        print("expect: %s" % _cexp)
    assert _xpos == [0.5] + xpos[1:]
    assert _ypos == ypos
    assert _zpos == zpos
    assert _cexp < c_exp  # due to _xpos[0] is 0.5 and less than 1.0

    _mean = 85.0
    _range = 0.25

    c.set_expect(_mean, f, npop=40, maxiter=200, tol=_range)
    _exp = c.expect(f)
    if disp:
        print("mean: %s" % _mean)
        print("range: %s" % _range)
        print("expect: %s" % _exp)
    assert almostEqual(_mean, _exp, tol=_mean * 0.01)

    # a test function for probability of failure
    def g(x):
        if f(x) <= 0.0: return False
        return True

    pof = c.pof(g)
    spof = c.sampled_pof(g, npts=10000)
    if disp:
        print("pof: %s" % pof)
        print("sampled_pof: %s" % spof)
    assert almostEqual(pof, spof, tol=0.02)
    return
예제 #6
0
def test_collection_behavior():
    from mystic.math import almostEqual
    from numpy import inf

    def f(x):
        return sum(x)  # a test function for expectation value

    # build three sets (x,y,z)
    sx = set([point(1.0, 1.0), point(2.0, 1.0), point(3.0, 2.0)])
    sy = set([point(0.0, 1.0), point(3.0, 2.0)])
    sz = set([point(1.0, 1.0), point(2.0, 3.0)])
    #NOTE: for marc_surr(x,y,z), we must have x > 0.0
    sx.normalize()
    sy.normalize()
    sz.normalize()

    # build a collection
    c = collection([sx, sy, sz])
    print "x_positions: %s" % c[0].positions
    print "y_positions: %s" % c[1].positions
    print "z_positions: %s" % c[2].positions
    print "x_weights: %s" % c[0].weights
    print "y_weights: %s" % c[1].weights
    print "z_weights: %s" % c[2].weights
    print "randomly selected support:\n %s" % c.support(10)

    print "npts: %s (i.e. %s)" % (c.npts, c.pts)
    print "weights: %s" % c.weights
    positions = c.positions
    print "positions: %s" % positions

    print "mass: %s" % c.mass
    print "expect: %s" % c.expect(f)

    #print "center: %s" % c.center
    #print "delta: %s" % c.delta

    # change the positions in the collection
    positions[::3]
    points = [list(i) for i in positions[::3]]
    for i in range(len(points)):
        points[i][0] = 0.5

    positions[::3] = points
    c.positions = positions
    print "x_positions: %s" % c[0].positions
    print "y_positions: %s" % c[1].positions
    print "z_positions: %s" % c[2].positions
    print "expect: %s" % c.expect(f)

    _mean = 85.0
    _range = 0.25

    c.set_expect((_mean, _range), f)
    print "mean: %s" % _mean
    print "range: %s" % _range
    print "expect: %s" % c.expect(f)

    # a test function for probability of failure
    def g(x):
        if f(x) <= 0.0: return False
        return True

    print "pof: %s" % c.pof(g)
    print "sampled_pof: %s" % c.sampled_pof(g, npts=10000)
    return
예제 #7
0
def test_collection_behavior():
  from mystic.math import almostEqual
  from numpy import inf
  def f(x): return sum(x)  # a test function for expectation value

  # build three sets (x,y,z)
  sx = set([point(1.0,1.0), point(2.0,1.0), point(3.0,2.0)])
  sy = set([point(0.0,1.0), point(3.0,2.0)])
  sz = set([point(1.0,1.0), point(2.0,3.0)])
  #NOTE: for marc_surr(x,y,z), we must have x > 0.0
  sx.normalize()
  sy.normalize()
  sz.normalize()
  assert sx.mass == sy.mass == sz.mass == 1.0

  # build a collection
  c = collection([sx,sy,sz])
  xpos = c[0].positions
  ypos = c[1].positions
  zpos = c[2].positions
  xwts = c[0].weights
  ywts = c[1].weights
  zwts = c[2].weights
  if disp:
    print "x_positions: %s" % xpos
    print "y_positions: %s" % ypos
    print "z_positions: %s" % zpos
    print "x_weights: %s" % xwts
    print "y_weights: %s" % ywts
    print "z_weights: %s" % zwts
  assert xpos == sx.positions
  assert ypos == sy.positions
  assert zpos == sz.positions
  assert xwts == sx.weights
  assert ywts == sy.weights
  assert zwts == sz.weights
  tol = .2
  supp = c.support(tol)
  positions = c.positions
  weights = c.weights
  assert supp == [p for (p,w) in zip(positions,weights) if w > tol]
  if disp:
    print "support points:\n %s" % supp
    print "npts: %s (i.e. %s)" % (c.npts, c.pts)
    print "weights: %s" % weights
    print "positions: %s" % positions
  assert c.npts == sx.npts * sy.npts * sz.npts
  assert len(weights) == len(positions) == c.npts
  assert sx.positions in c.pos
  assert sy.positions in c.pos
  assert sz.positions in c.pos
  assert sx.weights in c.wts
  assert sy.weights in c.wts
  assert sz.weights in c.wts

  c_exp = c.expect(f)
  if disp:
    print "mass: %s" % c.mass
    print "expect: %s" % c_exp
  assert c.mass == [sx.mass, sy.mass, sz.mass]
  assert c_exp == expectation(f, c.positions, c.weights)

 #print "center: %s" % c.center
 #print "delta: %s" % c.delta

  # change the positions in the collection
  points = [ list(i) for i in positions[::3] ]
  for i in range(len(points)):
    points[i][0] = 0.5
  positions[::3] = points
  c.positions = positions

  _xpos = c[0].positions
  _ypos = c[1].positions
  _zpos = c[2].positions
  _cexp = c.expect(f)
  if disp:
    print "x_positions: %s" % _xpos
    print "y_positions: %s" % _ypos
    print "z_positions: %s" % _zpos
    print "expect: %s" % _cexp
  assert _xpos == [0.5] + xpos[1:]
  assert _ypos == ypos
  assert _zpos == zpos
  assert _cexp < c_exp # due to _xpos[0] is 0.5 and less than 1.0

  _mean = 85.0
  _range = 0.25

  c.set_expect((_mean,_range), f)
  _exp = c.expect(f)
  if disp:
    print "mean: %s" % _mean
    print "range: %s" % _range
    print "expect: %s" % _exp
  assert almostEqual(_mean, _exp, tol=_mean*0.01)

  # a test function for probability of failure
  def g(x):
    if f(x) <= 0.0: return False
    return True
  pof = c.pof(g)
  spof = c.sampled_pof(g, npts=10000)
  if disp:
    print "pof: %s" % pof
    print "sampled_pof: %s" % spof
  assert almostEqual(pof, spof, tol=0.02)
  return
예제 #8
0
def test_collection_behavior():
  from mystic.math import almostEqual
  from numpy import inf
  def f(x): return sum(x)  # a test function for expectation value

  # build three sets (x,y,z)
  sx = set([point(1.0,1.0), point(2.0,1.0), point(3.0,2.0)])
  sy = set([point(0.0,1.0), point(3.0,2.0)])
  sz = set([point(1.0,1.0), point(2.0,3.0)])
  #NOTE: for marc_surr(x,y,z), we must have x > 0.0
  sx.normalize()
  sy.normalize()
  sz.normalize()

  # build a collection
  c = collection([sx,sy,sz])
  print "x_positions: %s" % c[0].positions
  print "y_positions: %s" % c[1].positions
  print "z_positions: %s" % c[2].positions
  print "x_weights: %s" % c[0].weights
  print "y_weights: %s" % c[1].weights
  print "z_weights: %s" % c[2].weights
  print "randomly selected support:\n %s" % c.support(10)

  print "npts: %s (i.e. %s)" % (c.npts, c.pts)
  print "weights: %s" % c.weights
  positions = c.positions
  print "positions: %s" % positions

  print "mass: %s" % c.mass
  print "expect: %s" % c.expect(f)

 #print "center: %s" % c.center
 #print "delta: %s" % c.delta

  # change the positions in the collection
  positions[::3]
  points = [ list(i) for i in positions[::3] ]
  for i in range(len(points)):
    points[i][0] = 0.5

  positions[::3] = points
  c.positions = positions
  print "x_positions: %s" % c[0].positions
  print "y_positions: %s" % c[1].positions
  print "z_positions: %s" % c[2].positions
  print "expect: %s" % c.expect(f)

  _mean = 85.0
  _range = 0.25

  c.set_expect((_mean,_range), f)
  print "mean: %s" % _mean
  print "range: %s" % _range
  print "expect: %s" % c.expect(f)

  # a test function for probability of failure
  def g(x):
    if f(x) <= 0.0: return False
    return True
  print "pof: %s" % c.pof(g)
  print "sampled_pof: %s" % c.sampled_pof(g, npts=10000)
  return