# TODO: Implement function return (prices - prices.shift(1)) / prices.shift(1) project_tests.test_generate_returns(generate_returns) # ### View Data # Let's generate the closing returns using `generate_returns` and view them using a heatmap. # In[ ]: returns = generate_returns(close) project_helper.plot_returns(returns, "Close Returns") # ## Weighted Returns # With the returns of each stock computed, we can use it to compute the returns for an index or ETF. Implement `generate_weighted_returns` to create weighted returns using the returns and weights. # In[ ]: def generate_weighted_returns(returns, weights): """ Generate weighted returns. Parameters ---------- returns : DataFrame
# TODO: Implement Function prev_mon_prices = prices.shift(1) returns = np.log(prices) - np.log(prev_mon_prices) return returns project_tests.test_compute_log_returns(compute_log_returns) # ### View Data # Using the same data returned from `resample_prices`, we'll generate the log returns. # In[10]: monthly_close_returns = compute_log_returns(monthly_close) project_helper.plot_returns( monthly_close_returns.loc[:, apple_ticker], 'Log Returns of {} Stock (Monthly)'.format(apple_ticker)) # ## Shift Returns # Implement the `shift_returns` function to shift the log returns to the previous or future returns in the time series. For example, the parameter `shift_n` is 2 and `returns` is the following: # # ``` # Returns # A B C D # 2013-07-08 0.015 0.082 0.096 0.020 ... # 2013-07-09 0.037 0.095 0.027 0.063 ... # 2013-07-10 0.094 0.001 0.093 0.019 ... # 2013-07-11 0.092 0.057 0.069 0.087 ... # ... ... ... ... ... # ``` #
The returns for each ticker and date """ #TODO: Implement function return (prices / prices.shift(1) - 1) project_tests.test_generate_returns(generate_returns) # ### View Data # Let's generate the closing returns using `generate_returns` and view them using a heatmap. # In[52]: returns = generate_returns(close) project_helper.plot_returns(returns, 'Close Returns') # ## Weighted Returns # With the returns of each stock computed, we can use it to compute the returns for an index or ETF. Implement `generate_weighted_returns` to create weighted returns using the returns and weights. # In[80]: def generate_weighted_returns(returns, weights): """ Generate weighted returns. Parameters ---------- returns : DataFrame Returns for each ticker and date