Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TST: Add test for annualized volatility factor
  • Loading branch information
Ana Ruelas committed Nov 21, 2016
1 parent b2f1091 commit 55e902a
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions tests/pipeline/test_technical.py
Expand Up @@ -17,7 +17,8 @@
LinearWeightedMovingAverage,
RateOfChangePercentage,
TrueRange,
MovingAverageConvergenceDivergence
MovingAverageConvergenceDivergence,
AnnualizedVolatility,
)
from zipline.testing import parameter_space
from zipline.testing.fixtures import ZiplineTestCase
Expand Down Expand Up @@ -407,7 +408,7 @@ def test_tr_basic(self):
assert_equal(out, np.full((3,), 2.))


class MovingAverageConvergenceDivergenceCase(ZiplineTestCase):
class MovingAverageConvergenceDivergenceTestCase(ZiplineTestCase):
def test_MACD_window_length_generation(self):
signal_period = random_integers(1, 90)
fast_period = random_integers(signal_period+1, signal_period+100)
Expand Down Expand Up @@ -480,3 +481,55 @@ def test_moving_average_convergence_divergence(self):
expected_hist,
decimal=8
)


class AnnualizedVolatilityTestCase(ZiplineTestCase):
"""
Test Annualized Volatility
"""
def test_simple_volatility(self):
"""
Simple test for uniform returns should generate 0 volatility
"""
nassets = 3
ann_vol = AnnualizedVolatility()
today = pd.Timestamp('2016', tz='utc')
assets = np.arange(nassets, dtype=np.float)
returns = np.full((ann_vol.window_length, nassets),
0.004,
dtype=np.float64)
out = np.empty(shape=(nassets,), dtype=np.float64)

ann_vol.compute(today, assets, out, returns, 252)

expected_vol = np.array([0] * nassets)
np.testing.assert_almost_equal(
out,
expected_vol,
decimal=8
)

def test_volatility(self):
"""
Check volatility results against values calculated manually
"""
nassets = 3
ann_vol = AnnualizedVolatility()
today = pd.Timestamp('2016', tz='utc')
assets = np.arange(nassets, dtype=np.float)
returns = np.random.normal(loc=0.001,
scale=0.01,
size=(ann_vol.window_length, nassets))
out = np.empty(shape=(nassets,), dtype=np.float64)
ann_vol.compute(today, assets, out, returns, 252)

mean = returns.sum(axis=0) / returns.shape[0]
annualized_variance = ((returns - mean) ** 2).sum(axis=0) / \
returns.shape[0] * 252
expected_vol = np.sqrt(annualized_variance)

np.testing.assert_almost_equal(
out,
expected_vol,
decimal=8
)

0 comments on commit 55e902a

Please sign in to comment.