def test_trig_pJac():
    """Boolean condition asserts that value and derivative of a function of the Autodiff class comprising several elementary operations are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    p = [1]
    c = [0.5]

    def myfunc(x):
        a = (EF.cos(x))
        b = (EF.arcsin(x))
        c = (EF.arctan(x))
        return a - b + c

    f_obj = ADiff(myfunc)
    res = f_obj.pJac(c, p)
    expectAns = {
        'diff':
        -math.sin(c[0]) - (1 / math.sqrt(1 - c[0]**2)) + (1 / (1 + c[0]**2)),
        'value':
        math.cos(c[0]) - math.asin(c[0]) + math.atan(c[0])
    }
    assert res == expectAns
def test_vec_func2():
    """Boolean condition asserts that value and derivative of a function of the Autodiff class comprising several elementary operations are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """

    c = [1, 2]
    p = [1, 1]

    def myfunc(x, y):
        a = EF.exp_base(2, x)  #base 2 and exponent x
        b = EF.logistic(y)
        c = EF.log(y, 2)  #log with base 2
        return a + b + c

    f_obj = ADiff(myfunc)
    res = f_obj.pJac(c, p)

    expectAns = {
        'diff':
        math.pow(2, c[0]) + 1 / (1 + math.exp(-c[1])) *
        (1 - (1 / (1 + math.exp(-c[1])))) + 1 / ((c[1]) * math.log(2)),
        'value':
        math.pow(2, c[0]) + (1 / (1 + math.exp(-c[1]))) + math.log(c[1], 2)
    }

    assert res == expectAns
def test_trig2_vector():
    """Boolean condition asserts that value and derivative of a function of the Autodiff class comprising several elementary operations are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    p = [1, 1, 1]
    c = [0.5, 0.5, 3]

    def myfunc(x, y, z):
        a = (EF.sin(x))
        b = (EF.arccos(y))
        c = (EF.tan(z))
        return a + b + c

    f_obj = ADiff(myfunc)
    res = f_obj.pJac(c, p)
    calc_diff = round(res['diff'], 10)
    assert {
        'diff': round(0.7432015404535481, 10),
        'value': math.sin(c[0]) + math.acos(c[1]) + math.tan(c[2])
    } == {
        'diff': round(res['diff'], 10),
        'value': res['value']
    }  #diff values differ at last digits when calculate with math.cos(c[0])- 1/(math.sqrt(1-c[1]**2))+ 1/(math.cos(c[2])*math.cos(c[2]))
def test_power():
    """Boolean condition asserts that value and derivative of a power function using the AutoDiff class are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = [1, 2]

    def myfunc(x, y):
        f1 = 3**x**y**1
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {
        'diff': [
            math.log(3) * 3**(c[0]**c[1]) * c[1] * c[0]**(c[1] - 1),
            math.log(3) * 3**(c[0]**c[1]) * math.log(c[0]) * c[0]**c[1]
        ],
        'value':
        3**1**2
    }

    assert res == expectAns
Exemple #5
0
    def __init__(self, target):

        """
        The constructor initialize all tuning parameters and target
        """
        np.random.seed(1)

        self.target=target
        # Count number of argument for the target
        self.dim=target.__code__.co_argcount

        # log target which we will require AD
        def logtarget(*arg):
            return EF.log(target(*arg))

        # Instantiate the AD object
        self.AD_logtarget=ADiff(logtarget)

        # This is used to percentage of accepted proposals for each run of the sampling algorithm
        # For MALA, the ideal rate is around 0.9 or higher, which the user should try to achieve by adjust tau
        self.accptRatio=-1

        # This is the average move size which should assit the user in tuning their parameters
        # in combination with acceptance ratio
        self.accptmoveSize=-1

        # This is the variance of the move size which should assit the user in tuning their parameters
        # in combination with acceptance ratio and the average move size
        self.varaccptmoveSize=-1
def test_arccos():
    """Boolean condition asserts that value and derivative of the inverse of cosine of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 0.5

    def myfunc(x):
        f1 = EF.arccos(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)
    expectAns = {'diff': -1 / math.sqrt(1 - c**2), 'value': math.acos(c)}
    assert res == expectAns
def test_neg_sub():
    """Boolean condition asserts that value and derivative of a subtraction using the AutoDiff class are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = [1, 2]

    def myfunc(x, y):
        f1 = 1 - x - y - 2
        return -f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {'diff': [1, 1], 'value': 4}

    assert res == expectAns
def test_log():
    """Boolean condition asserts that value and derivative of the natural logarithm of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 14

    def myfunc(x):
        f1 = EF.log(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {'diff': 1 / c, 'value': math.log(c)}

    assert res == expectAns
def test_cot():
    """Boolean condition asserts that value and derivative of the cotangent of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 0.5

    def myfunc(x):
        f1 = EF.cot(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)
    expectAns = {
        'diff': 2 / (math.cos(c * 2) - 1),
        'value': math.cos(c) / math.sin(c)
    }
    assert res == expectAns
def test_eq():
    """Boolean condition asserts that value and derivative of an AutoDiff instance are equal to that of a different Autodiff instance.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    def myfunc1(x, y):
        f1 = 1 * x * y * 2
        return f1

    def myfunc2(x, y):
        f1 = 1 * x * y * 4
        return f1

    f_obj1 = ADiff(myfunc1)
    res1 = f_obj1 == f_obj1
    f_obj2 = ADiff(myfunc2)
    res2 = f_obj1 == f_obj2

    assert res1 == True and res2 == False
def test_cosh():
    """Boolean condition asserts that value and derivative of the cosecant of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 2

    def myfunc(x):
        f1 = EF.cosh(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {
        'diff': 3.626860407847019,
        'value': math.cosh(c)
    }  #sinh(x) differ in last digits
    assert res == expectAns
def test_tanh():
    """Boolean condition asserts that value and derivative of the cosecant of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 14

    def myfunc(x):
        f1 = EF.tanh(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)
    expectAns = {
        'diff': (1 / ((math.exp(c) + math.exp(-c)) / 2) *
                 ((math.exp(c) + math.exp(-c)) / 2)),
        'value':
        ((math.exp(c) - math.exp(-c)) / 2) / ((math.exp(c) + math.exp(-c)) / 2)
    }
    assert res == expectAns
def test_vec_func1():
    """Boolean condition asserts that value and derivative of a function of the Autodiff class comprising several elementary operations are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    def myfunc(x, y):
        f1 = x * y
        f2 = EF.sin(x)
        f3 = 10
        f4 = x + y + EF.sin(x * y) + 10
        return [f1 + f2, -(f3 - f4)]

    f_obj = ADiff(myfunc)
    res = f_obj.Jac([1, 2])

    expectAns = {
        'diff': [[2.5403023058681398, 1.0],
                 [0.1677063269057152, 0.5838531634528576]],
        'value': [2.8414709848078967, 3.909297426825681]
    }

    assert res == expectAns