def test_lagfit(self):
        def f(x):
            return x*(x - 1)*(x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1], [1], -1)
        assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
        assert_raises(TypeError, lag.lagfit, [], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
        assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
        assert_raises(ValueError, lag.lagfit, [1], [1], [-1,])
        assert_raises(ValueError, lag.lagfit, [1], [1], [2, -1, 6])
        assert_raises(TypeError, lag.lagfit, [1], [1], [])

        # Test fit
        x = np.linspace(0, 2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        coef3 = lag.lagfit(x, y, [0, 1, 2, 3])
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        coef4 = lag.lagfit(x, y, [0, 1, 2, 3, 4])
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        coef2d = lag.lagfit(x, np.array([y, y]).T, [0, 1, 2, 3])
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        wcoef3 = lag.lagfit(x, yw, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        # test scaling with complex values x points whose square
        # is zero when summed.
        x = [1, 1j, -1, -1j]
        assert_almost_equal(lag.lagfit(x, x, 1), [1, -1])
        assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1])
Esempio n. 2
0
    def test_lagfit(self):
        def f(x):
            return x*(x - 1)*(x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1], [1], -1)
        assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
        assert_raises(TypeError, lag.lagfit, [], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
        assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
        assert_raises(ValueError, lag.lagfit, [1], [1], [-1,])
        assert_raises(ValueError, lag.lagfit, [1], [1], [2, -1, 6])
        assert_raises(TypeError, lag.lagfit, [1], [1], [])

        # Test fit
        x = np.linspace(0, 2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        coef3 = lag.lagfit(x, y, [0, 1, 2, 3])
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        coef4 = lag.lagfit(x, y, [0, 1, 2, 3, 4])
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        coef2d = lag.lagfit(x, np.array([y, y]).T, [0, 1, 2, 3])
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        wcoef3 = lag.lagfit(x, yw, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        # test scaling with complex values x points whose square
        # is zero when summed.
        x = [1, 1j, -1, -1j]
        assert_almost_equal(lag.lagfit(x, x, 1), [1, -1])
        assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1])
Esempio n. 3
0
 def eval(self, x, u, output_array=None):
     x = np.atleast_1d(x)
     if output_array is None:
         output_array = np.zeros(x.shape)
     w_hat = work[(u, 0, True)]
     w_hat[1:] = u[:-1]
     output_array[:] = lag.lagval(x, u) - lag.lagval(x, w_hat)
     output_array *= np.exp(-x/2)
     return output_array
Esempio n. 4
0
 def test_lagmul(self):
     # check values of result
     for i in range(5):
         pol1 = [0] * i + [1]
         val1 = lag.lagval(self.x, pol1)
         for j in range(5):
             msg = "At i=%d, j=%d" % (i, j)
             pol2 = [0] * j + [1]
             val2 = lag.lagval(self.x, pol2)
             pol3 = lag.lagmul(pol1, pol2)
             val3 = lag.lagval(self.x, pol3)
             assert_(len(pol3) == i + j + 1, msg)
             assert_almost_equal(val3, val1 * val2, err_msg=msg)
Esempio n. 5
0
 def test_lagmul(self) :
     # check values of result
     for i in range(5) :
         pol1 = [0]*i + [1]
         val1 = lag.lagval(self.x, pol1)
         for j in range(5) :
             msg = "At i=%d, j=%d" % (i, j)
             pol2 = [0]*j + [1]
             val2 = lag.lagval(self.x, pol2)
             pol3 = lag.lagmul(pol1, pol2)
             val3 = lag.lagval(self.x, pol3)
             assert_(len(pol3) == i + j + 1, msg)
             assert_almost_equal(val3, val1*val2, err_msg=msg)
 def laguerre_feature_func(x: float,
                           i: int,
                           ident=ident,
                           strike=strike) -> float:
     # noinspection PyTypeChecker
     xp = x / strike
     return np.exp(-xp / 2) * lagval(xp, ident[i])
Esempio n. 7
0
    def test_lagvander(self) :
        # check for 1d x
        x = np.arange(3)
        v = lag.lagvander(x, 3)
        assert_(v.shape == (3, 4))
        for i in range(4) :
            coef = [0]*i + [1]
            assert_almost_equal(v[..., i], lag.lagval(x, coef))

        # check for 2d x
        x = np.array([[1, 2], [3, 4], [5, 6]])
        v = lag.lagvander(x, 3)
        assert_(v.shape == (3, 2, 4))
        for i in range(4) :
            coef = [0]*i + [1]
            assert_almost_equal(v[..., i], lag.lagval(x, coef))
Esempio n. 8
0
    def test_lagvander(self):
        # check for 1d x
        x = np.arange(3)
        v = lag.lagvander(x, 3)
        assert_(v.shape == (3, 4))
        for i in range(4):
            coef = [0] * i + [1]
            assert_almost_equal(v[..., i], lag.lagval(x, coef))

        # check for 2d x
        x = np.array([[1, 2], [3, 4], [5, 6]])
        v = lag.lagvander(x, 3)
        assert_(v.shape == (3, 2, 4))
        for i in range(4):
            coef = [0] * i + [1]
            assert_almost_equal(v[..., i], lag.lagval(x, coef))
Esempio n. 9
0
def fitted_lspi_put_option(
        obj: OptimalExerciseRL, strike: float, expiry: float,
        training_data: Sequence[TrainingDataType], training_iters: int,
        split: int) -> LinearFunctionApprox[Tuple[float, float]]:

    num_laguerre: int = 3
    lspi_reg: float = 0.001

    ident: np.ndarray = np.eye(num_laguerre)
    features: List[Callable[[Tuple[float, float]], float]] = [lambda _: 1.]
    features += [
        (lambda t_s: np.exp(-t_s[1] /
                            (2 * strike)) * lagval(t_s[1] / strike, ident[i]))
        for i in range(num_laguerre)
    ]
    features += [
        lambda t_s: np.cos(-t_s[0] * np.pi / (2 * expiry)),
        lambda t_s: np.log(expiry - t_s[0])
        if t_s[0] != expiry else 0., lambda t_s: (t_s[0] / expiry)**2
    ]

    linear_approx: LinearFunctionApprox[Tuple[float, float]] = \
        obj.linear_func_approx(features=features, reg=lspi_reg)

    return obj.train_lspi(training_data=training_data,
                          init_fa=linear_approx,
                          training_iters=training_iters,
                          split=split)
Esempio n. 10
0
 def laguerre_feature_func(x: float,
                           i: int,
                           ident=ident,
                           strike=strike) -> float:
     # noinspection PyTypeChecker
     return np.exp(-x / (strike * 2)) * \
            lagval(x / strike, ident[i])
Esempio n. 11
0
def fitted_lspi_put_option(
        expiry: float, num_steps: int, num_paths: int, spot_price: float,
        spot_price_frac: float, rate: float, vol: float, strike: float,
        training_iters: int) -> LinearFunctionApprox[Tuple[float, float]]:

    num_laguerre: int = 4
    epsilon: float = 1e-3

    ident: np.ndarray = np.eye(num_laguerre)
    features: List[Callable[[Tuple[float, float]], float]] = [lambda _: 1.]
    features += [(lambda t_s, i=i: np.exp(-t_s[1] / (2 * strike)) * lagval(
        t_s[1] / strike, ident[i])) for i in range(num_laguerre)]
    features += [
        lambda t_s: np.cos(-t_s[0] * np.pi / (2 * expiry)),
        lambda t_s: np.log(expiry - t_s[0])
        if t_s[0] != expiry else 0., lambda t_s: (t_s[0] / expiry)**2
    ]

    training_data: Sequence[TrainingDataType] = training_sim_data(
        expiry=expiry,
        num_steps=num_steps,
        num_paths=num_paths,
        spot_price=spot_price,
        spot_price_frac=spot_price_frac,
        rate=rate,
        vol=vol)

    dt: float = expiry / num_steps
    gamma: float = np.exp(-rate * dt)
    num_features: int = len(features)
    states: Sequence[Tuple[float,
                           float]] = [(i * dt, s) for i, s, _ in training_data]
    next_states: Sequence[Tuple[float, float]] = \
        [((i + 1) * dt, s1) for i, _, s1 in training_data]
    feature_vals: np.ndarray = np.array([[f(x) for f in features]
                                         for x in states])
    next_feature_vals: np.ndarray = np.array([[f(x) for f in features]
                                              for x in next_states])
    non_terminal: np.ndarray = np.array(
        [i < num_steps - 1 for i, _, _ in training_data])
    exer: np.ndarray = np.array([max(strike - s1, 0) for _, s1 in next_states])
    wts: np.ndarray = np.zeros(num_features)
    for _ in range(training_iters):
        a_inv: np.ndarray = np.eye(num_features) / epsilon
        b_vec: np.ndarray = np.zeros(num_features)
        cont: np.ndarray = np.dot(next_feature_vals, wts)
        cont_cond: np.ndarray = non_terminal * (cont > exer)
        for i in range(len(training_data)):
            phi1: np.ndarray = feature_vals[i]
            phi2: np.ndarray = phi1 - \
                cont_cond[i] * gamma * next_feature_vals[i]
            temp: np.ndarray = a_inv.T.dot(phi2)
            a_inv -= np.outer(a_inv.dot(phi1), temp) / (1 + phi1.dot(temp))
            b_vec += phi1 * (1 - cont_cond[i]) * exer[i] * gamma
        wts = a_inv.dot(b_vec)

    return LinearFunctionApprox.create(feature_functions=features,
                                       weights=Weights.create(wts))
Esempio n. 12
0
    def test_lagval(self):
        #check empty input
        assert_equal(lag.lagval([], [1]).size, 0)

        #check normal input)
        x = np.linspace(-1, 1)
        y = [polyval(x, c) for c in Llist]
        for i in range(7):
            msg = "At i=%d" % i
            tgt = y[i]
            res = lag.lagval(x, [0]*i + [1])
            assert_almost_equal(res, tgt, err_msg=msg)

        #check that shape is preserved
        for i in range(3):
            dims = [2]*i
            x = np.zeros(dims)
            assert_equal(lag.lagval(x, [1]).shape, dims)
            assert_equal(lag.lagval(x, [1, 0]).shape, dims)
            assert_equal(lag.lagval(x, [1, 0, 0]).shape, dims)
    def test_lagval(self):
        #check empty input
        assert_equal(lag.lagval([], [1]).size, 0)

        #check normal input)
        x = np.linspace(-1, 1)
        y = [polyval(x, c) for c in Llist]
        for i in range(7):
            msg = "At i=%d" % i
            tgt = y[i]
            res = lag.lagval(x, [0]*i + [1])
            assert_almost_equal(res, tgt, err_msg=msg)

        #check that shape is preserved
        for i in range(3):
            dims = [2]*i
            x = np.zeros(dims)
            assert_equal(lag.lagval(x, [1]).shape, dims)
            assert_equal(lag.lagval(x, [1, 0]).shape, dims)
            assert_equal(lag.lagval(x, [1, 0, 0]).shape, dims)
Esempio n. 14
0
 def test_lagfromroots(self) :
     res = lag.lagfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1, 5) :
         roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
         pol = lag.lagfromroots(roots)
         res = lag.lagval(roots, pol)
         tgt = 0
         assert_(len(pol) == i + 1)
         assert_almost_equal(lag.lag2poly(pol)[-1], 1)
         assert_almost_equal(res, tgt)
