Example #1
0
 def test_logDetPosDef(self, numPoints = 200):
     for pointIndex in range(numPoints):
         n = randint(0, 20)
         factor = randn(n, n)
         mat = np.dot(factor, factor.T)
         trueDet = la.det(mat) if n > 0 else 1.0
         assert_allclose(np.exp(mathhelp.logDetPosDef(mat)), abs(trueDet))
Example #2
0
 def test_logDetPosDef(self, numPoints=200):
     for pointIndex in range(numPoints):
         n = randint(0, 20)
         factor = randn(n, n)
         mat = np.dot(factor, factor.T)
         trueDet = la.det(mat) if n > 0 else 1.0
         assert_allclose(np.exp(mathhelp.logDetPosDef(mat)), abs(trueDet))
Example #3
0
def check_derivParamsDeriv(transform, x, eps):
    assert np.shape(x) == ()
    direction = randn()
    numericDeriv = (transform.derivParams(x + direction * eps) -
                    transform.derivParams(x - direction * eps)) / (eps * 2.0)
    analyticDeriv = np.dot(direction, transform.derivParamsDeriv(x))
    assert_allclose(numericDeriv, analyticDeriv, atol = 1e-6, rtol = 1e-4)
Example #4
0
def check_derivParamsDeriv(transform, x, eps):
    assert np.shape(x) == ()
    direction = randn()
    numericDeriv = (transform.derivParams(x + direction * eps) -
                    transform.derivParams(x - direction * eps)) / (eps * 2.0)
    analyticDeriv = np.dot(direction, transform.derivParamsDeriv(x))
    assert_allclose(numericDeriv, analyticDeriv, atol=1e-6, rtol=1e-4)
Example #5
0
def check_derivParams(transform, x, eps):
    params = transform.params
    paramsDirection = randn(*np.shape(params))
    numericDeriv = (transform.parseAll(params + paramsDirection * eps)(x) -
                    transform.parseAll(params - paramsDirection * eps)
                    (x)) / (eps * 2.0)
    analyticDeriv = np.dot(paramsDirection, transform.derivParams(x))
    assert_allclose(numericDeriv, analyticDeriv, atol=1e-6, rtol=1e-4)
Example #6
0
def check_logJacDerivInput(outputTransform, input, x, eps):
    direction = randn(*np.shape(input))
    numericDeriv = (outputTransform.logJac(input + direction * eps, x) -
                    outputTransform.logJac(input - direction * eps, x)) / (
                        eps * 2.0)
    analyticDeriv = np.dot(direction,
                           outputTransform.logJacDerivInput(input, x))
    assert_allclose(numericDeriv, analyticDeriv, atol=1e-6, rtol=1e-4)
Example #7
0
def check_logJacDerivInput(outputTransform, input, x, eps):
    direction = randn(*np.shape(input))
    numericDeriv = (
        outputTransform.logJac(input + direction * eps, x) -
        outputTransform.logJac(input - direction * eps, x)
    ) / (eps * 2.0)
    analyticDeriv = np.dot(direction, outputTransform.logJacDerivInput(input, x))
    assert_allclose(numericDeriv, analyticDeriv, atol = 1e-6, rtol = 1e-4)
Example #8
0
 def test_logDet(self, numPoints=200):
     for pointIndex in range(numPoints):
         n = randint(0, 20)
         if randint(0, 10) == 0:
             A = np.zeros([n, n])
         else:
             A = randn(n, n)
         trueDet = la.det(A) if n > 0 else 1.0
         assert_allclose(np.exp(mathhelp.logDet(A)), abs(trueDet))
Example #9
0
def check_logJacDerivParams(transform, x, eps):
    params = transform.params
    paramsDirection = randn(*np.shape(params))
    numericDerivLJ = (
        transform.parseAll(params + paramsDirection * eps).logJac(x) -
        transform.parseAll(params - paramsDirection * eps).logJac(x)
    ) / (eps * 2.0)
    analyticDerivLJ = np.dot(transform.logJacDerivParams(x), paramsDirection)
    assert_allclose(numericDerivLJ, analyticDerivLJ, atol = 1e-6, rtol = 1e-4)
