0% found this document useful (0 votes)
99 views

Backtesting of Portfolio Datacamp

This document discusses optimizing portfolios in R. It begins with defining a benchmark equally weighted portfolio using historical return data. A base portfolio is then defined with constraints of full investment and long only, aiming to minimize risk. This base portfolio is optimized with quarterly rebalancing over historical data. Performance is analyzed. Constraints are then refined by adding a box constraint and re-optimizing/analyzing to improve the portfolio.

Uploaded by

priya ps
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Backtesting of Portfolio Datacamp

This document discusses optimizing portfolios in R. It begins with defining a benchmark equally weighted portfolio using historical return data. A base portfolio is then defined with constraints of full investment and long only, aiming to minimize risk. This base portfolio is optimized with quarterly rebalancing over historical data. Performance is analyzed. Constraints are then refined by adding a box constraint and re-optimizing/analyzing to improve the portfolio.

Uploaded by

priya ps
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

INTERMEDIATE PORTFOLIO ANALYSIS IN R

Application
Intermediate Portfolio Analysis in R

Real World Example


● Solve a portfolio optimization problem similar to the types 

of problems in the industry
● Apply techniques learned throughout the course
● Specify a portfolio with constraints and objectives
● Run the optimization with period rebalancing on historical data

● Analyze the results

● Refine constraints, objectives, and moment estimates

● Data
● EDHEC-Risk Alternative Indexes monthly returns

● Jan 1997 - March 2016


Intermediate Portfolio Analysis in R

Benchmark
> data(indexes)
> returns <- indexes[,1:4]

# Equal weight benchmark


> n <- ncol(returns)
> equal_weights <- rep(1 / n, n)

> benchmark_returns <- Return.portfolio(R = returns,


weights = equal_weights, rebalance_on = "years")

> colnames(benchmark_returns) <- "benchmark"

# Benchmark performance
> table.AnnualizedReturns(benchmark_returns)
benchmark
Annualized Return 0.0775
Annualized Std Dev 0.1032
Annualized Sharpe (Rf=0%) 0.7509
Intermediate Portfolio Analysis in R

Base Portfolio Definition


● Define a portfolio specification to be used as the base case
● The base portfolio specification is meant to be a simple
approach with relaxed constraints and basic objectives
● Do not overcomplicate or over-constrain

# Base portfolio specification


> base_port_spec <- portfolio.spec(assets = colnames(returns))
> base_port_spec <- add.constraint(portfolio = base_port_spec,
type = "full_investment")
> base_port_spec <- add.constraint(portfolio = base_port_spec,
type = "long_only")
> base_port_spec <- add.objective(portfolio = base_port_spec,
type = "risk", name = "StdDev")
INTERMEDIATE PORTFOLIO ANALYSIS IN R

Let’s practice!
INTERMEDIATE PORTFOLIO ANALYSIS IN R

Optimization Backtest
Intermediate Portfolio Analysis in R

Optimization Backtest: Execution


Run the optimization with periodic rebalancing
> opt_base <- optimize.portfolio.rebalancing(R = returns,
optimize_method = "ROI",
portfolio = base_port_spec,
rebalance_on = "quarters",
training_period = 60,
rolling_window = 60)

> # Calculate portfolio returns


> base_returns <- Return.portfolio(returns,
extractWeights(opt_base))
> colnames(base_returns) <- "base"
Intermediate Portfolio Analysis in R

Optimization Backtest: Analysis


> # Chart the optimal weights
> chart.Weights(opt_base)
Intermediate Portfolio Analysis in R

Optimization Backtest: Analysis


> # Merge benchmark and portfolio returns
> ret <- cbind(benchmark_returns, base_returns)

> # Annualized performance


> table.AnnualizedReturns(ret)
benchmark base
Annualized Return 0.0775 0.0772
Annualized Std Dev 0.1032 0.0436
Annualized Sharpe (Rf=0%) 0.7509 1.7714
Intermediate Portfolio Analysis in R

Optimization Backtest: Refine Constraints


> # Make a copy of the portfolio specification
> box_port_spec <- base_port_spec

> # Update the constraint


> box_port_spec <- add.constraint(portfolio = box_port_spec,
type = "box", min = 0.05, max = 0.4,
indexnum = 2)

> # Backtest
> opt_box <- optimize.portfolio.rebalancing(R = returns,
optimize_method = "ROI",
portfolio = box_port_spec,
rebalance_on = "quarters",
training_period = 60,
rolling_window = 60)

> # Calculate portfolio returns


> box_returns <- Return.portfolio(returns, extractWeights(opt_box))
> colnames(box_returns) <- "box"
Intermediate Portfolio Analysis in R

Optimization Backtest: Analysis Refined Constraints


> # Chart the optimal weights
> chart.Weights(opt_box)
Intermediate Portfolio Analysis in R

Optimization Backtest: Analysis Refined Constraints


> # Merge box portfolio returns
> ret <- cbind(ret, box_returns)

> # Annualized performance


> table.AnnualizedReturns(ret)
benchmark base box
Annualized Return 0.0775 0.0772 0.0760
Annualized Std Dev 0.1032 0.0436 0.0819
Annualized Sharpe (Rf=0%) 0.7509 1.7714 0.9282
INTERMEDIATE PORTFOLIO ANALYSIS IN R

Let’s practice!
INTERMEDIATE PORTFOLIO ANALYSIS IN R

Congratulations!

You might also like