Esempio n. 15
0
 def test_lagfromroots(self):
     res = lag.lagfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1, 5):
         roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
         pol = lag.lagfromroots(roots)
         res = lag.lagval(roots, pol)
         tgt = 0
         assert_(len(pol) == i + 1)
         assert_almost_equal(lag.lag2poly(pol)[-1], 1)
         assert_almost_equal(res, tgt)
Esempio n. 16
0
def getAbscissasAndWeights(N=5):
    # Laguerre polynomial roots and weights for Laguerre-Guass quadrature
    coef = np.concatenate([np.zeros(N), [1]])
    roots = laguerre.lagroots(coef)
    weights = []
    for n, root in enumerate(roots):
        n = n + 1
        array = np.concatenate([np.zeros(N + 1), [1]])
        value = laguerre.lagval(root, array)
        weight = root / ((N + 1) * value)**2
        weights.append(weight)

    return roots, weights
Esempio n. 17
0
    def test_lagval(self) :
        def f(x) :
            return x*(x**2 - 1)

        #check empty input
        assert_equal(lag.lagval([], [1]).size, 0)

        #check normal input)
        for i in range(7) :
            msg = "At i=%d" % i
            ser = np.zeros
            tgt = self.y[i]
            res = lag.lagval(self.x, [0]*i + [1])
            assert_almost_equal(res, tgt, err_msg=msg)

        #check that shape is preserved
        for i in range(3) :
            dims = [2]*i
            x = np.zeros(dims)
            assert_equal(lag.lagval(x, [1]).shape, dims)
            assert_equal(lag.lagval(x, [1,0]).shape, dims)
            assert_equal(lag.lagval(x, [1,0,0]).shape, dims)
