Skip to content

Commit

Permalink
Merge 3331ad8 into 7b0d8cc
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyclements committed Nov 26, 2017
2 parents 7b0d8cc + 3331ad8 commit 474e9c0
Show file tree
Hide file tree
Showing 39 changed files with 3,228 additions and 348 deletions.
38 changes: 18 additions & 20 deletions .travis.yml
@@ -1,40 +1,38 @@
language: python

python:
- "2.7"
- "3.4"
- "3.5"
env:
matrix:
- PYTHON=2.7 NUMPY=1.10 COVERAGE='false'
- PYTHON=2.7 NUMPY=1.11 COVERAGE='false'
- PYTHON=3.4 NUMPY=1.11 COVERAGE='false'
- PYTHON=3.5 NUMPY=1.11 COVERAGE='false'
- PYTHON=3.6 NUMPY=1.13 COVERAGE='true'

install:
# Install conda
- sudo apt-get update
# We do this conditionally because it saves us some downloading if the
# version is the same.
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh;
else
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
fi
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
- bash miniconda.sh -b -p $HOME/miniconda
- export PATH="$HOME/miniconda/bin:$PATH"
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
- conda info -a

# Install dependencies
- conda create -n test-environment python=$TRAVIS_PYTHON_VERSION
- conda create -n test-environment python=$PYTHON
- source activate test-environment
- conda install --file requirements.txt
- conda install numpy=$NUMPY pandas scipy multipledispatch matplotlib pytest pytest-cov

# Install package
# (from dask) - pip install --no-deps -e .[complete]
# taken from danielfrg/word2vec
- python setup.py bdist_wheel
- ls dist
- pip install --pre --no-index --find-links dist/ pennies
# Install package in develop mode
- python setup.py develop
#- pip install --no-deps -e .

script:
- py.test --cov --cov-report=html -vv -s --pyarg --doctest-modules pennies
- if [[ $COVERAGE == 'true' ]]; then
py.test --cov --cov-report=html -vv -s tests;
else
py.test -vv -s tests;
fi

after_success:
- coverage report --show-missing
Expand Down
9 changes: 0 additions & 9 deletions environment.yml

This file was deleted.

10 changes: 4 additions & 6 deletions pennies/__init__.py
@@ -1,7 +1,5 @@
# from . import models
from multipledispatch import dispatch
from functools import partial

__version__ = '0.1.0'

from pennies.calculators.bonds import *
from pennies.calculators.core import *
from pennies.calculators.core import present_value
NAMESPACE = dict()
dispatch = partial(dispatch, namespace=NAMESPACE) # TODO Why is namespace here?
Empty file removed pennies/assets/__init__.py
Empty file.
40 changes: 0 additions & 40 deletions pennies/assets/bonds.py

This file was deleted.

28 changes: 0 additions & 28 deletions pennies/assets/core.py

This file was deleted.

Empty file removed pennies/assets/tests/__init__.py
Empty file.
20 changes: 0 additions & 20 deletions pennies/assets/tests/test_core.py

This file was deleted.

2 changes: 0 additions & 2 deletions pennies/calculators/__init__.py
@@ -1,2 +0,0 @@
from __future__ import absolute_import, division, print_function

134 changes: 134 additions & 0 deletions pennies/calculators/assets.py
@@ -1,5 +1,115 @@
from __future__ import absolute_import, division, print_function

from pennies import dispatch
from six import string_types

from pennies.trading.assets import Asset
from pennies.market.market import Market

BASE_ASSET = (Asset, object)
BASE_MARKET = (Market, object)
BASE_STRING = string_types + (object,)


@dispatch(BASE_ASSET, BASE_MARKET, BASE_STRING)
def present_value(contract, market, reporting_ccy):
"""Base present value calculation.
Given an asset (or sequence of assets), calculate it's present
value as of today. The supplied market provides prices, curves, and so on.
Parameters
----------
contract: Asset
Asset to calculate present value
market: Market
Market to retrieve raw and computed market values
such as prices, curves, and surfaces.
reporting_ccy: str
Specifies which currency to report value in
Returns
-------
float
Present Value in the reporting currency
"""
raise NotImplementedError("Not available for base types")


