Problem Set 1
This problem set has two primary parts. In each part you are asked to write one or more functions that I will test with different inputs (simliar to what we did in the Labs). Don't jump around. Do part 1A, then 1B, etc. I really want you to push yourself to do this on your own. I'm here to help if needed, but only after you've spent some time on your own. Start early and then when you've reached the point where you can't get anything else done, take a break for a few hours or a day. Then come back and work on it some more.
When you are done with a part, run the tests to make sure everything is working. There are three or four tests for each part for a total of 25 tests. When you pass all the tests, hit the Submit button
Part 1: Base Python
Here we will use Base Python to write a couple functions for us. NumPy is not needed. iPython is installed just to make things easier for you to test.
1A: Special Words
Write a function calledis_special_word
that takes a string as input and returns a bool indicating whether this string is a "special word." For our purposes, a special word is one where at least one of the characters is a string representation of a number.
Inputs
the_string
the string to be checked for being a special word.
Example:
Input:the_string = "The"
Output:False
Input:the_string = "f7s"
Output:True
1B: Counting Words
Write a function calledword_count
that takes a string as input and returns a dictionary with unique word count of special words in the string. Special words are those described above. Each word in the string is separated by a space.
Inputs
the_string
the string to count special words within.
Example:
Input:the_string = "The rain in Spain stays mainly in the plain."
Output:{}
Input:the_string = "T7 t7 hi 9 there t7"
Output:{'T7': 1, 't7': 2, '9': 1}
1C: Weird Multiplication
I've invented a new way to do multiplication for whole numbers. For this, you take the number in the one's place and multiply it by all the numbers not in the one's place. However, if the number in the one's place is a zero, you don't do the multiplication and instead drop the zero (see the example below). You continue with this process until the number is a single digit (between 1 and 9). Write a function calledweird_multiplication
that implements this.
Inputs
the_number
the number for which we want to perform this weird multiplication.
Example:
Input:the_number = 345
Output:7
Explanation: 34*5 = 170. The one's place is a zero, so we drop the zero to get 17. Finally, 1*7 = 7.
Part 2: Binomial Model for Option Pricing
The binomial model for options pricing is based on the idea that an investment can either go up or down in any given time interval. To simplify things, let's imagine we are talking about a stock with acurrent_price
before the start market opens today and that will either go up byprice_percent_change
or down byprice_percent_change
at the end of the day. The probability that the price goes up isprob_up
.
2A: Binomial Stock Prices
Write a functionbinomial_stock_prices
that simulates stock prices using a binomal pricing model as described above. The number of days and number of times to simulate should be included as inputs. The output should be a 2-d array withnum_simulations
rows andnum_days
columns where each item in the matrix is the price of the stock on the simulation-day.
Inputs
current_price
the price of the stock today (at day zero).
num_simulations
the number of different stock prices to simulate.
num_days
the number of days to simulate for each simulation.
price_percent_change
the percent amount of price increases or decreases from one day to the next. For example, a 5% change would beprice_percent_change = 0.05
.
prob_up
the probability that the price increases with default of0.5
random_seed
the random seed to use for NumPy's random number generation with default13579
Example:
Input:binomial_stock_prices(current_price = 90, num_simulations = 5, num_days = 3, price_percent_change = 0.03, prob_up = 0.5, random_seed = 13579)
Output:array([[92.7 , 95.481 , 98.34543], [87.3 , 89.919 , 87.22143], [87.3 , 84.681 , 82.14057], [92.7 , 95.481 , 92.61657], [87.3 , 84.681 , 82.14057]])
2B: European Call Option
A European call option gives the owner the right, but not the obligation, to buy a stock for astrike_price
at an ending timenum_days
from now. You will only exercise a European call option if theending_price
(i.e, the pricenum_days
from now) is greater than thestrike_price
. Exercise the option means you agree to buy the stock for thestrike_price
. At that point you would then sell the stock in the open market and getending_price - strike_price
in profit. If theending_price
is below thestrike_price
then you don't buy the stock and you make no profit.
Write a functioneuropean_call_option
returns the average profit for a given set of inputs.
Inputs
current_price
the price of the stock today (at day zero).
strike_price
the price at which you can buy the stock afternum_days
.
num_simulations
the number of different stock prices to simulate.
num_days
the number of days to simulate for each simulation.
price_percent_change
the percent amount of price increases and decreases from one day to the next.
prob_up
the probability that the price increases with default of0.5
random_seed
the random seed to use for NumPy's random number generation with default13579
Example:
Input:european_call_option(current_price = 90, strike_price = 85, num_simulations = 5, num_days = 3, price_percent_change = 0.03, prob_up = 0.5, random_seed = 13579)
Output:4.636685999999997
2C: Lookback Call Option
A lookback call option gives the owner the right, but not the obligation, to buy a stock for astrike_price
at an ending timenum_days
from now and sell the stock atmax_price
, the maximum price observed from now untilnum_days
from now . You will only exercise a lookback call option if themax_price
is greater than thestrike_price
. Exercise the option means you agree to buy the stock for thestrike_price
. At that point you would then sell the stock formax_price
and getmax_price - strike_price
in profit. If themax_price
is below thestrike_price
then you don't buy the stock and you make no profit.
Write a functionlookback_call_option
returns the average profit for a given set of inputs.
Inputs
current_price
the price of the stock today (at day zero).
strike_price
the price at which you can buy the stock afternum_days
.
num_simulations
the number of different stock prices to simulate.
num_days
the number of days to simulate for each simulation.
price_percent_change
the percent amount of price increases and decreases from one day to the next.
prob_up
the probability that the price increases with default of0.5
random_seed
the random seed to use for NumPy's random number generation with default13579
Example:
Input:lookback_call_option(current_price = 90, strike_price = 85, num_simulations = 5, num_days = 3, price_percent_change = 0.03, prob_up = 0.5, random_seed = 13579)
Output:6.669085999999996
2D: Volatility Option
This option gives you the right, but not the obligation to buy the stock forending_price * volatility_ratio
innum_days
if thevolatility_ratio
is greater than avolatility_ratio_threshold
. Thevolatility_ratio
is the standard deviation ofsimulated_stock_prices
across all days, divided by the average ofsimulated_stock_prices
.
Write a functionvolatility_option
returns the average profit for a given set of inputs.
Inputs
current_price
the price of the stock today (at day zero).
volatility_ratio_threshold
the required ratio of standard deviation of stock price to average stock price to activate the option.
num_simulations
the number of different stock prices to simulate.
num_days
the number of days to simulate for each simulation.
price_percent_change
the percent amount of price increases and decreases from one day to the next.
prob_up
the probability that the price increases with default of0.5
random_seed
the random seed to use for NumPy's random number generation with default13579
Example:
Input:volatility_option(current_price = 90, volatility_ratio_threshold = 0.02, num_simulations = 5, num_days = 3, price_percent_change = 0.03, prob_up = 0.5, random_seed = 13579)
Output:51.23362477025618
2E: Dual Stock Gamble
In a dual stock gamble, we simulate two different stocks. The payout at the end can be either positive or negative. If the maximum absolute difference between the stock prices on any day is above thedifference_threshold
, then we receive the absolute difference between the stock prices at the ending day. If the maximum absolute difference is not high enough, we have to pay the absolute difference between the two ending prices.
Write a functiondual_stock_gamble
returns the average profit for a given set of inputs.
NOTE: To implement the two different stocks, userandom_seed
as an input for one of them andrandom_seed + 1
as an input for the other. If you do this any other way you will not get the results I get.
Inputs
current_price
the price of the stock today (at day zero).
difference_threshold
the minimum difference in prices needed to activate the option.
num_simulations
the number of different stock prices to simulate.
num_days
the number of days to simulate for each simulation.
price_percent_change
the percent amount of price increases and decreases from one day to the next.
prob_up
the probability that the price increases with default of0.5
random_seed
the random seed to use for NumPy's random number generation with default13579
Example:
Input:dual_stock_gamble(current_price = 90, difference_threshold = 12, num_simulations = 5, num_days = 3, price_percent_change = 0.03, prob_up = 0.5, random_seed = 13579)
Output:4.257143999999999