Homework 3
Asset Allocation Simulation

Computational Finance

Copyright © Philip H. Dybvig 1996

Scenario You are working in the treasury of a large corporation. One responsibility of your group is oversight of the company's pension fund, which normally is 50-50 in equities and fixed income. Your boss has asked you to evaluate the claim of some academic financial economists that such funds are usually traded much too frequently. Your analysis will use a simulation to assess the benefits of frequent rebalancing.

Action You will be given an existing program that performs a simple simulation assuming rebalancing each period. You will adapt the program to assume that rebalancing is less frequent.

Concept Simulation is a powerful tool for analyzing portfolio strategies. This Homework gives a realistic analysis of a serious practical problem faced in practice.


  1. Put the program files (simu1test.cc, simu1.cc, and simu1.h ) in a directory on your machine where you want to work. Rename them as necessary. Compile the programs. Move the output from the program to a spreadsheet and graph the relation between the final stock price and the portfolio value, using the default parameter values. Subject to your computer's capacity, I recommend using a much larger number than 100 random draws for this analysis.
  2. Write a new version of the program in which the portfolio proportions in stocks and bonds is varied only once every k periods, where k is a new argument to the function aa::fixprops. Debug and compile the program, and produce a new plot for this case, given the identical default parameter values but assuming that portfolio weights are rebalanced (i) monthly, (ii) quarterly, and (iii) annually.
  3. (thought question) Based on the simulation results, how infrequently must rebalancing occur before there is a significant impact on overall performance?
Extra for Experts
  1. Introduce transaction costs assuming that you pay a fixed proportion d of the dollar value of any stock transaction (purchase or sale). Do the same analysis as in the regular instructions with transaction costs of d=.2%. How often do you recommend the portfolio should be rebalanced?


    Assume that risk preferences are represented by maximization of the expected logarithm of terminal wealth. Compute (and/or approximate) the optimal trading strategy when facing transaction costs. Warning: this is very difficult.
Some useful C++ building blocks

To update the portfolio only every k periods, we can use an ``if'' command to have conditional execution of the part of the for loop in aa::fixprops that updates the portfolio to keep the proportions fixed. Of course, we do not want to delete the part of the for loop that computes the random change in stock price or the part of the loop that computes interest on cash!

In forming the logical argument to the if statement, we want to use the ``=='' operator, where ``e1 == e2'' returns a true value if the two expressions e1 and e2 evaluate to the same value. Note that in usual mathematical notation, this is sometimes expressed as ``e1 = e2'' but this is definitely not the same in C++, since this indicates assignment (i.e., the new value of e1 is given by e2, which makes sense only if e1 is a variable and will give the wrong answer in any case). Using ``='' instead of ``=='' in the condition of an if() is a mistake I often make. You should try to avoid it.

Another handy feature of C++ for forming the test is the remainder operator %. If e3 and e4 evaluate to integers, then ``e3 % e4'' gives the remainder from dividing e3 by e4. For example, we have that ``(e3 % e4) == 0'' is true if e3 is a multiple of e4 and false otherwise.

Exhibit A: the header file simu1.h

// simu1.h Asset Allocation Header

struct termvals {
  double termstock;
  double termwealth;};

class aa {
    aa(double ttm=1.0,int nper=120,double r=.05,double mean=.15,double stddev=.2);
    int restart(double ttm=1.0,int nper=120,double r=.05,double mean=.15,double stddev=.2);
    termvals fixprops(double inrisky,double initstock=0.0,double initcash=100.0);
    int npers;
    double tinc, r1per, mean1per, std1persqrt12, wealth, stockpos, cash, stockP, stockret;
    double stocktotret();};
Exhibit B: the test file simu1test.cc
// simu1test.cc Asset Allocation test file
#include <iostream.h>
#include "simu1.h"

main() {
  int i;
  aa sim1;
  termvals y;
  for(i=0;i<100;i++) {
    y = sim1.fixprops(.5);
    cout << y.termstock << " " << y.termwealth << endl;}
Exhibit C: the implementation file simu1.cc
// simu1.cc Asset Allocation Simulation
#include <math.h>
#include <stdlib.h>
#include <iostream.h>
#include "simu1.h"
#define unifrand() ((double) rand()/((double) RAND_MAX+1.0))

aa::aa(double ttm,int nper,double r,double mean,double stddev) {

int aa::restart(double ttm,int nper,double r,double mean,double stddev) {
  npers = nper;
  tinc = ttm/(double) nper;
  r1per = 1.0 + r*tinc;
  mean1per = 1.0 + mean*tinc;
  std1persqrt12 = sqrt((double) 12) * stddev*sqrt(tinc);
//  cout << " " << npers << " " << tinc << " " << r1per << " "
//    << mean1per << " " << std1persqrt12 << endl;

termvals aa::fixprops(double inrisky,double initstock,double initcash) {
  int i;
  termvals x;
  stockpos = initstock;
  cash = initcash;
  stockP = 100.0;
//  cout << npers << endl;
  for(i=0;i<npers;i++) {
    wealth = stockpos + cash;
    stockpos = inrisky * wealth;
    cash = wealth - stockpos;
    stockret = stocktotret();
//cout << stockret << " " << tinc << " " << mean1per << " " << std1persqrt12 <<
//" " << unifrand() <<endl;
    stockP *= stockret;
    stockpos *= stockret;
    cash *= r1per;
//    cout << stockret << " " << stockP << " " << stockpos << " " << cash << endl;
  x.termstock = stockP;
  x.termwealth = stockpos + cash;

double aa::stocktotret() {
  return(mean1per + std1persqrt12 * (unifrand()-0.5));}