Example #10
0
def check_derivParams(transform, x, eps):
    params = transform.params
    paramsDirection = randn(*np.shape(params))
    numericDeriv = (
        transform.parseAll(params + paramsDirection * eps)(x) -
        transform.parseAll(params - paramsDirection * eps)(x)
    ) / (eps * 2.0)
    analyticDeriv = np.dot(paramsDirection, transform.derivParams(x))
    assert_allclose(numericDeriv, analyticDeriv, atol = 1e-6, rtol = 1e-4)
Example #11
0
def check_logJacDerivParams(transform, x, eps):
    params = transform.params
    paramsDirection = randn(*np.shape(params))
    numericDerivLJ = (
        transform.parseAll(params + paramsDirection * eps).logJac(x) -
        transform.parseAll(params - paramsDirection * eps).logJac(x)) / (eps *
                                                                         2.0)
    analyticDerivLJ = np.dot(transform.logJacDerivParams(x), paramsDirection)
    assert_allclose(numericDerivLJ, analyticDerivLJ, atol=1e-6, rtol=1e-4)
Example #12
0
 def test_logDet(self, numPoints = 200):
     for pointIndex in range(numPoints):
         n = randint(0, 20)
         if randint(0, 10) == 0:
             A = np.zeros([n, n])
         else:
             A = randn(n, n)
         trueDet = la.det(A) if n > 0 else 1.0
         assert_allclose(np.exp(mathhelp.logDet(A)), abs(trueDet))
Example #13
0
    def __init__(self, protoNoSplit, fullQuestion, protoForAnswer):
        self.protoNoSplit = protoNoSplit
        self.fullQuestion = fullQuestion
        self.protoForAnswer = protoForAnswer

        assert len(self.protoForAnswer) >= 1
        if self.fullQuestion is None:
            assert self.protoForAnswer == [self.protoNoSplit]
        assert_allclose(sum([proto.count for proto in self.protoForAnswer]),
                        self.protoNoSplit.count)
Example #14
0
    def test_reprArray(self, numPoints=200):
        def evalArray(arrRepr):
            from numpy import array, zeros, dtype
            return eval(arrRepr)

        for _ in range(numPoints):
            shape = shapeRand(ranks=[0, 1, 2, 3])
            arr = randn(*shape)
            arrRepr = mathhelp.reprArray(arr)
            arrAgain = evalArray(arrRepr)
            assert_allclose(arrAgain, arr)
Example #15
0
    def test_reprArray(self, numPoints = 200):
        def evalArray(arrRepr):
            from numpy import array, zeros, dtype
            return eval(arrRepr)

        for _ in range(numPoints):
            shape = shapeRand(ranks = [0, 1, 2, 3])
            arr = randn(*shape)
            arrRepr = mathhelp.reprArray(arr)
            arrAgain = evalArray(arrRepr)
            assert_allclose(arrAgain, arr)
Example #16
0
    def __init__(self, protoNoSplit, fullQuestion, protoForAnswer):
        self.protoNoSplit = protoNoSplit
        self.fullQuestion = fullQuestion
        self.protoForAnswer = protoForAnswer

        assert len(self.protoForAnswer) >= 1
        if self.fullQuestion is None:
            assert self.protoForAnswer == [self.protoNoSplit]
        assert_allclose(
            sum([ proto.count for proto in self.protoForAnswer ]),
            self.protoNoSplit.count
        )
Example #17
0
def checkDiscreteTransform(transform, domain, codomain, its,
                           checkAdditional = None):
    assert transform.tag is not None
    transformEvaled = xf.eval_local(repr(transform))
    assert transformEvaled.tag == transform.tag
    assert repr(transform) == repr(transformEvaled)
    for it in range(its):
        x = random.choice(domain)
        if checkAdditional is not None:
            checkAdditional(transform, x)
        assert transform(x) in codomain
        assert_allclose(transformEvaled(x), transform(x))
