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...


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_wordthat 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_stringthe 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_countthat 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_stringthe 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_multiplicationthat implements this.



Inputs




  • the_numberthe 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_pricebefore the start market opens today and that will either go up byprice_percent_changeor down byprice_percent_changeat the end of the day. The probability that the price goes up isprob_up.


2A: Binomial Stock Prices


Write a functionbinomial_stock_pricesthat 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_simulationsrows andnum_dayscolumns where each item in the matrix is the price of the stock on the simulation-day.



Inputs




  • current_pricethe price of the stock today (at day zero).


  • num_simulationsthe number of different stock prices to simulate.


  • num_daysthe number of days to simulate for each simulation.


  • price_percent_changethe 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_upthe probability that the price increases with default of0.5


  • random_seedthe 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_priceat an ending timenum_daysfrom now. You will only exercise a European call option if theending_price(i.e, the pricenum_daysfrom 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_pricein profit. If theending_priceis below thestrike_pricethen you don't buy the stock and you make no profit.


Write a functioneuropean_call_optionreturns the average profit for a given set of inputs.



Inputs




  • current_pricethe price of the stock today (at day zero).


  • strike_pricethe price at which you can buy the stock afternum_days.


  • num_simulationsthe number of different stock prices to simulate.


  • num_daysthe number of days to simulate for each simulation.


  • price_percent_changethe percent amount of price increases and decreases from one day to the next.


  • prob_upthe probability that the price increases with default of0.5


  • random_seedthe 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_priceat an ending timenum_daysfrom now and sell the stock atmax_price, the maximum price observed from now untilnum_daysfrom now . You will only exercise a lookback call option if themax_priceis 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_priceand getmax_price - strike_pricein profit. If themax_priceis below thestrike_pricethen you don't buy the stock and you make no profit.


Write a functionlookback_call_optionreturns the average profit for a given set of inputs.



Inputs




  • current_pricethe price of the stock today (at day zero).


  • strike_pricethe price at which you can buy the stock afternum_days.


  • num_simulationsthe number of different stock prices to simulate.


  • num_daysthe number of days to simulate for each simulation.


  • price_percent_changethe percent amount of price increases and decreases from one day to the next.


  • prob_upthe probability that the price increases with default of0.5


  • random_seedthe 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_ratioinnum_daysif thevolatility_ratiois greater than avolatility_ratio_threshold. Thevolatility_ratiois the standard deviation ofsimulated_stock_pricesacross all days, divided by the average ofsimulated_stock_prices.


Write a functionvolatility_optionreturns the average profit for a given set of inputs.



Inputs




  • current_pricethe price of the stock today (at day zero).


  • volatility_ratio_thresholdthe required ratio of standard deviation of stock price to average stock price to activate the option.


  • num_simulationsthe number of different stock prices to simulate.


  • num_daysthe number of days to simulate for each simulation.


  • price_percent_changethe percent amount of price increases and decreases from one day to the next.


  • prob_upthe probability that the price increases with default of0.5


  • random_seedthe 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_gamblereturns the average profit for a given set of inputs.


NOTE: To implement the two different stocks, userandom_seedas an input for one of them andrandom_seed + 1as an input for the other. If you do this any other way you will not get the results I get.



Inputs




  • current_pricethe price of the stock today (at day zero).


  • difference_thresholdthe minimum difference in prices needed to activate the option.


  • num_simulationsthe number of different stock prices to simulate.


  • num_daysthe number of days to simulate for each simulation.


  • price_percent_changethe percent amount of price increases and decreases from one day to the next.


  • prob_upthe probability that the price increases with default of0.5


  • random_seedthe 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

Sep 28, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here