Coder Social home page Coder Social logo

mustafaseisa / sharpemax Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 32 KB

Sharpe ratio portfolio maximization by way of quadratic programming.

License: MIT License

R 100.00%
portfolio-optimization time-series quantitative-finance statistics convex-optimization sharpe-ratio quadratic-programming quadprog

sharpemax's Introduction

Sharpe Ratio Portfolio Optimization via Quadratic Programming

This repository contains a set of scripts that perform (constrained) Sharpe Ratio portfolio optimization by casting the original quasi-convex Sharpe ratio maximization problem as a convex program (i.e. a quadratic program).

Requirements

In order to use the sharpe ration maximization scripts in this repository:

  • You must be using Mac OSX or Linux
  • You must have installed R programming language, version 3 or later
  • You must have installed the quadprog library
  • You must navigate to this repository after cloning it
git clone https://…/sharpemax.git
cd ./sharpemax

before executing any of the scripts therein

Unconstrained

Format your returns data as a csv that has the asset names as the column headers and the returns down each column, however do not include a date column and make sure instead of percent (12, -3, …) you use raw numbers (.12, -.03, …). To obtain the optimal portfolio weights, simply run the following in command line

Rscript --vanilla ./maxSharpe.R /path/to/your/data.csv

and it will output maxSharpe.csv containing portfolio weights to the root of this repository.

Robust Sharpe Ratio

The sharpe ratio is the ratio between the mean and variance. However, the simple arithmetic estimators for both statistics are not impervious to outliers or extreme events (which are common in financial time series) and, in the case of covariance, underestimates non-linear monotonic relationships. The provided script maxSharpeRobust.R functions exactly like maxSharpe.R,

Rscript --vanilla ./maxSharpeRobust.R /path/to/your/data.csv

however, internally it uses the truncated mean in place of the arithmetic mean and Spearman’s rank covariance in place of the arithmetic covariance. This ultimately results in a Sharpe ratio that is less statistically efficient, but significantly more robust.

Imposing Constraints

The script maxSharpeConstrained.R can process asset constraints, sector constraints, and class constraints. You can call it as follows

Rscript --vanilla maxSharpeConstrained.R ./returns.csv ./assetConstraints.csv ./sectorConstraints.csv ./classConstraints.csv

where the csv’s follow the same format as the files contained in the included testData directory. The script will write a new csv with weights and another with performance metrics, in the same as maxSharpe.R. Leave constraints blank and they will be ignored. The fewer the constraints, the faster the program will typically run.

Comparing Several Sets of Constraints

The script maxSharpeConstrainedLoop.R runs maxSharpeConstrained.R many times on a set of strategies you define. Specifically, you must fill a directory the same way as the folder loopTestData; containing one returns.csv but many different constraint files according to your strategy. They must also abide by the same naming convention as the files in the loopTestData folder (i.e. assetConstraints_1.csv, sectorConstraints_2.csv, and so forth for as many strategies as you'll be considering). Then, you call the script from command line as follows

Rscript --vanilla maxSharpeConstrainedLoop.R …/directory/with/constraint/files/ totalNumberOfStrategies

Where totalNumberOfStrategies is a positive integer specifying how many sets of constraints you have in your constraints directory. This will then run the algorithm through your strategies and output a file sharpeLoops.csv with strategies listed along the columns and metrics listed along the rows.

Sortino Ratio Maximization

Additionally included is a script maxSortinoConstrainedLoop.R which attempts to maximize the Sortino ratio instead of the Sharpe ratio, using Estrada’s method for estimating the semi-covariance matrix. If the semi-covariance matrix is indefinite, it is projected onto the semidefinite cone so that the underlying optimization problem remains convex.

The maxSortinoConstrainedLoop.R script functions just like maxSharpeConstrainedLoop.R

Rscript --vanilla maxSortinoConstrainedLoop.R …/directory/with/constraint/files/ totalNumberOfStrategies

Note on Formatting Data

In all these scripts the algorithm takes returns and all constraints as raw numbers (so that if you multiplied them by 100, you get percent) and also outputs the weights as raw numbers. This way the allocation size doesn't affect how the constraints are interpreted: A minimum of .3 or 30% on asset 1 is the same irrespective of whether you're investing $10k or $1M. You can then multiply the weights by the investment amount and get the per-asset dollar allocations back.

Also, the assets/securities must have text names (column headers) or else R has some issues.

sharpemax's People

Contributors

mustafaseisa avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ianmadlenya

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.