Example #18
0
def checkTransform(transform, shapeIn, invertible, hasDeriv, hasParams, is1D, eps, its, checkAdditional = None):
    assert transform.tag is not None
    transformEvaled = xf.eval_local(repr(transform))
    assert transformEvaled.tag == transform.tag
    assert repr(transform) == repr(transformEvaled)
    if hasParams:
        params = transform.params
        transformParsed = transform.parseAll(params)
        assert transformParsed.tag == transform.tag
        assert_allclose(transformParsed.params, params)
        assert_allclose(transformEvaled.params, params)
    for it in range(its):
        x = randn(*shapeIn)
        if checkAdditional is not None:
            checkAdditional(transform, x, eps)
        if True:
            assert_allclose(transformEvaled(x), transform(x))
        if hasParams:
            assert_allclose(transformParsed(x), transform(x))
        if hasDeriv:
            check_deriv(transform, x, eps)
        if is1D and hasDeriv:
            check_derivDeriv(transform, x, eps)
        if hasParams:
            check_derivParams(transform, x, eps)
        if hasDeriv and hasParams and is1D:
            check_derivParamsDeriv(transform, x, eps)
        if invertible and hasDeriv:
            check_logJac(transform, x)
        if invertible and hasDeriv:
            check_logJacDeriv(transform, x, eps)
        if invertible and hasDeriv and hasParams:
            check_logJacDerivParams(transform, x, eps)
        if invertible:
            check_inv(transform, x, randn(*shapeIn))
Example #19
0
def checkDiscreteTransform(transform,
                           domain,
                           codomain,
                           its,
                           checkAdditional=None):
    assert transform.tag is not None
    transformEvaled = xf.eval_local(repr(transform))
    assert transformEvaled.tag == transform.tag
    assert repr(transform) == repr(transformEvaled)
    for it in range(its):
        x = random.choice(domain)
        if checkAdditional is not None:
            checkAdditional(transform, x)
        assert transform(x) in codomain
        assert_allclose(transformEvaled(x), transform(x))
Example #20
0
    def test_sampleDiscrete(self, numDists = 20, numSamples = 10000):
        for distIndex in range(numDists):
            n = randint(1, 5)
            s = 0.0
            while s == 0.0:
                probs = np.exp([ randLogProb() for _ in range(n) ])
                s = np.sum(probs)
            probs = probs / s
            assert_allclose(np.sum(probs), 1.0)
            valueProbList = list(enumerate(probs))

            count = np.zeros(n)
            for _ in xrange(numSamples):
                sample = mathhelp.sampleDiscrete(valueProbList)
                count[sample] += 1.0
            assert_allclose(count / numSamples, probs, rtol = 1e-2, atol = 1e-2)
Example #21
0
    def test_sampleDiscrete(self, numDists=20, numSamples=10000):
        for distIndex in range(numDists):
            n = randint(1, 5)
            s = 0.0
            while s == 0.0:
                probs = np.exp([randLogProb() for _ in range(n)])
                s = np.sum(probs)
            probs = probs / s
            assert_allclose(np.sum(probs), 1.0)
            valueProbList = list(enumerate(probs))

            count = np.zeros(n)
            for _ in xrange(numSamples):
                sample = mathhelp.sampleDiscrete(valueProbList)
                count[sample] += 1.0
            assert_allclose(count / numSamples, probs, rtol=1e-2, atol=1e-2)
Example #22
0
def checkOutputTransform(outputTransform,
                         shapeInput,
                         shapeOutput,
                         hasParams,
                         eps,
                         its,
                         checkAdditional=None):
    outputTransformEvaled = xf.eval_local(repr(outputTransform))
    assert repr(outputTransform) == repr(outputTransformEvaled)
    if hasParams:
        params = outputTransform.params
        outputTransformParsed = outputTransform.parseAll(params)
        assert_allclose(outputTransformParsed.params, params)
        assert_allclose(outputTransformEvaled.params, params)
    for it in range(its):
        input = randn(*shapeInput)
        x = randn(*shapeOutput)
        transform = outputTransform.atInput(input)
        if checkAdditional is not None:
            checkAdditional(outputTransform, input, x, eps)
        if True:
            assert_allclose(outputTransform(input, x), transform(x))
        if True:
            assert_allclose(outputTransformEvaled(input, x),
                            outputTransform(input, x))
        if hasParams:
            assert_allclose(outputTransformParsed(input, x),
                            outputTransform(input, x))
        if True:
            check_deriv(transform, x, eps)
        if True:
            check_derivInput(outputTransform, input, x, eps)
        if hasParams:
            check_derivParams(transform, x, eps)
        if True:
            check_logJac(transform, x)
        if True:
            check_logJacDeriv(transform, x, eps)
        if True:
            check_logJacDerivInput(outputTransform, input, x, eps)
        if hasParams:
            check_logJacDerivParams(transform, x, eps)
        if True:
            check_inv(transform, x, randn(*shapeOutput))