Esempio n. 18
0
    def test_lagval(self) :
        def f(x) :
            return x*(x**2 - 1)

        #check empty input
        assert_equal(lag.lagval([], [1]).size, 0)

        #check normal input)
        for i in range(7) :
            msg = "At i=%d" % i
            ser = np.zeros
            tgt = self.y[i]
            res = lag.lagval(self.x, [0]*i + [1])
            assert_almost_equal(res, tgt, err_msg=msg)

        #check that shape is preserved
        for i in range(3) :
            dims = [2]*i
            x = np.zeros(dims)
            assert_equal(lag.lagval(x, [1]).shape, dims)
            assert_equal(lag.lagval(x, [1,0]).shape, dims)
            assert_equal(lag.lagval(x, [1,0,0]).shape, dims)
Esempio n. 19
0
    def test_lagfit(self) :
        def f(x) :
            return x*(x - 1)*(x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1],    [1],     -1)
        assert_raises(TypeError,  lag.lagfit, [[1]],  [1],      0)
        assert_raises(TypeError,  lag.lagfit, [],     [1],      0)
        assert_raises(TypeError,  lag.lagfit, [1],    [[[1]]],  0)
        assert_raises(TypeError,  lag.lagfit, [1, 2], [1],      0)
        assert_raises(TypeError,  lag.lagfit, [1],    [1, 2],   0)
        assert_raises(TypeError,  lag.lagfit, [1],    [1],   0, w=[[1]])
        assert_raises(TypeError,  lag.lagfit, [1],    [1],   0, w=[1,1])

        # Test fit
        x = np.linspace(0,2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y,y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3,coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw,yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
Esempio n. 20
0
    def test_lagfit(self) :
        def f(x) :
            return x*(x - 1)*(x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1],    [1],     -1)
        assert_raises(TypeError,  lag.lagfit, [[1]],  [1],      0)
        assert_raises(TypeError,  lag.lagfit, [],     [1],      0)
        assert_raises(TypeError,  lag.lagfit, [1],    [[[1]]],  0)
        assert_raises(TypeError,  lag.lagfit, [1, 2], [1],      0)
        assert_raises(TypeError,  lag.lagfit, [1],    [1, 2],   0)
        assert_raises(TypeError,  lag.lagfit, [1],    [1],   0, w=[[1]])
        assert_raises(TypeError,  lag.lagfit, [1],    [1],   0, w=[1,1])

        # Test fit
        x = np.linspace(0,2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y,y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3,coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw,yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
Esempio n. 21
0
def mainPricing(I, M, df):
    S = GenS(I, M)  # generate  stock  price  paths
    h = IV(S)  # inner  value  matrix
    V = IV(S)  # value  matrix
    for t in range(M - 1, -1, -1):
        # rg = polyfit(S[t,:], V[t+1,:]*df, reg)           # regression  at time t
        rg = a.lagfit(S[t, :], V[t + 1, :] * df, reg)
        C = a.lagval(S[t, :], rg, True)
        # C = polyval(rg, S[t, :])
        ##C = polyval(rg,S[t,:])                            # continuation  values
        V[t, :] = where(h[t, :] > C, h[t, :],
                        V[t + 1, :] * df)  # exercise  decision
    V0 = sum(V[0, :]) / I  # LSM  estimator
    return V0
Esempio n. 22
0
def GaussLaguerre(N):
    """
    function to return roots and weights for Gauss-Laguerre integration
    with N mesh points
    no multiplication for weights for Laguerre!
    """
    #initialize coeffitents for Laguerre series
    coeff = np.zeros(N+1)
    #find root of N-th Laguerre polynomial
    coeff[N] = 1
    roots = laguerre.lagroots(coeff)
    #reset coeff.; initalize L matrix
    L = np.zeros((N,N), dtype = np.float64)
    coeff = np.zeros(N)
    for i in range(N):
        #fill j-th Laguerre poly with the i-th root.
        for j in range(N):
            coeff[j] = 1
            L[i, j] = laguerre.lagval(roots[i], coeff)
            coeff[j] = 0
    L_inv = inv(L)
    return   L_inv[0,:], roots
Esempio n. 23
0
def plot_fitted_call_prices(is_call: bool, strike: float, expiry: float,
                            r: float, sigma: float) -> None:
    spot_prices = np.linspace(strike * 0.5, strike * 1.5, 1001)
    option_prices = [
        EuropeanBSPricing(is_call, s, strike, expiry, r,
                          sigma).get_option_price() for s in spot_prices
    ]

    def fit_func(x: np.ndarray, a: float, b: float, c: float) -> np.ndarray:
        return a * np.exp(b * x + c)

    def jac_func(x: np.ndarray, a: float, b: float, c: float) -> np.ndarray:
        t = np.exp(b * x + c)
        da = t
        db = a * t * x
        dc = a * t
        return np.transpose([da, db, dc])

    fp = curve_fit(f=fit_func,
                   xdata=spot_prices,
                   ydata=option_prices,
                   jac=jac_func)[0]
    pred1_option_prices = fit_func(spot_prices, fp[0], fp[1], fp[2])

    num_laguerre = 10
    ident = np.eye(num_laguerre)
    spot_features = np.array([[1.] + [
        np.exp(-s / (strike * 2)) * lagval(s / strike, ident[i])
        for i in range(num_laguerre)
    ] for s in spot_prices])
    lp = np.linalg.lstsq(spot_features, np.array(option_prices), rcond=None)[0]
    pred2_option_prices = spot_features.dot(lp)

    plt.plot(spot_prices, option_prices, 'r')
    plt.plot(spot_prices, pred1_option_prices, 'b')
    plt.plot(spot_prices, pred2_option_prices, 'g')
    plt.show()
Esempio n. 24
0
    def test_lagint(self) :
        # check exceptions
        assert_raises(ValueError, lag.lagint, [0], .5)
        assert_raises(ValueError, lag.lagint, [0], -1)
        assert_raises(ValueError, lag.lagint, [0], 1, [0, 0])

        # test integration of zero polynomial
        for i in range(2, 5):
            k = [0]*(i - 2) + [1]
            res = lag.lagint([0], m=i, k=k)
            assert_almost_equal(res, [1, -1])

        # check single integration with integration constant
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [1/scl]
            lagpol = lag.poly2lag(pol)
            lagint = lag.lagint(lagpol, m=1, k=[i])
            res = lag.lag2poly(lagint)
            assert_almost_equal(trim(res), trim(tgt))

        # check single integration with integration constant and lbnd
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            lagpol = lag.poly2lag(pol)
            lagint = lag.lagint(lagpol, m=1, k=[i], lbnd=-1)
            assert_almost_equal(lag.lagval(-1, lagint), i)

        # check single integration with integration constant and scaling
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [2/scl]
            lagpol = lag.poly2lag(pol)
            lagint = lag.lagint(lagpol, m=1, k=[i], scl=2)
            res = lag.lag2poly(lagint)
            assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with default k
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = lag.lagint(tgt, m=1)
                res = lag.lagint(pol, m=j)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with defined k
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = lag.lagint(tgt, m=1, k=[k])
                res = lag.lagint(pol, m=j, k=list(range(j)))
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with lbnd
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1)
                res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with scaling
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = lag.lagint(tgt, m=1, k=[k], scl=2)
                res = lag.lagint(pol, m=j, k=list(range(j)), scl=2)
                assert_almost_equal(trim(res), trim(tgt))
