CSE 131 Module 8: ADTs

Extensions

Extension 1: Sad Cycles (5 points):

Authors
This extension needs a unit test. For now, run it as indicated at the end of this assignment.

Overview

So it turns out that numbers can feel things. We define a happy or sad number through this process:

  1. Split up the number into its digits.
  2. Square the digits and sum them.
  3. Repeat with the new sum until you reach a set of numbers that repeat or the number 1.
  4. If your process results in a repeating 1, your initial number was happy! Otherwise, it has fallen into a cycle of depression, characterized by the repeating numbers [4, 16, 37, 58, 89, 145, 42, 20, 4...] How sad.
For example, starting with the number 15, our process yields:

In this extension you will be designing a program that uses sets to find the sad cycle of a number for an arbitrary power.

Procedure

In the sadcycle package of your extensions folder, create a SadCycler class.

When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 8.1
Last name WUSTL Key Propagate?
(or your numeric ID) Do not propagate
e.g. Smith j.smith
1 Copy from 1 to all others
2 Copy from 2 to all others

TA: Password:

End of extension 1


Extension 2: Sharpe Computation (5 points):

Authors
Issues: Working on it, stay tuned

Introduction

Have you wondered how to determine the profitability of a portfolio or of a given trading strategy? In this assignment, you calculate the profitability of a portfolio using one of the most fundamental tools of finance: the Sharpe ratio.

Sharpe Ratio

The Sharpe ratio is a measure for calculating risk-adjusted return. It was developed by Nobel laureate William F. Sharpe.

Suppose you are considering investing a given amount of money in one of two possible ways:

a
This investment has risk. You may experience a positive return on your investment, or you could lose your money. Let Ra denote the return you will receive if your money is invested this way.
b
This investment has no risk. So let us say that you will certainly receive Rb funds if your money is invested this way.
The Sharpe ratio is the average return earned in excess of the risk-free rate per unit of risk. Subtracting the risk-free rate from the mean return, the performance of the portfolio with risk can be isolated. For example, a portfolio engaging in zero risk, such as the purchase of U.S. Treasury bills or bonds, has a Sharpe ratio of exactly zero (the numerator is 0).

The Sharpe ratio is calculated with this formula:

RKC need picture

To calculate average rate of return of x, just add up all the provided returns for each individual stock and divide by the number of stocks.

Assume that for this portfolio, the risk free rate or Rf is 2%.

The portfolio standard deviation can be calculated using this formula:

RKC Do exponentiation using this Stddev = [ (wgt1 ^2 * s1 ^2) + (wgt2 ^2 * s2 ^ 2) + (2 * w1 * w2 * cov12) ] ^ .5

where

For the purposes of this extension, assume that all the stocks have no correlation with each other and are weighted equally. This means that the covariance between any two stocks is 0 and that the weight of each stock is simply 1/n where n is the number of stocks.

Now that you understand what the Sharpe ratio is, it is time to apply what you have learned.

Directions

Click on the extensions folder and create a new class called CalculateSharpeRatio in the sharperatio package.

Set up the ArgsProcessor to take input from datafiles/stockaccount. This is where the data you will use to calculate the Sharpe ratio comes from.

You will have to create a List of doubles for both the expected returns and the variances in the main method.

After, write up a loop to read in the doubles for the expected returns and another loop for the standard deviations. Once your program has read in these values, you can start making your calculations for the Sharpe ratio.

Calculate the Sharpe ratio and round it to two decimal points then print it out.

When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 8.2
Last name WUSTL Key Propagate?
(or your numeric ID) Do not propagate
e.g. Smith j.smith
1 Copy from 1 to all others
2 Copy from 2 to all others

TA: Password:

End of extension 2