Example #23
0
def checkOutputTransform(outputTransform, shapeInput, shapeOutput, hasParams, eps, its, checkAdditional = None):
    outputTransformEvaled = xf.eval_local(repr(outputTransform))
    assert repr(outputTransform) == repr(outputTransformEvaled)
    if hasParams:
        params = outputTransform.params
        outputTransformParsed = outputTransform.parseAll(params)
        assert_allclose(outputTransformParsed.params, params)
        assert_allclose(outputTransformEvaled.params, params)
    for it in range(its):
        input = randn(*shapeInput)
        x = randn(*shapeOutput)
        transform = outputTransform.atInput(input)
        if checkAdditional is not None:
            checkAdditional(outputTransform, input, x, eps)
        if True:
            assert_allclose(outputTransform(input, x), transform(x))
        if True:
            assert_allclose(outputTransformEvaled(input, x), outputTransform(input, x))
        if hasParams:
            assert_allclose(outputTransformParsed(input, x), outputTransform(input, x))
        if True:
            check_deriv(transform, x, eps)
        if True:
            check_derivInput(outputTransform, input, x, eps)
        if hasParams:
            check_derivParams(transform, x, eps)
        if True:
            check_logJac(transform, x)
        if True:
            check_logJacDeriv(transform, x, eps)
        if True:
            check_logJacDerivInput(outputTransform, input, x, eps)
        if hasParams:
            check_logJacDerivParams(transform, x, eps)
        if True:
            check_inv(transform, x, randn(*shapeOutput))
Example #24
0
def checkTransform(transform,
                   shapeIn,
                   invertible,
                   hasDeriv,
                   hasParams,
                   is1D,
                   eps,
                   its,
                   checkAdditional=None):
    assert transform.tag is not None
    transformEvaled = xf.eval_local(repr(transform))
    assert transformEvaled.tag == transform.tag
    assert repr(transform) == repr(transformEvaled)
    if hasParams:
        params = transform.params
        transformParsed = transform.parseAll(params)
        assert transformParsed.tag == transform.tag
        assert_allclose(transformParsed.params, params)
        assert_allclose(transformEvaled.params, params)
    for it in range(its):
        x = randn(*shapeIn)
        if checkAdditional is not None:
            checkAdditional(transform, x, eps)
        if True:
            assert_allclose(transformEvaled(x), transform(x))
        if hasParams:
            assert_allclose(transformParsed(x), transform(x))
        if hasDeriv:
            check_deriv(transform, x, eps)
        if is1D and hasDeriv:
            check_derivDeriv(transform, x, eps)
        if hasParams:
            check_derivParams(transform, x, eps)
        if hasDeriv and hasParams and is1D:
            check_derivParamsDeriv(transform, x, eps)
        if invertible and hasDeriv:
            check_logJac(transform, x)
        if invertible and hasDeriv:
            check_logJacDeriv(transform, x, eps)
        if invertible and hasDeriv and hasParams:
            check_logJacDerivParams(transform, x, eps)
        if invertible:
            check_inv(transform, x, randn(*shapeIn))
Example #25
0
 def checkAdditional(transform, x, eps):
     assert_allclose(transform(x), x)
Example #26
0
def check_inv(transform, x, y):
    """(N.B. x and y not supposed to correspond to each other)"""
    xAgain = transform.inv(transform(x))
    assert_allclose(xAgain, x, msg='inverse not consistent')
    yAgain = transform(transform.inv(y))
    assert_allclose(yAgain, y, msg='inverse not consistent')