Esempio n. 25
0
 def laguerre_feature_func(x: float, i: int) -> float:
     xp = x / strike
     return np.exp(-xp / 2) * lagval(xp, eye[i])
Esempio n. 26
0
 def laguerre_state_ff(x: Tuple[NonTerminal[S], A], i=i) -> float:
     return lagval(float(x[0].state), states_ident[i])
Esempio n. 27
0
 def laguerre_action_ff(x: Tuple[NonTerminal[S], A], j=j) -> float:
     return lagval(float(x[1]), actions_ident[j])
Esempio n. 28
0
        cont += 1
        r = r * x + polinomio[i]
    print("El numero de multiplicaciones con horner es  {}".format(cont))
    print(r)
    return r


def hornerd2(polinomio, x):
    r = 0
    cont = 0
    polinomio = Derivada(polinomio)
    polinomio = Derivada(polinomio)
    for i in range(len(polinomio)):
        # multiplica el valor de la x
        # luego suma el coeficinte
        cont += 1
        r = r * x + polinomio[i]

    print("El numero de multiplicaciones con horner es  {}".format(cont))
    print(r)
    return r


if __name__ == "__main__":
    polinomio = [1, -5, -9, 155, -250]
    horner(polinomio, complex(1))
    hornerd1(polinomio, complex(1))
    hornerd2(polinomio, complex(1))
    p = np.array(polinomio)
    r = lagval(x=1, c=p)  #no tiene sentido lo que da
    print(r)
