Commit Graph

29 Commits

Author SHA1 Message Date
Eddie Hebert 7cc24cec1f BUG: Fix numerous cumulative and period risk calculations.
The calculations that are expected to change are:
- cumulative.beta
- cumulative.alpha
- cumulative.information
- cumulative.sharpe
- period.sortino

* Explanation of how risk calculations are changing

** Risk Fixes for Both Period and Cumulative

*** Downside Risk

   Use sample instead of population for standard deviation.

   Add a rounding factor, so that if the two values are close for a given
   dt, that they do not count as a downside value, which would throw off
   the denominator of the standard deviation of the downside diffs.

*** Standard Deviation Type

    Across the board the standard deviation has been standardized to using
    a 'sample' calculation, whereas before cumulative risk was monstly using
    'population'. Using `ddof=1` with `np.std` calculates as if the values
    are a sample.

** Cumulative Risk Fixes

*** Beta

   Use the daily algorithm returns and benchmarks instead of annualized
   mean returns.

*** Volatility

   Use sample instead of population with standard deviation.

   The volatility is an input to other calculations so this change affects
   Sharpe and Information ratio calculations.

*** Information Ratio

   The benchmark returns input is changed from annualized benchmark returns
   to the annualized mean returns.

*** Alpha

   The benchmark returns input is changed from annualized benchmark returns
   to the annualized mean returns.

** Period Risk Fixes

*** Sortino

    Use the downside risk of the daily return vs. the mean algorithm returns
    for the minimum acceptable return instead of the treasury return.

    The above required adding the calculation of the mean algorithm returns
    for period risk.

    Also, use algorithm_period_returns and tresaury_period_return as the
    cumulative Sortino does, instead of using algorithm returns for both
    inputs into the Sortino calculation.

* Other Supporting Changes

** answer_key

   Add new mappings for downside risk and Sortino as well as
   re-address the index mappings because of changes to the answer key
   spread sheet.

** test_risk_cumulative

   Change the decimal precision to expect higher precision.
   The calculations are now more aligned with the answer key, so we can
   expect higher precision. In particular now that the standard deviation
   type matches everywhere in both the Python implementation and the answer
   sheet, the precision of the first value no longer has to be glossed over.

** test_events_through_risk

  Change the results which are used as a canary for risk changes,
  since we do expect Sharpe to change with this change..
2014-04-14 16:44:28 -04:00
Eddie Hebert 4860a966b3 REL: Update copyright year on all files changed since the new year. 2014-03-07 22:31:41 -05:00
Eddie Hebert 6cdd5ddb10 BUG: Fix max drawdown calculation.
The input into max drawdown was incorrect, causing the bad results.
i.e. the `compounded_log_returns` were not values representative of
the algorithms total return at a given time, though
`calculate_max_drawdown` was treating the values as if they were.
Instead, use the `algorithm_period_returns` series, which does provide
the total return.

Update risk answer key with an Excel calculation of max drawdown
to help corroborate the calculations.

Also, remove `compounded_log_returns`, (which actually had stopped
being the `compounded_log_returns` at some point), since the max
drawdown was the only calculation using the values in that series.
2014-02-27 17:16:35 -05:00
Eddie Hebert c53196b5ea BUG: Make write of binary file compatible with Python 3.
Open the destination file with the byte flag to avoid error in
Python 3 with mismatch of str and byte.
2014-01-07 18:01:25 -05:00
Eddie Hebert 68b78a6914 MAINT: Explicitly convert map to list when converting answer key values.
For compatibility with iterator version of map in Python 3.

Also always use iterator version of map,
so that code path is exercised in Python 2.
2014-01-07 11:43:38 -05:00
Eddie Hebert 98956f19ed MAINT: Make answer key reading compatible with Python 3.
Ensure reading of bytes and checking against byte type, when
parsing the Excel spreadsheet which contains the answers.
2014-01-07 11:42:12 -05:00
Eddie Hebert dad34d2ddb TST: Add annualized alpha and beta to answer key.
Add a column that uses annualized mean returns as the inputs into
alpha and beta.
2013-10-11 00:27:03 -04:00
Eddie Hebert dcae6af67b ENH: Annualize information ratio.
Use annualized values for information, so that it is calculated
using the same units as sharpe, etc.
2013-10-11 00:27:03 -04:00
Eddie Hebert 0ebdb2fe77 ENH: Annualize sortino ratio.
Use annualized values for sortino, so that it is calculated using the
same units as sharpe, etc.
2013-10-11 00:27:03 -04:00
Eddie Hebert 433f97c38f ENH: Improve headline Sharpe risk calculations.
This could perhaps be labelled BUG, as well.

Change the Sharpe (and algorithm volatiilty) value used to compare
algorithms/backtests so that it is annualized and uses daily returns.

Previously, the Sharpe metric was using the same calculation style
as the fixed size periods, i.e. 3 Month, 6 Month, etc., which can
use the geometric mean when comparing against the risk free.

Change the Sharpe calculation to use the arithmetic mean differenc
against the risk free rate, using daily (non-compounded) values.

Also, use annualized mean returns.
2013-10-10 18:37:53 -04:00
Eddie Hebert 08bc42dc0c TST: Fix index to cumulative risk answer key.
Indexes to risk answers were pointing to a previous version.

