class TestRegressionBasis(unittest.TestCase):
    '''Tests for stochastic process implementations.'''

    def setUp(self):
        mu = 0.123
        sigma = 0.456
        self.bm = BrownianMotion(mu=mu, sigma=sigma)
        self.gbm = GeometricBrownianMotion(mu=mu, sigma=sigma)
        self.rnd = RandomState(1234)

    def test_brownian_motion_distribution(self):
        '''Test terminal distribution of Brownian Motion.'''
        t = np.linspace(0, 20, 20)
        n = 200
        x = self.bm.simulate(t, n, self.rnd)
        self.assertEqual((t.size, n), x.shape)
        self.assertEqual(n, x[-1, :].size)
        terminal_dist = self.bm.distribution(t[-1])
        test_result = kstest(x[-1, :], terminal_dist.cdf)
        self.assertGreater(test_result.pvalue, 0.4)

    def test_geometric_brownian_motion_distribution(self):
        '''Test terminal distribution of Geometric Brownian Motion.'''
        t = np.linspace(0, 20, 20)
        n = 1000
        x = self.gbm.simulate(t, n, self.rnd)
        self.assertEqual((t.size, n), x.shape)
        self.assertEqual(n, x[-1, :].size)
        terminal_dist = self.gbm.distribution(t[-1])
        test_result = kstest(x[-1, :], terminal_dist.cdf)
        print(test_result)
        self.assertGreater(test_result.pvalue, 0.4)
Beispiel #2
0
    def test_readme_demo(self):
        '''Test usage demo including imports'''
        from longstaff_schwartz.algorithm import longstaff_schwartz
        from longstaff_schwartz.stochastic_process \
            import GeometricBrownianMotion
        import numpy as np

        # Model parameters
        t = np.linspace(0, 5, 100)  # timegrid for simulation
        r = 0.01  # riskless rate
        sigma = 0.15  # annual volatility of underlying
        n = 100  # number of simulated paths

        # Simulate the underlying
        gbm = GeometricBrownianMotion(mu=r, sigma=sigma)
        rnd = np.random.RandomState(1234)
        x = gbm.simulate(t, n, rnd)  # x.shape == (t.size, n)

        # Payoff (exercise) function
        strike = 0.95

        def put_payoff(spot):
            return np.maximum(strike - spot, 0.0)

        # Discount factor function
        def constant_rate_df(t_from, t_to):
            return np.exp(-r * (t_to - t_from))

        # Approximation of continuation value
        def fit_quadratic(x, y):
            return np.polynomial.Polynomial.fit(x, y, 2, rcond=None)

        # Selection of paths to consider for exercise
        # (and continuation value approxmation)
        def itm(payoff, spot):
            return payoff > 0

        # Run valuation of American put option
        npv_american = longstaff_schwartz(x, t, constant_rate_df,
                                          fit_quadratic, put_payoff, itm)

        # European put option for comparison
        npv_european = constant_rate_df(t[0], t[-1]) * put_payoff(x[-1]).mean()

        # Check results
        assert np.round(npv_american, 4) == 0.0734
        assert np.round(npv_european, 4) == 0.0626
        assert npv_american > npv_european
 def test_general_against_specific_algorithm(self):
     '''Choose parameters for general algorithm implementation
        such that they math the american put option specific code
     '''
     gbm = GeometricBrownianMotion(mu=r, sigma=0.1)
     rnd = RandomState(1234)
     t = np.linspace(0, 5, 20)
     n = 50
     x = gbm.simulate(t, n, rnd)
     general = longstaff_schwartz(x, t, constant_rate_df, fit_quadratic,
                                  american_put_payoff, itm)
     specific = longstaff_schwartz_american_option_quadratic(
         x, t, r, strike)
     self.assertAlmostEqual(general, specific)
     allpathregr = longstaff_schwartz(x, t, constant_rate_df, fit_quadratic,
                                      american_put_payoff)
     self.assertNotAlmostEqual(general, allpathregr)
Beispiel #4
0
def findY(size):
    Y = np.empty((size, 1))
    Z = np.empty((size, 1))
    global count10  #load model or not
    # Model parameters
    t = np.linspace(0, 1, 50)  # timegrid for simulation
    r = 0.06  # riskless rate
    sigma = 0.4  # annual volatility of underlying
    n = 10**5  # number of simulated paths
    # Payoff (exercise) function
    strike = 40
    spot = 36
    gbm = GeometricBrownianMotion(mu=r, sigma=sigma)
    rnd = np.random.RandomState(100)
    for i in range(size):
        # Simulate the underlying
        x = gbm.simulate(t, n, rnd)  # x.shape == (t.size, n)
        count10 = 0
        #MLPs I
        Y[i] = longstaff_schwartz(X=x * spot,
                                  t=t,
                                  df=constant_rate_df,
                                  fit=fit_neural,
                                  exercise_payoff=put_payoff,
                                  itm_select=itm)
        #LSM
        Z[i] = algorithm1.longstaff_schwartz(X=x * spot,
                                             t=t,
                                             df=constant_rate_df,
                                             fit=fit_quadratic,
                                             exercise_payoff=put_payoff,
                                             itm_select=itm)

        print("spot", spot, "vol", sigma, "MLPsI", Y[i])

    df = pd.DataFrame({'LSM': Z[:, 0], 'MLPs I': Y[:, 0]})
    return df
 def setUp(self):
     mu = 0.123
     sigma = 0.456
     self.bm = BrownianMotion(mu=mu, sigma=sigma)
     self.gbm = GeometricBrownianMotion(mu=mu, sigma=sigma)
     self.rnd = RandomState(1234)
Beispiel #6
0
from longstaff_schwartz.algorithm import longstaff_schwartz
from longstaff_schwartz.stochastic_process import GeometricBrownianMotion
import numpy as np
import datetime
start = datetime.datetime.now()

# Model parameters
t = np.linspace(0, 1, 100)  # timegrid for simulation
r = 0.06  # riskless rate
sigma = 0.2  # annual volatility of underlying
n = 10**5  # number of simulated paths

# Simulate the underlying
gbm = GeometricBrownianMotion(mu=r, sigma=sigma)
rnd = np.random.RandomState(1234)
x = 36 * gbm.simulate(t, n, rnd)  # x.shape == (t.size, n)

# Payoff (exercise) function
strike = 40

def put_payoff(spot):
        return np.maximum(strike - spot, 0.0)

# Discount factor function
def constant_rate_df(t_from, t_to):
        return np.exp(-r * (t_to - t_from))

def fit_quadratic(x, y):
        return np.polynomial.Polynomial.fit(x, y, 2, rcond=None)

# Selection of paths to consider for exercise