def apply_fitting_fields(plotgui):
    """
    Apply the set fitting parameters from the window.

    This routine reads the fitting parameters from the window and then
    applies the fitting.  Each time it is called a new data set should
    be generated, unless the fit order is too large for the number of
    points.  The fit order must be at most 1 less than the number
    of points, otherwise the routine just shows an error message
    pop-up and returns.

    Parameters
    ----------

        plotgui:   by assumption a matplotlib_user_interface object

    Returns
    -------

       None

    """
    fit_type = plotgui.set_fitting_fields[0].get()
    if 'Cubic Spline' in fit_type:
        fit_order = float(plotgui.set_fitting_fields[1].get())
    else:
        try:
            fit_order = int(plotgui.set_fitting_fields[1].get())
        except ValueError:
            str1 = 'Error: bad fit order (%s).  Settng to 4.' % (
                plotgui.set_fitting_fields[1].get())
            plotgui.fit_text.insert(Tk.END, str1)
            plotgui.fit_text.see(Tk.END)
            fit_order = 4
    set_number = plotgui.set_fitting_list_area.current()
    fit_flag = plotgui.fit_option.get()
    if fit_flag == 0:
        xvalues = numpy.copy(plotgui.xdata[set_number]['values'])
        yvalues = numpy.copy(plotgui.ydata[set_number]['values'])
    else:
        xvalues = numpy.copy(plotgui.ydata[set_number]['values'])
        yvalues = numpy.copy(plotgui.xdata[set_number]['values'])
    inds = numpy.argsort(xvalues)
    xvalues = xvalues[inds]
    yvalues = yvalues[inds]
    npoints = len(xvalues)
    if ('Spline' not in fit_type) and ('Internal' not in fit_type):
        if npoints + 1 <= fit_order:
            tkinter.messagebox.showinfo(
                'Error', 'The number of points is too few for the fit order.' +
                '  Please check your inputs.')
            return
    xmin = numpy.min(xvalues)
    xmax = numpy.max(xvalues)
    delx = xmax - xmin
    xstep = 1.2 * delx / 1201.
    xout = numpy.arange(xmin - delx / 10., xmax + delx / 10., xstep)
    if 'Internal' in fit_type:
        if npoints < 2:
            tkinter.messagebox.showinfo(
                'Error', 'The number of points is too few for a linear fit.' +
                '  Please check your inputs.')
            return
        if fit_flag == 0:
            yerrors = (plotgui.ydata[set_number]['lowerror'] +
                       plotgui.ydata[set_number]['higherror']) / 2.
        else:
            yerrors = (plotgui.xdata[set_number]['lowerror'] +
                       plotgui.xdata[set_number]['higherror']) / 2.
        if (numpy.min(yerrors) == 0.) and (numpy.max(yerrors) == 0.):
            yerrors = yerrors + 1.
        slope, intercept, slope_error, intercept_error, covariance, \
            correlation = general_utilities.slope_calculation(
                xvalues, yvalues, yerrors)
        if slope is None:
            tkinter.messagebox.showinfo('Error',
                                        'Error in the standard slope fit.\n')
            return
        yfit = intercept + xvalues * slope
        yout = intercept + xout * slope
        errorterm1 = xout * 0. + intercept_error
        errorterm2 = xout * slope_error
        youterror = numpy.sqrt(errorterm1 * errorterm1 +
                               errorterm2 * errorterm2)
        labelstring = 'Standard linear fit'
        rms = numpy.sqrt(numpy.mean((yvalues - yfit) * (yvalues - yfit)))
        str1 = 'Regression calculation results:\n'
        str1 = str1 + 'Slope: %g +/- %g\n' % (slope, slope_error)
        str1 = str1 + 'Intercept: %g +/- %g\n' % (intercept, intercept_error)
        str1 = str1 + 'Covariance: %f\n' % (covariance)
        str1 = str1 + 'Correlation: %f\n' % (correlation)
        str1 = str1 + 'RMS deviation: %f\n' % (rms)
        tkinter.messagebox.showinfo('Information', str1)
        outfile = open('fit_values.txt', 'a')
        print(str1, file=outfile)
        print(' ', file=outfile)
        outfile.close()
    if fit_type == 'Polynomial':
        fitpars = polynomial.polyfit(xvalues, yvalues, fit_order)
        yout = polynomial.polyval(xout, fitpars)
        yfit = polynomial.polyval(xvalues, fitpars)
        labelstring = 'Order %d polynomial fit' % (fit_order)
        general_utilities.list_polynomial_fitpars(fit_type, fit_order, fitpars)
    if fit_type == 'Legendre':
        fitpars = legendre.legfit(xvalues, yvalues, fit_order)
        yout = legendre.legval(xout, fitpars)
        yfit = legendre.legval(xvalues, fitpars)
        labelstring = 'Order %d Legendre polynomial fit' % (fit_order)
        general_utilities.list_polynomial_fitpars(fit_type, fit_order, fitpars)
    if fit_type == 'Laguerre':
        fitpars = laguerre.lagfit(xvalues, yvalues, fit_order)
        yout = laguerre.lagval(xout, fitpars)
        yfit = laguerre.lagval(xvalues, fitpars)
        labelstring = 'Order %d Laguerre polynomial fit' % (fit_order)
        general_utilities.list_polynomial_fitpars(fit_type, fit_order, fitpars)
    if fit_type == 'Chebyshev':
        fitpars = chebyshev.chebfit(xvalues, yvalues, fit_order)
        yout = chebyshev.chebval(xout, fitpars)
        yfit = chebyshev.chebval(xvalues, fitpars)
        labelstring = 'Order %d Chebyshev polynomial fit' % (fit_order)
        general_utilities.list_polynomial_fitpars(fit_type, fit_order, fitpars)
    if fit_type == 'Least-Squares Spline':
        if fit_flag == 0:
            yerrors = (plotgui.ydata[set_number]['lowerror'] +
                       plotgui.ydata[set_number]['higherror']) / 2.
        else:
            yerrors = (plotgui.xdata[set_number]['lowerror'] +
                       plotgui.xdata[set_number]['higherror']) / 2.
        if (numpy.min(yerrors) == 0.) and (numpy.max(yerrors) == 0.):
            yerrors = yerrors + 1.
        xmin1 = numpy.min(xvalues)
        xmax1 = numpy.max(xvalues)
        xrange = xmax1 - xmin1
        nknots = int(fit_order)
        if (nknots < 3) or (nknots > int(len(xvalues) / 2)):
            nknots = 3
        xstep = xrange / (nknots - 2)
        xknots = numpy.arange(
            numpy.min(xvalues) + xstep,
            numpy.max(xvalues) * 0.999999999, xstep)
        k = 3
        # Use cubic splines
        knotedges = numpy.r_[(xmin, ) * (k + 1), xknots, (xmax, ) * (k + 1)]
        weights = 1. / yerrors
        weights[yerrors == 0.] = 0.
        fitobject = make_lsq_spline(xvalues, yvalues, knotedges, k, w=weights)
        yout = fitobject(xout)
        yfit = fitobject(xvalues)
        labelstring = 'Least squares spline fit, sections = %d' % (nknots)
    if fit_type == 'Spline':
        fitpars = UnivariateSpline(xvalues,
                                   yvalues,
                                   k=1,
                                   s=None,
                                   bbox=[xmin - delx, xmax + delx])
        labelstring = 'Default spline fit'
        yout = fitpars(xout)
        yfit = fitpars(xvalues)
    if fit_type == 'Cubic Spline':
        if fit_order < 0.:
            str1 = 'Error: smoothing value %f (< 0) is not allowed.'\
                   + '  Settng to 0.0' % (fit_order)
            plotgui.fit_text.insert(Tk.END, str1)
            plotgui.fit_text.see(Tk.END)
            fit_order = 0.0
        fitpars = UnivariateSpline(xvalues,
                                   yvalues,
                                   k=3,
                                   bbox=[xmin - delx, xmax + delx],
                                   s=fit_order)
        yout = fitpars(xout)
        yfit = fitpars(xvalues)
        labelstring = 'Cubic spline fit, smoothing = %f' % (fit_order)
    rms = fit_statistics(yvalues, yfit)
    if 'Internal' in fit_type:
        if fit_flag == 0:
            xlowerror = youterror * 0.
            xhigherror = youterror * 0.
            ylowerror = youterror
            yhigherror = youterror
        else:
            xlowerror = youterror
            xhigherror = youterror
            ylowerror = youterror * 0.
            yhigherror = youterror * 0.
    else:
        xlowerror = xout * 0.
        xhigherror = xout * 0.
        ylowerror = yout * 0.
        yhigherror = yout * 0.
    xmin = numpy.min(xout)
    xmax = numpy.max(xout)
    ymin = numpy.min(yfit)
    ymax = numpy.max(yfit)
    if rms is not None:
        str1 = 'Fit: RMS = %g for %d points\n' % (rms, len(yfit))
        plotgui.fit_text.insert(Tk.END, str1)
        plotgui.fit_text.see(Tk.END)
    if fit_flag == 0:
        plotgui.xdata[plotgui.nsets] = {
            'values': xout,
            'lowerror': xlowerror,
            'higherror': xhigherror,
            'minimum': xmin,
            'maximum': xmax,
            'errors': False,
            'legend': True
        }
        plotgui.ydata[plotgui.nsets] = {
            'values': yout,
            'lowerror': ylowerror,
            'higherror': yhigherror,
            'minimum': ymin,
            'maximum': ymax,
            'errors': True,
            'legend': True
        }
    else:
        plotgui.xdata[plotgui.nsets] = {
            'values': yout,
            'lowerror': ylowerror,
            'higherror': yhigherror,
            'minimum': ymin,
            'maximum': ymax,
            'errors': False,
            'legend': True
        }
        plotgui.ydata[plotgui.nsets] = {
            'values': xout,
            'lowerror': xlowerror,
            'higherror': xhigherror,
            'minimum': xmin,
            'maximum': xmax,
            'errors': False,
            'legend': True
        }
    m = plotgui.nsets % 10
    n = int(math.floor(plotgui.nsets / 10))
    plotgui.set_properties[plotgui.nsets]['symbol'] = None
    plotgui.set_properties[plotgui.nsets]['linestyle'] = '-'
    plotgui.set_properties[plotgui.nsets]['colour'] = plotgui.colourset[m]
    plotgui.set_properties[plotgui.nsets]['symbolsize'] = 4.0 + 0.3 * n
    plotgui.set_properties[plotgui.nsets]['label'] = labelstring
    plotgui.nsets = plotgui.nsets + 1
    make_plot.make_plot(plotgui)