Also, provide the risk cumulative answers as a pd.Series,
so that it is easier to compare to values produced by risk class.
2013-09-24 21:36:57 -04:00
Eddie Hebert b94d10cfb6 TST: Add answer key indexes for cumulative risk metrics.
Add indexes for Sharpe, returns and other values needed for
reading answers for cumulative risk metrics.

Prepare for unit test and matching change of implementation.
2013-08-15 15:09:44 -04:00
Eddie Hebert f3fd9d598a TST: Add parser for date values from answer key.
Refactor the reading of values from the Excel spreadsheet so that
parsers are configurable by index.

Needed so that we can parse columns that have dates, in addition
to floats as previously.
2013-08-15 15:08:42 -04:00
Eddie Hebert 3732c105b8 TST: Improve answer key interface.
Instead of using the indexes defined in the answer key class
to index back into the answer key object, populate the answers
so that they are available as members of the answer key object.

Update period risk test to use new answer key structure.

Also, remove the rounding behavior from the answer sheet, leaving
the rounding to the consumer of the answer key values, so that
the values can be retrieved from the spreadsheet during answer
key __init__ without knowledge of the decimal point that the calling
code expects.
Correspondingly, change period risk tests to use
np.testing.assert_almost_equal when doing floating point comparison.
2013-08-14 22:41:52 -04:00
Eddie Hebert ead9fc953f TST: Begin annotation notebook for risk answer key.
Read tests.risk.answer_key module into an IPython notebook, to start
a base to which answer key values and explanations can be added and
display without access to Excel.

For now, the notebook just provides the latest download link for
the spreadsheet.
2013-08-14 12:36:47 -04:00
Eddie Hebert faee91876f MAINT: Add formatting for printing of answer key DataIndex.
Output format of the range compatible with Excel, so that it
can be copy and pasted back into the spread sheet for easier
corroboration.
2013-08-09 17:32:17 -04:00
Eddie Hebert c31bbee424 MAINT: Create separate page in risk answer key for period returns.
Copy the `Sim` sheet to `Sim Cumulative` as well as rename it to
`Sim Period`.

Update the answer key module accordingly.
2013-08-07 17:16:01 -04:00
Eddie Hebert 3c305bbe35 TST: Add script to automate upload of risk answer sheet to S3.
For maintainer use, requires AWS credentials for the account where
the `zipline-test-data` bucket is hosted.

Script does the following steps which used to be manual:
- Create a key name based on the md5 of the answer key file.
- Upload the answer key to S3 bucket.
- Make the file publically downloadable over HTTP.
2013-07-29 14:42:53 -04:00
Eddie Hebert f451efe483 TST: Read alpha values from answer key.
Corroborate alpha values with Excel answer key.
2013-07-23 12:15:24 -04:00
Eddie Hebert 7c01d39858 BUG: Fix beta calculation.
Use recent change to benchmark variance in the beta calculation,
instead of referring to the 4th quadrant of the covariance.

Also, read answers from answer key for corroboration of beta values.
2013-07-23 12:07:24 -04:00
Eddie Hebert 3164aa9016 BUG: Make covariance match values in answer key.
The np.cov call needs a ddof of 0 to match the answer key, which uses
Excel's VAR.

When switching np.cov to use a ddof of 0, the benchmark variance is
no longer the 4th quadrant of the cov result, so use np.var directly.
2013-07-23 11:30:48 -04:00
Eddie Hebert 2314e8a281 TST: Read benchmark variance from answer key.
Add reference to updated answer key with benchmark variance cells,
and use the new cells as the reference for the benchmark variance
test.

The values changed from the original hardcoded values, due to the
change to close over close benchmarks.
2013-07-19 20:28:12 -04:00
Eddie Hebert 56c7c840fa TST: Fix answer key index converter for values larger than 'AB'
Change logic so that it uses the 26 ^ n as the base for each letters
index value.
2013-07-19 20:26:30 -04:00
Eddie Hebert 8c006fc347 TST: Use xlsx format instead of xls.
The xls conversion was corrupting some equations, so switching to
use Excel produced xlsx.
2013-07-19 11:06:37 -04:00
Eddie Hebert 135b872a58 DOC: Fix typo in xslx format name. 2013-07-18 18:36:42 -04:00
Eddie Hebert 575e45ab4e TST: Read more risk expected values from the answer spreadsheet.
Convert test_risk from hardcoded values to reading algorithm
returns, volatility, and sharpe answers from the spreadsheet.
2013-07-18 18:09:49 -04:00
Eddie Hebert d58181db34 TST: Fix case where there is no existing risk answer key.
The download checksum was not being correctly set when no file existed.
Set the checksum to the latest value so that a download is triggered.
2013-07-18 14:47:33 -04:00
Eddie Hebert e5e38a437b TST: Download risk answer key from S3.
So that the answer key does not onerous on the SCM repo size, add a
utility to download the answer key automatically.

Prevent re-download on every test suite run if the local answer key
matches the latest version.
2013-07-18 14:31:08 -04:00
Eddie Hebert 5579e54c6f TST: Read benchmark returns directly from answer key spreadsheet.
The risk tests originally were based on a spread sheet, with the
results of returns etc copy and pasted into the `test_risk` module.

Include the spreadsheet and read the values directly using a Python
Excel spreadsheet library.
2013-07-18 14:31:08 -04:00