Download as ps, pdf, or txt
Download as ps, pdf, or txt
You are on page 1of 4

CMU 15-211 Fundamental Structures of Computer Science I

Assignment # 3 Fall 1997


Dynamic Programming for Fun and Pro t
Due: Thursday, October 2, 1997, 11:59 PM (Thursday night)
and
Sunday, October 5, 1997, 11:59 PM (Sunday night)
Note: this assignment contains both a written part and a programming part. Written
answers should be in a le written.txt and your program should be in a le invest.C.
Your le written.txt should contain answers to the two tasks given in this font below.
The written portion is worth 20 points and the programming portion is worth 80 points.
The written part is due on Thursday, October 2, 1997, 11:59 PM. The program is due on
Sunday, October 5, 1997, 11:59 PM. To get started on the programming portion of the
assignment, run hw3setup. To hand in the written portion run handin-part1. To hand in
the programming portion of the assignment, run handin.

1 Overview
You have just been hired as deputy assistant analyst at the Funds-R-Us mutual fund com-
pany. Funds-R-Us allocates its funds among several di erent investments (e.g., stocks, bonds,
CDs, and other mutual funds). As your rst job, you are given data on how each of these
investments performed in the past year. Your job is not to predict the future (that's for your
boss, the assistant analyst); instead, you are supposed to gure out what would have been
the best that Funds-R-Us could have done in the past year, had it known in advance what
was going to happen. In other words, if one year ago somebody from the future had told
you what the market was going to do this past year, what would have been the best thing
to do. (So, make sure to complete this assignment before building any time machines.)
If it were the case that you could buy and sell without paying any commission, then this
task would be easy. You simply look up which of these investments did best on week 1,
and say that all of your money should have been there on week 1. Then you look up which
investment did best on week 2, and say that you should have moved all your money there for
week 2. Then you look up which did best on week 3, and so forth. Unfortunately, you have
to pay a commission every time you move money around.1 So, the situation is a little more
tricky. For instance, suppose on week 1 that investment A did well and investment B was
mediocre, and then on week 2 investment A was mediocre and investment B was better, but
only slightly better than A. In that case, you would probably have been best o with A on
both weeks because you would have had to pay a fee to move your money. But, now suppose
weeks 3, 4, and 5 were all like week 2. In that case, you should have moved your money right
after week 1, because the advantage of B over A on weeks 3, 4, and 5 was enough to pay for
the cost of moving the money. Read this paragraph several times until it makes sense.
1 Even a big company like Funds-R-Us has to pay the \specialists" that determine a slight di erence
between buy and sell prices of a given stock.

1
A simplifying assumption we will make is that all commission charges are in percentages.
For instance, investment A might have a 1% charge to buy and a 1% charge to sell: so if
you purchase investment A, then A doubles in value, and then you sell all your shares, your
money will have gone up by a factor of 0 99  2 0  0 99 = 1 96.
: : : :

There's one more important aspect to note about this problem. A standard piece of
advice for investing when you don't know the future is to diversify: to spread your money
around to hedge against risk. However, in the problem that we are looking at, there is always
an optimal strategy that at every point in time has all its money in one place. For instance,
it might say: \you should have started with all your money in investment A, and then after
week 1 moved all your money to investment B, and then after week 5, moved it all back to
investment A." It won't say \in week 2 put half of your money in A and half of it in B."
Think about why there is always an optimal strategy of this form, and give a short (1 paragraph)
explanation. It's OK to talk to other people about this one, but write down your answer in your
own words. Put your answer into written.txt. Remember, this is due Thurs 10/2. This fact will
allow us to solve the problem by using Dynamic Programming.

2 Details
You will be given an input le that contains all the market data. A typical le might look
like this.
4 5

Cash 0.0 0.0 AAA 0.01 0.01 IBM 0.05 0.0 YUG 0.0 0.03

1.0 1.07 1.04 1.01


1.0 0.95 1.02 1.01
1.0 1.02 1.00 1.04
1.0 0.99 0.99 1.01
1.0 0.0 0.0 0.0

