<jupyter_start><jupyter_text>Compute Drawdowns1. Compute a wealth index2. Compute previous peaks3. Compute drawdown - which is the wealth value as a percentage of the previous peak<jupyter_code>wealth_index = 1000*(1+rets["LargeCap"]).cumprod()
wealth_index.head()
wealth_index.plot.line()
def drawdown(return_series: pd.Series):
    """
    Takes a time series of asset returns
    Computes and returns a DataFrame that contains:
    the wealth index
    the previous peaks
    percent drawdowns
    """
    wealth_index = 1000*(1+return_series).cumprod()
    previous_peaks = wealth_index.cummax()
    drawdowns = (wealth_index - previous_peaks)/previous_peaks
    return pd.DataFrame({
        "Wealth": wealth_index,
        "Peaks": previous_peaks,
        "Drawdown": drawdowns
    })
drawdown(rets["LargeCap"]).head()
drawdown(rets["LargeCap"])[["Wealth", "Peaks"]].head()
drawdown(rets["LargeCap"])[["Wealth", "Peaks"]].plot()
drawdown(rets["LargeCap"])["Drawdown"].min()
drawdown(rets["LargeCap"])["Drawdown"].idxmin()
drawdown(rets["SmallCap"])["Drawdown"].idxmin()
drawdown(rets["1940":]["LargeCap"])["Drawdown"].idxmin()
drawdown(rets["1975":]["LargeCap"])["Drawdown"].idxmin()
drawdown(rets["1940":]["SmallCap"])["Drawdown"].idxmin()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16927899686520376, 'fraction_numerical': 0.019592476489028215, 'mean_word_length': 6.05524861878453, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '553340', 'license': 'MIT', 'n_tokens_mistral': 435, 'n_tokens_neox': 412, 'n_words': 77, 'path': 'course1/lab4-drawdown.ipynb', 'repo_name': 'plbenpeterson/dash-and-jupyter-notebook-with-gitpod', 'size': '1276'}