@dispatch(BASE_ASSET, BASE_MARKET, BASE_STRING, object, BASE_STRING)
def sens_to_zero_rates(contract, market, curve_ccy, curve_key, reporting_ccy):
"""Sensitivity of each cashflow to the curve specified by currency and key
Given an asset contract (or sequence of assets), calculate the change in
Present Value to a unit shift in the zero rate of the curve specified
by curve_ccy and curve_key.
Parameters
----------
contract: Asset
Calculate Sensitivity to this Asset.
market: Market
Market to retrieve raw and computed market values
such as prices, curves, and surfaces.
curve_ccy: str
Specifies currency of the curve
curve_key: str
Along with curve_ccy, specifies which curve to compute sensitivity to.
reporting_ccy: str
Specifies which currency to report value in
Returns
-------
DataFrame
Table containing maturities, sensitivities, curve currency and key,
columns=['ttm', 'sens', 'ccy', 'curve'].
"""
raise NotImplementedError("Not available for base types")


@dispatch(BASE_ASSET, BASE_MARKET, str)
def sens_to_market_rates(contract, market, reporting_ccy):
"""Compute sensitivity of contract to each node in the market's curves.
Market Curve Calibration consists of finding the discount rates for a
desired set of curves that correctly prices a target set of contracts.
To do this, we form a Jacobian matrix consisting of the sensitivities
of each contract's Present Value, V_i,
to each node of each curve in the market, r_j: d(V_i)/dr_j.
This function produces a single row of the Jacobian. Note the indices:
i = index over market contracts. (rows)
j = index over model curve nodes, for all curves. (columns)
Thus, if one had a discount curve with N nodes,
and a single LIBOR curve with M nodes. j = 0..N+M-1
V_i = Present Value of the i'th contract.
r_j = Discount rate of node j.
t_k = Maturity of some bond required to price V_i.
r_{c,k} = discount rate of curve, c, at time t_k.
z_{c,k} = discount bond price of curve, c, at time t_k.
dP/dr_j = ( dV/dz_{c,k} ) * ( dz_{c,k} / dr_{c,k} ) * ( dr_{c,k} / dr_j )
Note that dr_{c,k} / dr_j == 0 if node j does not belong to curve c.
Parameters
----------
contract: Asset
Asset to calculate present value
market: Market
Market to retrieve raw and computed market values
such as prices, curves, and surfaces.
Returns
-------
Array of CurrencyAmount's
Derivative of Present Value with respect to each node's rate
"""
raise NotImplementedError("Not available for base types")


class AssetCalculator(object):

def __init__(self, contract, market):
Expand All @@ -17,3 +127,27 @@ def __init__(self, contract, market):

def all_calculators():
return AssetCalculator.__subclasses__()


def default_calculators():
from pennies.trading import assets
from pennies.calculators import payments
return {
str(assets.BulletPayment): payments.BulletPaymentCalculator,
str(assets.DiscountBond): payments.BulletPaymentCalculator,
str(assets.SettlementPayment): payments.BulletPaymentCalculator,
str(assets.Zero): payments.BulletPaymentCalculator,
str(assets.ZeroCouponBond): payments.BulletPaymentCalculator,
str(assets.CompoundAsset): None,
str(assets.Annuity): None,
str(assets.FixedLeg): None,
str(assets.IborLeg): None,
str(assets.Swap): None,
str(assets.VanillaSwap): None,
str(assets.CurrencySwap): None,
str(assets.TenorSwap): None,
str(assets.Deposit): None,
str(assets.StirFuture): None,
str(assets.FRA): None,
str(assets.IborFixing): None}

0 comments on commit 474e9c0

Please sign in to comment.