Explanation: The rst number in the le gives the number of di erent investments under
consideration: in the above example, there are 4. The second number gives the number of
time periods (weeks). Next, for each investment, are two numbers: the commission you pay
to buy it, and the commission to sell it. For instance, in this example, there is no commission
to buy or sell Cash, the rst investment. The second investment, AAA, costs 1% to buy and
1% to sell. The third, IBM, has a 5% charge to buy and zero to sell, and YUG has zero
to buy and 3% to sell. We assume that at time zero, all our money is in the Cash, which
corresponds to having the money in our pocket (that's why there's no commission on it).
Finally, there is the market data. In this example, in week 1 Cash didn't change in value,
AAA went up by 7% (so your money gets multiplied by 1.07), the third investment, IBM,
went up by 4%, and YUG went up by 1%. In week 2, Cash didn't change in value, the AAA
went down by 5%, IBM up by 2%, and YUG up by 1%. Notice that on the last day, all
values are 0 except for Cash: this is just to force you to \cash out" at the end. (E.g., at the
2
end of the game we want to sell any holdings and count our money.) You may assume (it
will save you a few lines of code) that the last line of the input le looks like this.
In this example, the optimal strategy turns out to be the following. Begin by buying
AAA (paying a 1% commission), then after week 1, sell it (paying another 1%) and buy
YUG (paying 0%). Keep YUG throughout weeks 2,3,4, nally selling it at the end (paying
a 3% commission). This causes the value of one's portfolio to increase by a factor of
0 99  1 07  0 99  1 01  1 04  1 01  0 97 = 1 0792
: : : : : : : : :

(The \0.99"s correspond to paying the 1% commission, and the \0.97" corresponds to the
3% commission.)

3 Speci cations
Your job is to write a program that will compute the optimal strategy. If there are N

investments and weeks, your program should run in time ( 2 ). Here is a hint on how
T O N T

to do it. Imagine that you have already computed the best strategy for the rst 1 weeks,
t

and moreover, for each  , you have computed the best strategy for the rst 1 weeks
i N t

that ends with all your money in investment . Now, use this to compute the same thing for
i

week . For instance, the best -week strategy that ends with all your money in investment
t t

3 either had all its money in investment 3 at the end of week 1 and then kept it there, or
t

else it had all its money in one of the other investments at the end of week 1 and then
t

sold that and bought number 3. You want to pick the best out of these. Explain in words
why the running time of your program is ( 2 ). Put your answer into written.txt. Remember,
O N T

this is due Thurs 10/2.


Your program should print out what the value is of the best strategy assuming all your
money is in cash before week 1 (e.g., 1.0792 in the above example) and should print out the
sequence of buy/sell decisions. There are several sample input les that hw3setup deposits
in your 211hw/assign3 directory you can use for testing.
We have given you an executable invest211 (it is in bin/@sys, but just typing \in-
vest211" should work if your paths are set up correctly). The output from your program
should exactly match the output of invest211. In order to see if the output matches, you
can run \diff your-output our-output". If they agree, di prints nothing, otherwise it
prints out the di erences between the two les.
Your program must be named invest.C, and any header information must be in a le
named invest.h. If you wish, you may create a Make le (otherwise, just type \g++ -g
-Wall invest.C -o invest" to compile your program and create an executable named
\invest").
Your program should use cin and cout for input and output. You can run your program
on input \inputfile" by typing \invest < inputfile" at the unix prompt. You can run
it on input \inputfile" and put the output into \outputfile" by typing:
\invest < inputfile > outputfile".

3
To recap: Your output should exactly match the output of invest211, and when you
run handin you should have the following les in your directory:
 invest.C
 invest.h
 Make le (optional)

4 2-D arrays
You might also nd it useful to create a 2-dimensional array in your program. Here is one
way to make a 2-d array if you haven't done this sort of thing before:
typedef double *double_arr; // double_arr means an array of double-precision floats
int main(void)
{
double_arr *arr; //2-d array of doubles
int num_investments, max_time;
cin >> num_investments >> max_time;
arr = new double_arr[max_time+1];
for(int t=0; t <= max_time; ++t) arr[t] = new double[num_investments];
...

After this code, arr is a max time+1 by num investments size array, and arr[t][i] would
be the entry for investment i and time t.

You might also like