Esempio n. 30
0
    opt_ex_bi: OptimalExerciseBI = OptimalExerciseBI(
        spot_price=spot_price_val,
        payoff=lambda x: max(strike - x, 0.),
        expiry=expiry_val,
        rate=rate_val,
        vol=vol_val,
        num_steps=num_steps_val,
        spot_price_frac=spot_price_frac_val)

    num_laguerre: int = 4
    reglr_coeff: float = 0.001

    ident: np.ndarray = np.eye(num_laguerre)
    ffs: List[Callable[[NonTerminal[float]], float]] = [lambda _: 1.]
    ffs += [(lambda s: np.log(1 + np.exp(-s.state / (2 * strike))) * lagval(
        s.state / strike, ident[i])) for i in range(num_laguerre)]
    it_vf = opt_ex_bi.backward_induction_vf_and_pi(features=ffs,
                                                   reg_coeff=reglr_coeff)

    prices: np.ndarray = np.arange(120.0)

    print("Backward Induction: VF And Policy")
    print("---------------------------------")
    print()

    all_funcs: List[FunctionApprox[NonTerminal[float]]] = []
    for t, (v, p) in enumerate(it_vf):
        print(f"Time {t:d}")
        print()

        if t == 0 or t == int(num_steps_val / 2) or t == num_steps_val - 1:
Esempio n. 31
0
    def test_lagfit(self):
        def f(x):
            return x * (x - 1) * (x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1], [1], -1)
        assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
        assert_raises(TypeError, lag.lagfit, [], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
        assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])

        # Test fit
        x = np.linspace(0, 2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)

        #test NA
        y = f(x)
        y[10] = 100

        xm = x.view(maskna=1)
        xm[10] = np.NA
        res = lag.lagfit(xm, y, 3)
        assert_almost_equal(res, coef3)

        ym = y.view(maskna=1)
        ym[10] = np.NA
        res = lag.lagfit(x, ym, 3)
        assert_almost_equal(res, coef3)

        y2 = np.vstack((y, y)).T
        y2[10, 0] = 100
        y2[15, 1] = 100
        y2m = y2.view(maskna=1)
        y2m[10, 0] = np.NA
        y2m[15, 1] = np.NA
        res = lag.lagfit(x, y2m, 3).T
        assert_almost_equal(res[0], coef3)
        assert_almost_equal(res[1], coef3)

        wm = np.ones_like(x, maskna=1)
        wm[10] = np.NA
        res = lag.lagfit(x, y, 3, w=wm)
        assert_almost_equal(res, coef3)
for m in range(1, 10):
    # Simulation Parameters
    I = 25000
    M = 50
    dt=T/M
    df = exp(-r * dt)

    # Stock Price Paths
    S = S0 * np.exp(np.cumsum((r - 0.5 * sigma ** 2) * dt
        + sigma * math.sqrt(dt) * np.random.standard_normal((M + 1, I)), axis=0))
    S[0] = S0
    # Inner Values
    h = np.maximum(K - S, 0)
    # Present Value Vector (Initialization)
    V = h[-1]
    
    # American Option Valuation by Backwards Induction
    for t in xrange(M - 1, 0, -1):
        rg= laguerre.lagfit(S[t], V * df, m)
        C = laguerre.lagval(S[t], rg)# continuation values V = np.where(h[t] > C, h[t], V * df)

    # exercise decision
    V0 = df*np.sum(V)/I #LSMestimator
    error.append(abs(V0 - Vbsm))

print error

plt.xlabel('Order of Polynomial for LS regression')
plt.ylabel('Errors')
plt.plot(range(1, 10), error)
plt.show()
Esempio n. 33
0
def fitted_dql_put_option(
        expiry: float, num_steps: int, num_paths: int, spot_price: float,
        spot_price_frac: float, rate: float, vol: float, strike: float,
        training_iters: int) -> DNNApprox[Tuple[float, float]]:

    reg_coeff: float = 1e-2
    neurons: Sequence[int] = [6]

    #     features: List[Callable[[Tuple[float, float]], float]] = [
    #         lambda t_s: 1.,
    #         lambda t_s: t_s[0] / expiry,
    #         lambda t_s: t_s[1] / strike,
    #         lambda t_s: t_s[0] * t_s[1] / (expiry * strike)
    #     ]

    num_laguerre: int = 2
    ident: np.ndarray = np.eye(num_laguerre)
    features: List[Callable[[Tuple[float, float]], float]] = [lambda _: 1.]
    features += [(lambda t_s, i=i: np.exp(-t_s[1] / (2 * strike)) * lagval(
        t_s[1] / strike, ident[i])) for i in range(num_laguerre)]
    features += [
        lambda t_s: np.cos(-t_s[0] * np.pi / (2 * expiry)),
        lambda t_s: np.log(expiry - t_s[0])
        if t_s[0] != expiry else 0., lambda t_s: (t_s[0] / expiry)**2
    ]

    ds: DNNSpec = DNNSpec(neurons=neurons,
                          bias=True,
                          hidden_activation=lambda x: np.log(1 + np.exp(-x)),
                          hidden_activation_deriv=lambda y: np.exp(-y) - 1,
                          output_activation=lambda x: x,
                          output_activation_deriv=lambda y: np.ones_like(y))

    fa: DNNApprox[Tuple[float, float]] = DNNApprox.create(
        feature_functions=features,
        dnn_spec=ds,
        adam_gradient=AdamGradient(learning_rate=0.1, decay1=0.9,
                                   decay2=0.999),
        regularization_coeff=reg_coeff)

    dt: float = expiry / num_steps
    gamma: float = np.exp(-rate * dt)
    training_data: Sequence[TrainingDataType] = training_sim_data(
        expiry=expiry,
        num_steps=num_steps,
        num_paths=num_paths,
        spot_price=spot_price,
        spot_price_frac=spot_price_frac,
        rate=rate,
        vol=vol)
    for _ in range(training_iters):
        t_ind, s, s1 = training_data[randrange(len(training_data))]
        t = t_ind * dt
        x_val: Tuple[float, float] = (t, s)
        val: float = max(strike - s1, 0)
        if t_ind < num_steps - 1:
            val = max(val, fa.evaluate([(t + dt, s1)])[0])
        y_val: float = gamma * val
        fa = fa.update([(x_val, y_val)])
        # for w in fa.weights:
        #     pprint(w.weights)
    return fa
Esempio n. 34
0
    opt_ex_bi: OptimalExerciseBI = OptimalExerciseBI(
        spot_price=spot_price_val,
        payoff=lambda x: max(strike - x, 0.),
        expiry=expiry_val,
        rate=rate_val,
        vol=vol_val,
        num_steps=num_steps_val,
        spot_price_frac=spot_price_frac_val)

    num_laguerre: int = 4
    reglr_coeff: float = 0.001

    ident: np.ndarray = np.eye(num_laguerre)
    ffs: List[Callable[[StateType], float]] = [lambda _: 1.]
    ffs += [(lambda s_e: np.log(1 + np.exp(-s_e[0] / (2 * strike))) * lagval(
        s_e[0] / strike, ident[i])) for i in range(num_laguerre)]
    it_vf = opt_ex_bi.backward_induction_vf_and_pi(features=ffs,
                                                   reg_coeff=reglr_coeff)

    prices: np.ndarray = np.arange(120.0)

    print("Backward Induction: VF And Policy")
    print("---------------------------------")
    print()

    all_funcs: List[FunctionApprox[StateType]] = []
    for t, (v, p) in enumerate(it_vf):
        print(f"Time {t:d}")
        print()

        if t == 0 or t == int(num_steps_val / 2) or t == num_steps_val - 1:
Esempio n. 35
0
 def laguerre_feature_function(state: State, num_feature: int) -> float:
     xp = state.price / strike
     return np.exp(-xp / 2) * lagval(xp, eye[num_feature])
Esempio n. 36
0
 def laguerre_ff(x: NonTerminal[S], i=i) -> float:
     return lagval(float(x.state), ident[i])
Esempio n. 37
0
 def laguerre_func(x: float, i=i) -> float:
     return lagval(x, ident[i])
Esempio n. 38
0
    def test_lagint(self):
        # check exceptions
        assert_raises(ValueError, lag.lagint, [0], .5)
        assert_raises(ValueError, lag.lagint, [0], -1)
        assert_raises(ValueError, lag.lagint, [0], 1, [0, 0])

        # test integration of zero polynomial
        for i in range(2, 5):
            k = [0] * (i - 2) + [1]
            res = lag.lagint([0], m=i, k=k)
            assert_almost_equal(res, [1, -1])

        # check single integration with integration constant
        for i in range(5):
            scl = i + 1
            pol = [0] * i + [1]
            tgt = [i] + [0] * i + [1 / scl]
            lagpol = lag.poly2lag(pol)
            lagint = lag.lagint(lagpol, m=1, k=[i])
            res = lag.lag2poly(lagint)
            assert_almost_equal(trim(res), trim(tgt))

        # check single integration with integration constant and lbnd
        for i in range(5):
            scl = i + 1
            pol = [0] * i + [1]
            lagpol = lag.poly2lag(pol)
            lagint = lag.lagint(lagpol, m=1, k=[i], lbnd=-1)
            assert_almost_equal(lag.lagval(-1, lagint), i)

        # check single integration with integration constant and scaling
        for i in range(5):
            scl = i + 1
            pol = [0] * i + [1]
            tgt = [i] + [0] * i + [2 / scl]
            lagpol = lag.poly2lag(pol)
            lagint = lag.lagint(lagpol, m=1, k=[i], scl=2)
            res = lag.lag2poly(lagint)
            assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with default k
        for i in range(5):
            for j in range(2, 5):
                pol = [0] * i + [1]
                tgt = pol[:]
                for k in range(j):
                    tgt = lag.lagint(tgt, m=1)
                res = lag.lagint(pol, m=j)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with defined k
        for i in range(5):
            for j in range(2, 5):
                pol = [0] * i + [1]
                tgt = pol[:]
                for k in range(j):
                    tgt = lag.lagint(tgt, m=1, k=[k])
                res = lag.lagint(pol, m=j, k=list(range(j)))
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with lbnd
        for i in range(5):
            for j in range(2, 5):
                pol = [0] * i + [1]
                tgt = pol[:]
                for k in range(j):
                    tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1)
                res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with scaling
        for i in range(5):
            for j in range(2, 5):
                pol = [0] * i + [1]
                tgt = pol[:]
                for k in range(j):
                    tgt = lag.lagint(tgt, m=1, k=[k], scl=2)
                res = lag.lagint(pol, m=j, k=list(range(j)), scl=2)
                assert_almost_equal(trim(res), trim(tgt))
Esempio n. 39
0
    def test_lagfit(self) :
        def f(x) :
            return x*(x - 1)*(x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1],    [1],     -1)
        assert_raises(TypeError,  lag.lagfit, [[1]],  [1],      0)
        assert_raises(TypeError,  lag.lagfit, [],     [1],      0)
        assert_raises(TypeError,  lag.lagfit, [1],    [[[1]]],  0)
        assert_raises(TypeError,  lag.lagfit, [1, 2], [1],      0)
        assert_raises(TypeError,  lag.lagfit, [1],    [1, 2],   0)
        assert_raises(TypeError,  lag.lagfit, [1],    [1],   0, w=[[1]])
        assert_raises(TypeError,  lag.lagfit, [1],    [1],   0, w=[1,1])

        # Test fit
        x = np.linspace(0,2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y,y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3,coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw,yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)

        #test NA
        y = f(x)
        y[10] = 100

        xm = x.view(maskna=1)
        xm[10] = np.NA
        res = lag.lagfit(xm, y, 3)
        assert_almost_equal(res, coef3)

        ym = y.view(maskna=1)
        ym[10] = np.NA
        res = lag.lagfit(x, ym, 3)
        assert_almost_equal(res, coef3)

        y2 = np.vstack((y,y)).T
        y2[10,0] = 100
        y2[15,1] = 100
        y2m = y2.view(maskna=1)
        y2m[10,0] = np.NA
        y2m[15,1] = np.NA
        res = lag.lagfit(x, y2m, 3).T
        assert_almost_equal(res[0], coef3)
        assert_almost_equal(res[1], coef3)

        wm = np.ones_like(x, maskna=1)
        wm[10] = np.NA
        res = lag.lagfit(x, y, 3, w=wm)
        assert_almost_equal(res, coef3)
Esempio n. 40
0
 def eval(self, x, u, output_array=None):
     x = np.atleast_1d(x)
     if output_array is None:
         output_array = np.zeros(x.shape)
     output_array[:] = lag.lagval(x, u)*np.exp(-x/2)
     return output_array