Example #27
0
 def test_logAdd(self, numPoints = 200):
     for pointIndex in range(numPoints):
         a = randLogProb()
         b = randLogProb()
         r = mathhelp.logAdd(a, b)
         assert_allclose(np.exp(r), np.exp(a) + np.exp(b))
Example #28
0
def check_logJacDeriv(transform, x, eps):
    direction = randn(*np.shape(x))
    numericDeriv = (transform.logJac(x + direction * eps) -
                    transform.logJac(x - direction * eps)) / (eps * 2.0)
    analyticDeriv = np.dot(direction, transform.logJacDeriv(x))
    assert_allclose(numericDeriv, analyticDeriv, atol=1e-5, rtol=1e-4)
Example #29
0
def check_logJac(transform, x):
    numericLJ = computeLogJac(transform, x)
    analyticLJ = transform.logJac(x)
    assert np.shape(analyticLJ) == ()
    assert_allclose(numericLJ, analyticLJ, atol=1e-10)
Example #30
0
 def checkAdditional(transform, x, eps):
     assert_allclose(transform(x), x)
Example #31
0
 def checkAdditional(outputTransform, input, x, eps):
     assert_allclose(outputTransform(input, x),
                     x + outputTransform.shift(input))
Example #32
0
 def test_logAdd(self, numPoints=200):
     for pointIndex in range(numPoints):
         a = randLogProb()
         b = randLogProb()
         r = mathhelp.logAdd(a, b)
         assert_allclose(np.exp(r), np.exp(a) + np.exp(b))
Example #33
0
def check_logJacDeriv(transform, x, eps):
    direction = randn(*np.shape(x))
    numericDeriv = (transform.logJac(x + direction * eps) -
                    transform.logJac(x - direction * eps)) / (eps * 2.0)
    analyticDeriv = np.dot(direction, transform.logJacDeriv(x))
    assert_allclose(numericDeriv, analyticDeriv, atol = 1e-5, rtol = 1e-4)
Example #34
0
def check_inv(transform, x, y):
    """(N.B. x and y not supposed to correspond to each other)"""
    xAgain = transform.inv(transform(x))
    assert_allclose(xAgain, x, msg = 'inverse not consistent')
    yAgain = transform(transform.inv(y))
    assert_allclose(yAgain, y, msg = 'inverse not consistent')
Example #35
0
 def checkAdditional(outputTransform, input, x, eps):
     assert_allclose(outputTransform(input, x), outputTransform.transform(x))
Example #36
0
 def checkAdditional(transform, x, eps):
     assert_allclose(transform(x),
                     np.array(map(transform.transform1D, x)))
Example #37
0
 def test_logSum(self, numPoints=200):
     for pointIndex in range(numPoints):
         n = randint(0, 20)
         l = [randLogProb() for _ in range(n)]
         r = mathhelp.logSum(l)
         assert_allclose(np.exp(r), np.sum(np.exp(l)))
Example #38
0
 def checkAdditional(outputTransform, input, x, eps):
     assert_allclose(outputTransform(input, x), x + outputTransform.shift(input))
Example #39
0
 def checkAdditional(outputTransform, input, x, eps):
     assert_allclose(outputTransform(input, x),
                     outputTransform.transform(x))
Example #40
0
def check_logJac(transform, x):
    numericLJ = computeLogJac(transform, x)
    analyticLJ = transform.logJac(x)
    assert np.shape(analyticLJ) == ()
    assert_allclose(numericLJ, analyticLJ, atol = 1e-10)
Example #41
0
 def test_logSum(self, numPoints = 200):
     for pointIndex in range(numPoints):
         n = randint(0, 20)
         l = [ randLogProb() for _ in range(n) ]
         r = mathhelp.logSum(l)
         assert_allclose(np.exp(r), np.sum(np.exp(l)))
Example #42
0
 def checkAdditional(transform, x, eps):
     assert_allclose(transform(x), np.array(map(transform.transform1D, x)))