[Returnanalytics-commits] r2369 - pkg/PortfolioAnalytics/sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 19 02:28:55 CEST 2013


Author: rossbennett34
Date: 2013-06-19 02:28:54 +0200 (Wed, 19 Jun 2013)
New Revision: 2369

Added:
   pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R
Log:
adding script to test and demo the portfolio specification structure with adding constraints and objectives

Added: pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R	2013-06-19 00:28:54 UTC (rev 2369)
@@ -0,0 +1,56 @@
+# Testing for the new portfolio specification
+
+# Load necessary packages
+library(PerformanceAnalytics)
+library(PortfolioAnalytics)
+
+# Load the edhec data
+data(edhec)
+
+# Use the first 5 columns of edhec as returns
+ret <- edhec[, 1:5]
+funds <- colnames(ret)
+
+# Specify a portfolio object
+pspec <- portfolio.spec(assets=funds)
+
+# pspec is an object of class "portfolio" that holds information about the
+# assets, constraints, and objectives.
+# The constraints will be stored as objects in the $constraints list
+# The objectives will be stored as objects in the $constraints list. Note that
+# this is just like how they are currently stored in the constraints object.
+class(pspec)
+str(pspec)
+
+# Add a constraint object to pspec for the sum of the weights
+pspec <- add.constraint(portfolio=pspec, type="weight_sum",
+                        min_sum=0.99, max_sum=1.01)
+print(pspec)
+
+# Add box constraints to the pspec object
+pspec <- add.constraint(portfolio=pspec, type="box", min=0.1, max=0.4)
+print(pspec)
+
+# Update the box constraints to pass in a vector for min and max. Updates the
+# object in place with the indexnum argument
+pspec <- add.constraint(portfolio=pspec, type="box", 
+                        min=c(0.1, 0.05, 0.1, 0.15, 0.2), 
+                        max=c(0.4, 0.4, 0.5, 0.45, 0.6), 
+                        indexnum=2)
+print(pspec)
+
+# Add objectives to the pspec object
+pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", 
+                       enabled=FALSE, multiplier=0)
+print(pspec)
+
+pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", 
+                       enabled=FALSE, multiplier=0, risk_aversion=10)
+print(pspec)
+
+pspec <- add.objective_v2(portfolio=pspec, type="risk", name="CVaR", 
+                       enabled=FALSE, multiplier=0)
+print(pspec)
+
+str(pspec)
+summary(pspec)
\ No newline at end of file



More information about the Returnanalytics-commits mailing list