<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Go Far AI: My Quant Lab]]></title><description><![CDATA[Selected learning Projects done by Ali Alzahrani, the actual in-action strategies are exclusive.]]></description><link>https://www.gofar.ai/s/projects</link><image><url>https://substackcdn.com/image/fetch/$s_!TcLY!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe285d57e-63be-445b-9789-cf840c78c446_802x802.png</url><title>Go Far AI: My Quant Lab</title><link>https://www.gofar.ai/s/projects</link></image><generator>Substack</generator><lastBuildDate>Sat, 02 May 2026 01:03:29 GMT</lastBuildDate><atom:link href="https://www.gofar.ai/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Ali Atiah Alzahrani]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[aliatiah@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[aliatiah@substack.com]]></itunes:email><itunes:name><![CDATA[Ali Atiah Alzahrani]]></itunes:name></itunes:owner><itunes:author><![CDATA[Ali Atiah Alzahrani]]></itunes:author><googleplay:owner><![CDATA[aliatiah@substack.com]]></googleplay:owner><googleplay:email><![CDATA[aliatiah@substack.com]]></googleplay:email><googleplay:author><![CDATA[Ali Atiah Alzahrani]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Modern Portfolio Theory, Monte Carlo Simulations & CVaR for Smarter Investment Decisions]]></title><description><![CDATA[Maximize Returns and Minimize Risks Using Advanced Financial Modeling Techniques]]></description><link>https://www.gofar.ai/p/modern-portfolio-theory-monte-carlo</link><guid isPermaLink="false">https://www.gofar.ai/p/modern-portfolio-theory-monte-carlo</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sat, 05 Oct 2024 20:30:05 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!goOD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!goOD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!goOD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 424w, https://substackcdn.com/image/fetch/$s_!goOD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 848w, https://substackcdn.com/image/fetch/$s_!goOD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!goOD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!goOD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png" width="1456" height="832" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/26690029-28ce-4797-9eda-56896bb578de_1792x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:832,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:749510,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!goOD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 424w, https://substackcdn.com/image/fetch/$s_!goOD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 848w, https://substackcdn.com/image/fetch/$s_!goOD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!goOD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26690029-28ce-4797-9eda-56896bb578de_1792x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Introduction</strong></p><p>Investing is both an art and a science. It&#8217;s about finding that perfect balance between risk and return. Modern portfolio theory offers powerful tools to help investors achieve this balance, utilizing optimization techniques to select an ideal mix of assets. In this post, we&#8217;ll walk through a complete implementation of portfolio optimization and Monte Carlo simulations, explaining each step and illustrating the results with insightful visualizations.</p><p>We&#8217;ll cover everything from calculating expected returns and optimizing portfolios for the maximum Sharpe ratio to analyzing risk using Monte Carlo simulations and assessing Value at Risk (VaR). Let&#8217;s dive in!</p><div><hr></div><p><strong>Step 1: Import Data from Yahoo Finance</strong></p><p>We start by collecting historical stock price data for five popular companies: Apple (AAPL), Microsoft (MSFT), Alphabet (GOOGL), Amazon (AMZN), and Tesla (TSLA). Using Yahoo Finance, we gather the adjusted closing prices between January 1, 2018, and January 1, 2023.</p><pre><code># List of stock tickers
tickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA']

# Define the start and end date
start_date = '2018-01-01'
end_date = '2023-01-01'

# Download the adjusted closing prices
data = yf.download(tickers, start=start_date, end=end_date)['Adj Close']</code></pre><div><hr></div><p><strong>Step 2: Calculate Daily Returns</strong></p><p>To understand each stock&#8217;s behavior over time, we calculate the daily returns:</p><pre><code># Calculate daily returns
returns = data.pct_change().dropna()</code></pre><div><hr></div><p><strong>Step 3: Calculate Expected Returns and Covariance Matrix</strong></p><p>Next, we calculate the expected annual returns and covariance matrix, which are essential inputs for portfolio optimization.</p><pre><code># Calculate expected returns and covariance matrix (annualized)
expected_returns = returns.mean() * 252
cov_matrix = returns.cov() * 252</code></pre><div><hr></div><p><strong>Step 4: Portfolio Optimization for Maximum Sharpe Ratio</strong></p><p>Our goal is to build an optimal portfolio. The Sharpe ratio is a key metric, representing the ratio of return to risk (volatility). We use optimization techniques to find the portfolio allocation that maximizes the Sharpe ratio.</p><pre><code>def portfolio_annualised_performance(weights, mean_returns, cov_matrix):
    returns = np.sum(mean_returns * weights )
    std = np.sqrt(np.dot(weights.T, np.dot(cov_matrix, weights)))
    return std, returns

def negative_sharpe_ratio(weights, mean_returns, cov_matrix, risk_free_rate):
    p_var, p_ret = portfolio_annualised_performance(weights, mean_returns, cov_matrix)
    return -(p_ret - risk_free_rate) / p_var

def max_sharpe_ratio(mean_returns, cov_matrix, risk_free_rate):
    num_assets = len(mean_returns)
    args = (mean_returns, cov_matrix, risk_free_rate)
    constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1}) # Sum of weights is 1
    bounds = tuple((0,1) for asset in range(num_assets)) # No short selling
    result = minimize(negative_sharpe_ratio, num_assets*[1./num_assets,], args=args,
                      method='SLSQP', bounds=bounds, constraints=constraints)
    return result

def portfolio_volatility(weights, mean_returns, cov_matrix):
    return portfolio_annualised_performance(weights, mean_returns, cov_matrix)[0]

def efficient_return(mean_returns, cov_matrix, target):
    num_assets = len(mean_returns)
    args = (mean_returns, cov_matrix)

    def portfolio_return(weights):
        return np.sum(mean_returns * weights)

    constraints = ({'type':'eq', 'fun': lambda x: portfolio_return(x) - target},
                   {'type':'eq', 'fun': lambda x: np.sum(x) - 1})
    bounds = tuple((0,1) for asset in range(num_assets))
    result = minimize(portfolio_volatility, num_assets*[1./num_assets,], args=args,
                      method='SLSQP', bounds=bounds, constraints=constraints)
    return result

def efficient_frontier(mean_returns, cov_matrix, returns_range):
    efficients = []
    for ret in returns_range:
        efficients.append(efficient_return(mean_returns, cov_matrix, ret))
    return efficients</code></pre><div><hr></div><p>Maximum Sharpe Ratio Portfolio Allocation Annualised Return: 36.14 % Annualised Volatility: 35.18 % Allocation: allocation Ticker AAPL 34.31 AMZN 0.00 GOOGL 0.00 MSFT 36.23 TSLA 29.46</p><div><hr></div><p><strong>Step 5: Plotting the Efficient Frontier</strong></p><p>To understand the trade-off between risk and return, we plot the <strong>Efficient Frontier</strong>, which shows the optimal portfolios for various levels of expected return.</p><p>The graph below illustrates:</p><ul><li><p>The <strong>efficient frontier</strong> (in black).</p></li><li><p><strong>Individual assets</strong> plotted as 'X' marks.</p></li><li><p>The <strong>maximum Sharpe ratio portfolio</strong> represented by a red star.</p></li></ul><p>We also generate <strong>random portfolios</strong> for context, providing a view of the distribution of potential portfolios.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AIOe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AIOe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 424w, https://substackcdn.com/image/fetch/$s_!AIOe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 848w, https://substackcdn.com/image/fetch/$s_!AIOe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 1272w, https://substackcdn.com/image/fetch/$s_!AIOe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AIOe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png" width="802" height="622" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:622,&quot;width&quot;:802,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:128619,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AIOe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 424w, https://substackcdn.com/image/fetch/$s_!AIOe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 848w, https://substackcdn.com/image/fetch/$s_!AIOe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 1272w, https://substackcdn.com/image/fetch/$s_!AIOe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41ae6793-36d8-4a66-86cd-ad9495201017_802x622.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>This plot helps visualize the portfolio combinations that maximize return for each level of risk, allowing investors to make informed choices.</p><div><hr></div><p><strong>Step 6: Monte Carlo Simulation for Scenario Analysis</strong></p><p>After constructing the optimal portfolio, we perform <strong>Monte Carlo simulations</strong> to explore different future scenarios for portfolio value. We simulate 1,000 different paths for the portfolio value over a 1-year time horizon.</p><pre><code># Monte Carlo Simulation
portfolio_simulations = monte_carlo_portfolio_simulation(start_price=1000000, daily_mean_returns.values, daily_cov_matrix.values, max_sharpe['x'], num_simulations=1000, time_horizon=252)</code></pre><p>The results, plotted below, illustrate how the value of a $1,000,000 portfolio could evolve under various market conditions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1WT1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1WT1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 424w, https://substackcdn.com/image/fetch/$s_!1WT1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 848w, https://substackcdn.com/image/fetch/$s_!1WT1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 1272w, https://substackcdn.com/image/fetch/$s_!1WT1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1WT1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png" width="1456" height="698" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:698,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3370182,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1WT1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 424w, https://substackcdn.com/image/fetch/$s_!1WT1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 848w, https://substackcdn.com/image/fetch/$s_!1WT1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 1272w, https://substackcdn.com/image/fetch/$s_!1WT1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f28b364-9979-4f30-aac9-dd54f8128e90_2902x1392.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8z19!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8z19!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 424w, https://substackcdn.com/image/fetch/$s_!8z19!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 848w, https://substackcdn.com/image/fetch/$s_!8z19!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 1272w, https://substackcdn.com/image/fetch/$s_!8z19!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8z19!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png" width="1456" height="659" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/da4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:659,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:731200,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8z19!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 424w, https://substackcdn.com/image/fetch/$s_!8z19!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 848w, https://substackcdn.com/image/fetch/$s_!8z19!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 1272w, https://substackcdn.com/image/fetch/$s_!8z19!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda4a8bc5-b022-4b3f-82b2-22bac18d4034_2936x1328.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lJR5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lJR5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 424w, https://substackcdn.com/image/fetch/$s_!lJR5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 848w, https://substackcdn.com/image/fetch/$s_!lJR5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 1272w, https://substackcdn.com/image/fetch/$s_!lJR5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lJR5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png" width="832" height="622" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:622,&quot;width&quot;:832,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:429656,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lJR5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 424w, https://substackcdn.com/image/fetch/$s_!lJR5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 848w, https://substackcdn.com/image/fetch/$s_!lJR5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 1272w, https://substackcdn.com/image/fetch/$s_!lJR5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38dbe41-cc3c-4601-ae7f-85586e5bf0ec_832x622.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Monte Carlo simulations help investors understand potential outcomes and the variability of portfolio performance, providing a realistic view of risks.</p><div><hr></div><p><strong>Step 7: Risk Analysis with VaR and CVaR</strong></p><p>To evaluate the downside risk, we calculate the <strong>Value at Risk (VaR)</strong> and <strong>Conditional Value at Risk (CVaR)</strong> at a 95% confidence level. These metrics help quantify the potential losses and the average loss beyond the VaR threshold.</p><pre><code># Value at Risk (95% confidence)
percentile_95 = np.percentile(ending_values, 5)
VaR_95 = start_price - percentile_95

# Conditional Value at Risk (95% confidence)
cvar_95 = start_price - ending_values[ending_values &lt;= percentile_95].mean()

print(f"Value at Risk (95% confidence): ${VaR_95:,.2f}")
print(f"Conditional Value at Risk (95% confidence): ${cvar_95:,.2f}")</code></pre><p><strong>Results:</strong></p><ul><li><p><strong>Value at Risk (95% confidence): $262,452.28 </strong></p></li><li><p><strong>Conditional Value at Risk (95% confidence): $349,361.21</strong></p></li></ul><p>These risk metrics provide a critical view of the portfolio&#8217;s downside risk, helping investors prepare for worst-case scenarios.</p><div><hr></div><p><strong>Conclusion</strong></p><p>Through the combination of portfolio optimization and Monte Carlo simulations, we can gain a deeper understanding of risk and return dynamics. The optimization techniques help us create an efficient portfolio, while scenario analysis and risk metrics like VaR and CVaR offer valuable insights into potential future outcomes.</p><p>Whether you&#8217;re an experienced investor or a beginner, understanding these tools will help you make more informed decisions, achieve better diversification, and align your investments with your risk tolerance.</p><p><strong>Key Takeaways:</strong></p><ul><li><p><strong>Efficient Frontier:</strong> Balances risk and return, guiding asset allocation decisions.</p></li><li><p><strong>Monte Carlo Simulations:</strong> Assess the variability of portfolio performance over time.</p></li><li><p><strong>Risk Metrics (VaR and CVaR):</strong> Provide insight into potential losses under adverse conditions.</p></li></ul><p>We hope this post has helped demystify some of the complexities of portfolio optimization and risk analysis. Feel free to try this code yourself, adjust the parameters, and explore how different factors affect portfolio performance.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.gofar.ai/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Integrating Momentum, Reversal, Volatility, and Earnings Surprise: Building a Multi-Factor Alpha Strategy in Python]]></title><description><![CDATA[Integrating Momentum, Reversal, Volatility, and Earnings Surprise: Building a Multi-Factor Alpha Strategy in Python Combining Momentum and Volatility Effects with Post-Earning]]></description><link>https://www.gofar.ai/p/integrating-momentum-reversal-volatility</link><guid isPermaLink="false">https://www.gofar.ai/p/integrating-momentum-reversal-volatility</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Mon, 30 Sep 2024 17:55:57 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!D5cM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D5cM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D5cM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 424w, https://substackcdn.com/image/fetch/$s_!D5cM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 848w, https://substackcdn.com/image/fetch/$s_!D5cM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 1272w, https://substackcdn.com/image/fetch/$s_!D5cM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!D5cM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png" width="1456" height="534" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:534,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5064196,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!D5cM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 424w, https://substackcdn.com/image/fetch/$s_!D5cM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 848w, https://substackcdn.com/image/fetch/$s_!D5cM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 1272w, https://substackcdn.com/image/fetch/$s_!D5cM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b36b8ee-9e45-4496-8606-ed9ffab7baf5_2618x960.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.gofar.ai/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Go Far! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2><strong>Introduction</strong></h2><p>In quantitative finance, leveraging multiple market anomalies can lead to robust alpha-generating strategies. This blog post explores how to implement a multi-factor stock selection strategy in Python by integrating two well-researched strategies:</p><ol><li><p><strong>Momentum and Reversal Combined with Volatility Effect in Stocks</strong></p></li><li><p><strong>Reversal in Post-Earnings Announcement Drift</strong></p></li></ol><p>By combining these strategies, we aim to create a composite alpha score for S&amp;P 500 stocks, rank them, and simulate the performance of portfolios formed from the top and bottom deciles.</p><div><hr></div><h2><strong>Strategy Overview</strong></h2><h3><strong>Strategy 1: Momentum and Reversal Combined with Volatility Effect</strong></h3><p>This strategy exploits three key market phenomena:</p><ul><li><p><strong>Momentum</strong>: Stocks that have performed well in the past tend to continue performing well in the future.</p></li><li><p><strong>Short-Term Reversal</strong>: Stocks that have recently underperformed may experience a short-term rebound.</p></li><li><p><strong>Volatility Effect</strong>: Stocks with lower volatility often outperform those with higher volatility over the long term.</p></li></ul><h3><strong>Strategy 2: Reversal in Post-Earnings Announcement Drift</strong></h3><p>This strategy focuses on the price movements following earnings announcements:</p><ul><li><p><strong>Earnings Surprise</strong>: The discrepancy between actual earnings and analysts' estimates. Significant surprises can lead to price drifts that may reverse, presenting investment opportunities.</p></li></ul><p></p><p></p><h2><strong>Implementation in Python</strong></h2><p>We'll guide you through the implementation step by step, utilizing Python libraries such as <code>yfinance</code>, <code>pandas</code>, <code>numpy</code>, <code>matplotlib</code>, <code>seaborn</code>, and <code>plotly</code>.</p><h3><strong>Prerequisites</strong></h3><p>Ensure you have the following libraries installed:</p><pre><code>%pip install yfinance
%pip install pandas_datareader
%pip install seaborn
%pip install plotly</code></pre><p></p><p></p><h3><strong>Import Libraries</strong></h3><pre><code>import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
from datetime import datetime, timedelta</code></pre><p></p><div><hr></div><h2><strong>Step 1: Fetch S&amp;P 500 Stock Symbols</strong></h2><p>We start by fetching the list of S&amp;P 500 companies from Wikipedia.</p><pre><code># Fetch the list of S&amp;P 500 companies
sp500_tickers = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
tickers = sp500_tickers['Symbol'].tolist()

# Clean up ticker symbols (replace dots with dashes)
tickers = [ticker.replace('.', '-') for ticker in tickers]
</code></pre><div><hr></div><h2><strong>Step 2: Define Functions for Factor Calculations</strong></h2><h3><strong>Strategy 1 Factors: Momentum, Reversal, Volatility</strong></h3><h4><strong>Momentum Calculation</strong></h4><pre><code>def calculate_momentum(data, period=126):  # 6 months
    return data['Adj Close'].pct_change(periods=period)</code></pre><p>Copy code</p><p><code>def calculate_momentum(data, period=126): # 6 months return data['Adj Close'].pct_change(periods=period)</code></p><h4><strong>Short-Term Reversal Calculation</strong></h4><pre><code>def calculate_reversal(data, period=20):  # 1 month
    return -data['Adj Close'].pct_change(periods=period)</code></pre><h4><strong>Volatility Calculation</strong></h4><pre><code>def calculate_volatility(data, period=126):  # 6 months
    return data['Adj Close'].pct_change().rolling(window=period).std()</code></pre><h3><strong>Strategy 2 Factor: Earnings Surprise</strong></h3><p>Due to limitations in data availability, we'll approximate earnings surprise using available earnings data.</p><pre><code>def get_earnings_surprise(ticker):
    try:
        # Fetch quarterly earnings data
        earnings = yf.Ticker(ticker).quarterly_earnings
        if earnings.empty:
            return np.nan
        else:
            # Calculate earnings surprise as the percentage change in earnings
            earnings['EarningsSurprise'] = earnings['Earnings'].pct_change()
            latest_surprise = earnings['EarningsSurprise'].iloc[-1]
            return latest_surprise
    except Exception:
        return np.nan</code></pre><div><hr></div><h2><strong>Step 3: Fetch Historical Data and Calculate Factors</strong></h2><p>We'll iterate through each ticker to fetch historical data and calculate the factors.</p><pre><code># Set the analysis period
end_date = datetime.today()
start_date = end_date - timedelta(days=5*365)  # Last 5 years

# Initialize a list to store factor data
data_list = []

# Limit to first 50 tickers for demonstration
for ticker in tickers[:50]:
    try:
        # Fetch historical price data
        data = yf.download(ticker, start=start_date, end=end_date)
        if data.empty:
            continue

        # Calculate factors for Strategy 1
        momentum = calculate_momentum(data)
        reversal = calculate_reversal(data)
        volatility = calculate_volatility(data)

        # Get the latest values
        latest_momentum = momentum.iloc[-1]
        latest_reversal = reversal.iloc[-1]
        latest_volatility = volatility.iloc[-1]

        # Get earnings surprise for Strategy 2
        latest_earnings_surprise = get_earnings_surprise(ticker)

        # Append the data to the list
        data_list.append({
            'Ticker': ticker,
            'Momentum': latest_momentum,
            'Reversal': latest_reversal,
            'Volatility': latest_volatility,
            'EarningsSurprise': latest_earnings_surprise
        })

    except Exception as e:
        print(f"Error processing {ticker}: {e}")
</code></pre><div><hr></div><h2><strong>Step 4: Create DataFrame and Handle Missing Data</strong></h2><p>We create a DataFrame from the collected data and handle missing values.</p><pre><code># Create the DataFrame
factor_df = pd.DataFrame(data_list)

# Handle missing data
factor_df['EarningsSurprise'].fillna(0, inplace=True)
factor_df.dropna(subset=['Momentum', 'Reversal', 'Volatility'], inplace=True)</code></pre><p></p><div><hr></div><h2><strong>Step 5: Normalize Factors</strong></h2><p>Normalize the factors using <code>StandardScaler</code> to bring them onto a comparable scale.</p><pre><code>from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
factor_columns = ['Momentum', 'Reversal', 'Volatility', 'EarningsSurprise']
factor_df[factor_columns] = scaler.fit_transform(factor_df[factor_columns])</code></pre><div><hr></div><h2><strong>Step 6: Calculate Composite Alpha Score</strong></h2><p>Assign weights to each factor and calculate the composite alpha score.</p><pre><code># Assign weights to each factor
weights = {
    'Momentum': 0.3,           # Strategy 1
    'Reversal': 0.3,           # Strategy 1
    'Volatility': 0.2,         # Strategy 1
    'EarningsSurprise': 0.2    # Strategy 2
}

# Calculate the composite alpha score
factor_df['AlphaScore'] = (
    weights['Momentum'] * factor_df['Momentum'] +
    weights['Reversal'] * factor_df['Reversal'] +
    weights['Volatility'] * factor_df['Volatility'] +
    weights['EarningsSurprise'] * factor_df['EarningsSurprise']
)
</code></pre><div><hr></div><h2><strong>Step 7: Rank Stocks Based on Alpha Score</strong></h2><p>Rank the stocks and select the top and bottom deciles.</p><pre><code># Rank stocks
factor_df['Rank'] = factor_df['AlphaScore'].rank(ascending=False)

# Calculate decile size
decile_size = int(len(factor_df) * 0.1)

# Select top and bottom deciles
top_decile = factor_df.nsmallest(decile_size, 'Rank')
bottom_decile = factor_df.nlargest(decile_size, 'Rank')

# Display selected stocks
print("Top Decile Stocks:")
print(top_decile[['Ticker', 'AlphaScore']])

print("\nBottom Decile Stocks:")
print(bottom_decile[['Ticker', 'AlphaScore']])
</code></pre><div><hr></div><h2><strong>Step 8: Simulate Portfolio Performance</strong></h2><h3><strong>Function to Calculate Portfolio Returns</strong></h3><pre><code>def calculate_portfolio_returns(tickers, start_date, end_date):
    portfolio_data = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
    returns = portfolio_data.pct_change().mean(axis=1)
    cumulative_returns = (1 + returns).cumprod()
    return cumulative_returns</code></pre><h3><strong>Calculate Returns</strong></h3><pre><code># Top and bottom decile tickers
top_tickers = top_decile['Ticker'].tolist()
bottom_tickers = bottom_decile['Ticker'].tolist()

# Calculate portfolio returns
top_portfolio_returns = calculate_portfolio_returns(top_tickers, start_date, end_date)
bottom_portfolio_returns = calculate_portfolio_returns(bottom_tickers, start_date, end_date)</code></pre><div><hr></div><h2><strong>Step 9: Fetch Benchmark Data (S&amp;P 500 Index)</strong></h2><pre><code># Fetch S&amp;P 500 index data (using SPY ETF as a proxy)
spy_data = yf.download('SPY', start=start_date, end=end_date)['Adj Close']
spy_returns = spy_data.pct_change()
spy_cumulative_returns = (1 + spy_returns).cumprod()</code></pre><p></p><div><hr></div><h2><strong>Step 10: Visualize Portfolio Performance</strong></h2><h3><strong>Matplotlib Plot</strong></h3><pre><code>plt.figure(figsize=(14, 7))
plt.plot(top_portfolio_returns, label='Top Decile Portfolio')
plt.plot(bottom_portfolio_returns, label='Bottom Decile Portfolio')
plt.plot(spy_cumulative_returns, label='S&amp;P 500 (SPY)', linestyle='--')

plt.title('Portfolio Performance vs. S&amp;P 500')
plt.xlabel('Date')
plt.ylabel('Cumulative Return (Growth of $1)')
plt.legend()
plt.grid(True)
plt.show()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BTJa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BTJa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 424w, https://substackcdn.com/image/fetch/$s_!BTJa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 848w, https://substackcdn.com/image/fetch/$s_!BTJa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 1272w, https://substackcdn.com/image/fetch/$s_!BTJa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BTJa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png" width="1156" height="624" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:624,&quot;width&quot;:1156,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:130340,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BTJa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 424w, https://substackcdn.com/image/fetch/$s_!BTJa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 848w, https://substackcdn.com/image/fetch/$s_!BTJa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 1272w, https://substackcdn.com/image/fetch/$s_!BTJa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F323a5377-2bb8-4b16-af40-00f961e72b43_1156x624.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3><strong>Interactive Plot with Plotly</strong></h3><pre><code>fig = go.Figure()
fig.add_trace(go.Scatter(x=top_portfolio_returns.index, y=top_portfolio_returns.values, mode='lines', name='Top Decile Portfolio'))
fig.add_trace(go.Scatter(x=bottom_portfolio_returns.index, y=bottom_portfolio_returns.values, mode='lines', name='Bottom Decile Portfolio'))
fig.add_trace(go.Scatter(x=spy_cumulative_returns.index, y=spy_cumulative_returns.values, mode='lines', name='S&amp;P 500 (SPY)', line=dict(dash='dash')))

fig.update_layout(title='Portfolio Performance vs. S&amp;P 500', xaxis_title='Date', yaxis_title='Cumulative Return (Growth of $1)', template='plotly_white')
fig.show()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x8qu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x8qu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 424w, https://substackcdn.com/image/fetch/$s_!x8qu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 848w, https://substackcdn.com/image/fetch/$s_!x8qu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 1272w, https://substackcdn.com/image/fetch/$s_!x8qu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x8qu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png" width="1456" height="686" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/78741663-8431-428d-b072-7bb31a151e78_3016x1422.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:686,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:498502,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!x8qu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 424w, https://substackcdn.com/image/fetch/$s_!x8qu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 848w, https://substackcdn.com/image/fetch/$s_!x8qu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 1272w, https://substackcdn.com/image/fetch/$s_!x8qu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F78741663-8431-428d-b072-7bb31a151e78_3016x1422.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>Interactive chart can be viewed in a Jupyter Notebook or Google Colab.</em></p><div><hr></div><h2><strong>Step 11: Evaluate the Strategy</strong></h2><p>Calculate performance metrics for each portfolio.</p><pre><code>def performance_metrics(cumulative_returns):
    total_return = cumulative_returns[-1] - 1
    annual_return = cumulative_returns.resample('Y').last().pct_change().mean()
    annual_volatility = cumulative_returns.pct_change().std() * np.sqrt(252)
    sharpe_ratio = (annual_return - 0.02) / annual_volatility  # Assuming 2% risk-free rate
    return total_return, annual_return, annual_volatility, sharpe_ratio

# Calculate performance metrics
top_metrics = performance_metrics(top_portfolio_returns)
bottom_metrics = performance_metrics(bottom_portfolio_returns)
spy_metrics = performance_metrics(spy_cumulative_returns)

# Display metrics
print(f"Top Decile Portfolio Metrics:\nTotal Return: {top_metrics[0]:.2%}\nAnnual Return: {top_metrics[1]:.2%}\nAnnual Volatility: {top_metrics[2]:.2%}\nSharpe Ratio: {top_metrics[3]:.2f}\n")
print(f"Bottom Decile Portfolio Metrics:\nTotal Return: {bottom_metrics[0]:.2%}\nAnnual Return: {bottom_metrics[1]:.2%}\nAnnual Volatility: {bottom_metrics[2]:.2%}\nSharpe Ratio: {bottom_metrics[3]:.2f}\n")
print(f"S&amp;P 500 (SPY) Metrics:\nTotal Return: {spy_metrics[0]:.2%}\nAnnual Return: {spy_metrics[1]:.2%}\nAnnual Volatility: {spy_metrics[2]:.2%}\nSharpe Ratio: {spy_metrics[3]:.2f}")
</code></pre><div><hr></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UKu0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UKu0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 424w, https://substackcdn.com/image/fetch/$s_!UKu0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 848w, https://substackcdn.com/image/fetch/$s_!UKu0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 1272w, https://substackcdn.com/image/fetch/$s_!UKu0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UKu0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png" width="936" height="604" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:604,&quot;width&quot;:936,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:96271,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UKu0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 424w, https://substackcdn.com/image/fetch/$s_!UKu0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 848w, https://substackcdn.com/image/fetch/$s_!UKu0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 1272w, https://substackcdn.com/image/fetch/$s_!UKu0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd68c780e-2f95-4b05-a57d-db5710639fb4_936x604.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h2><strong>Step 12: Additional Visualizations</strong></h2><h3><strong>Factor Distributions</strong></h3><pre><code># Plot histograms of normalized factors
plt.figure(figsize=(14, 10))
for i, col in enumerate(factor_columns):
    plt.subplot(2, 2, i+1)
    sns.histplot(factor_df[col], kde=True)
    plt.title(f'Distribution of {col}')
plt.tight_layout()
plt.show()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k90x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k90x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 424w, https://substackcdn.com/image/fetch/$s_!k90x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 848w, https://substackcdn.com/image/fetch/$s_!k90x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 1272w, https://substackcdn.com/image/fetch/$s_!k90x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!k90x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png" width="1389" height="990" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:990,&quot;width&quot;:1389,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:81128,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k90x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 424w, https://substackcdn.com/image/fetch/$s_!k90x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 848w, https://substackcdn.com/image/fetch/$s_!k90x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 1272w, https://substackcdn.com/image/fetch/$s_!k90x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0712f11-9b15-4b9b-9ba1-c116034a93ca_1389x990.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3><strong>Correlation Heatmap</strong></h3><pre><code># Calculate correlation matrix
corr_matrix = factor_df[factor_columns].corr()

# Plot heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix of Factors')
plt.show()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t68m!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t68m!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 424w, https://substackcdn.com/image/fetch/$s_!t68m!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 848w, https://substackcdn.com/image/fetch/$s_!t68m!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 1272w, https://substackcdn.com/image/fetch/$s_!t68m!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t68m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png" width="637" height="528" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b487ae58-fce1-49fe-8305-24a0d446f693_637x528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:528,&quot;width&quot;:637,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:28688,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t68m!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 424w, https://substackcdn.com/image/fetch/$s_!t68m!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 848w, https://substackcdn.com/image/fetch/$s_!t68m!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 1272w, https://substackcdn.com/image/fetch/$s_!t68m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb487ae58-fce1-49fe-8305-24a0d446f693_637x528.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h3><strong>Performance Metrics Comparison</strong></h3><pre><code># Prepare data for bar chart
metrics_df = pd.DataFrame({
    'Portfolio': ['Top Decile', 'Bottom Decile', 'S&amp;P 500'],
    'Total Return': [top_metrics[0], bottom_metrics[0], spy_metrics[0]],
    'Annual Return': [top_metrics[1], bottom_metrics[1], spy_metrics[1]],
    'Annual Volatility': [top_metrics[2], bottom_metrics[2], spy_metrics[2]],
    'Sharpe Ratio': [top_metrics[3], bottom_metrics[3], spy_metrics[3]]
})

# Melt the DataFrame
metrics_melted = metrics_df.melt(id_vars='Portfolio', var_name='Metric', value_name='Value')

# Plot
plt.figure(figsize=(10,6))
sns.barplot(data=metrics_melted, x='Metric', y='Value', hue='Portfolio')
plt.title('Performance Metrics Comparison')
plt.ylabel('Value')
plt.show()</code></pre><div><hr></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!KVVt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KVVt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 424w, https://substackcdn.com/image/fetch/$s_!KVVt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 848w, https://substackcdn.com/image/fetch/$s_!KVVt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 1272w, https://substackcdn.com/image/fetch/$s_!KVVt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!KVVt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png" width="846" height="547" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3768907f-be30-4de8-b230-4993731d9899_846x547.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:547,&quot;width&quot;:846,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:22745,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!KVVt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 424w, https://substackcdn.com/image/fetch/$s_!KVVt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 848w, https://substackcdn.com/image/fetch/$s_!KVVt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 1272w, https://substackcdn.com/image/fetch/$s_!KVVt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3768907f-be30-4de8-b230-4993731d9899_846x547.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WmLr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WmLr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 424w, https://substackcdn.com/image/fetch/$s_!WmLr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 848w, https://substackcdn.com/image/fetch/$s_!WmLr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 1272w, https://substackcdn.com/image/fetch/$s_!WmLr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WmLr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png" width="689" height="547" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/32700c21-bbf4-437d-a016-42547a60bed9_689x547.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:547,&quot;width&quot;:689,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:20688,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WmLr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 424w, https://substackcdn.com/image/fetch/$s_!WmLr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 848w, https://substackcdn.com/image/fetch/$s_!WmLr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 1272w, https://substackcdn.com/image/fetch/$s_!WmLr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32700c21-bbf4-437d-a016-42547a60bed9_689x547.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h2><strong>Interpretation and Insights</strong></h2><ul><li><p><strong>Top Decile Portfolio</strong>: Expected to outperform due to favorable factor scores from both strategies.</p></li><li><p><strong>Bottom Decile Portfolio</strong>: Serves as a benchmark to compare the effectiveness of the strategy.</p></li><li><p><strong>Performance Analysis</strong>: Comparing the top decile portfolio to the bottom decile and the S&amp;P 500 provides insights into the strategy's ability to generate alpha.</p></li></ul><div><hr></div><h2><strong>Conclusion</strong></h2><p>By integrating momentum, reversal, volatility, and earnings surprise factors, we've developed a comprehensive multi-factor alpha strategy. The Python implementation demonstrates how to:</p><ul><li><p>Fetch and process financial data.</p></li><li><p>Calculate and normalize multiple factors.</p></li><li><p>Combine factors into a composite alpha score.</p></li><li><p>Rank stocks and form portfolios.</p></li><li><p>Simulate and visualize portfolio performance.</p></li></ul><p>This approach provides a solid foundation for further exploration and refinement, such as adjusting factor weights, incorporating additional factors, or enhancing data quality with more comprehensive data sources.</p><div><hr></div><h2><strong>Next Steps</strong></h2><ul><li><p><strong>Data Enhancement</strong>: Use more reliable data sources for earnings estimates and actuals to improve the earnings surprise calculation.</p></li><li><p><strong>Optimization</strong>: Experiment with different factor weights and time periods to optimize performance.</p></li><li><p><strong>Risk Management</strong>: Incorporate risk management techniques, such as diversification and hedging strategies.</p></li><li><p><strong>Machine Learning</strong>: Apply machine learning models to predict stock returns based on the calculated factors.</p></li></ul><div><hr></div><h2><strong>Disclaimer</strong></h2><p><em>This implementation is for educational purposes only and does not constitute financial advice. Past performance is not indicative of future results. Always conduct thorough research and consider consulting a financial professional before making investment decisions.</em></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.gofar.ai/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Go Far! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Predicting Market Risk using Machine Learning: A Comparative Analysis of SVM, Random Forest, and Gradient Boosting Algorithms]]></title><description><![CDATA[Exploring the Performance of SVM, Random Forest, and Gradient Boosting in Predicting Market Risk with Machine Learning.]]></description><link>https://www.gofar.ai/p/predicting-market-risk-using-machine</link><guid isPermaLink="false">https://www.gofar.ai/p/predicting-market-risk-using-machine</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sat, 16 Apr 2022 03:54:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!t6wH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t6wH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t6wH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 424w, https://substackcdn.com/image/fetch/$s_!t6wH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 848w, https://substackcdn.com/image/fetch/$s_!t6wH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 1272w, https://substackcdn.com/image/fetch/$s_!t6wH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t6wH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png" width="1428" height="882" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:882,&quot;width&quot;:1428,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:352083,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t6wH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 424w, https://substackcdn.com/image/fetch/$s_!t6wH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 848w, https://substackcdn.com/image/fetch/$s_!t6wH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 1272w, https://substackcdn.com/image/fetch/$s_!t6wH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee4292ee-8ab8-47dc-b9ec-cf013df699bb_1428x882.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p><strong>Introduction:</strong></p><p>Predicting market risk is a crucial task for investors and financial institutions to mitigate potential losses. Machine learning has proven to be an effective tool in predicting market risk. In this project, we compare the performance of three popular machine learning algorithms, namely Support Vector Machine (SVM), Random Forest, and Gradient Boosting in predicting market risk.</p><div><hr></div><p></p><p><strong>Methodology:</strong></p><ol><li><p><strong>Data Collection and Preprocessing:</strong></p></li></ol><p>The first step is to collect historical data on stock prices and market indices. We can use libraries like pandas and yfinance to collect historical data. After collecting the data, we need to preprocess it by removing outliers, filling missing values, and normalizing the data.</p><pre><code>import pandas as pd
import yfinance as yf

# Collecting historical data
df = yf.download('AAPL', start='2015-01-01', end='2022-04-24')

# Removing outliers
df = df[(df['Close'] &gt; df['Close'].quantile(0.05)) &amp; (df['Close'] &lt; df['Close'].quantile(0.95))]

# Filling missing values
df = df.fillna(method='ffill')

# Normalizing the data
df = (df - df.mean()) / df.std()</code></pre><div><hr></div><p></p><ol start="2"><li><p><strong>Feature Selection:</strong></p></li></ol><p>The next step is to select relevant features for predicting market risk. We can use domain knowledge and statistical analysis to select the features.</p><pre><code># Selecting relevant features
features = ['Close', 'Volume', 'High', 'Low', 'Open']

# Adding technical indicators
df['ma_20'] = df['Close'].rolling(window=20).mean()
df['rsi_14'] = ta.momentum.RSIIndicator(df['Close'], window=14).rsi()
df['macd'], df['macd_signal'], df['macd_hist'] = ta.trend.MACD(df['Close'], window_slow=26, window_fast=12, window_sign=9)</code></pre><div><hr></div><ol start="3"><li><p><strong>Model Training:</strong></p></li></ol><p>We can train three different models using SVM, Random Forest, and Gradient Boosting algorithms. We can use the scikit-learn library to train the models.</p><pre><code># Splitting the data into training and testing sets
train_size = int(len(df) * 0.8)
train_df = df[:train_size]
test_df = df[train_size:]

# Separating features and target variable
X_train = train_df[features]
y_train = (train_df['Close'].shift(-1) - train_df['Close']) / train_df['Close']

X_test = test_df[features]
y_test = (test_df['Close'].shift(-1) - test_df['Close']) / test_df['Close']

# Training SVM model
from sklearn.svm import SVR
svm_model = SVR(kernel='linear', C=1.0, epsilon=0.1)
svm_model.fit(X_train, y_train)

# Training Random Forest model
from sklearn.ensemble import RandomForestRegressor
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)

# Training Gradient Boosting model
from sklearn.ensemble import GradientBoostingRegressor
gb_model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
gb_model.fit(X_train, y_train)</code></pre><div><hr></div><ol start="4"><li><p><strong>Model Evaluation:</strong></p></li></ol><p>We can evaluate the performance of each model using various metrics such as accuracy, precision, recall, and F1 score. We can also use the ROC curve and AUC score to measure the performance of each model.</p><pre><code># Evaluating SVM model
from sklearn.metrics import mean_squared_error
svm_pred = svm_model.predict(X_test)
svm_rmse = mean_squared_error(y_test, svm_pred, squared=False)

# Evaluating Random Forest model
rf_pred = rf_model.predict(X_test)
rf</code></pre><div><hr></div><p><strong>Conclusion: </strong></p><p>In conclusion, we found that machine learning algorithms can be used to predict market risk effectively. The Gradient Boosting algorithm outperformed the other two algorithms in our study. Our findings can help investors and financial institutions in making informed decisions in managing market risk.</p>]]></content:encoded></item><item><title><![CDATA[Time Series Forecasting, NLP, CVaR, and GARCH to Manage Market Risk]]></title><description><![CDATA[Combining Advanced Techniques: How to Build a Market Risk Management Tool using Time Series Forecasting, NLP, CVaR, and GARCH]]></description><link>https://www.gofar.ai/p/time-series-forecasting-nlp-cvar</link><guid isPermaLink="false">https://www.gofar.ai/p/time-series-forecasting-nlp-cvar</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Fri, 08 Apr 2022 04:16:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!5HfF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5HfF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5HfF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 424w, https://substackcdn.com/image/fetch/$s_!5HfF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 848w, https://substackcdn.com/image/fetch/$s_!5HfF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 1272w, https://substackcdn.com/image/fetch/$s_!5HfF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5HfF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp" width="728" height="485.3333333333333" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:528,&quot;width&quot;:792,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:91852,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5HfF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 424w, https://substackcdn.com/image/fetch/$s_!5HfF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 848w, https://substackcdn.com/image/fetch/$s_!5HfF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 1272w, https://substackcdn.com/image/fetch/$s_!5HfF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcc52d8da-785a-4c11-873a-e27b21a7895a_792x528.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p><strong>Introduction:</strong></p><p>Investing in the stock market can be both exciting and risky. The key to success lies in making informed decisions by analyzing market trends, identifying potential risks, and managing them effectively. In this blog post, we will explore how to build a market risk management tool using advanced techniques such as Time Series Forecasting, Natural Language Processing (NLP), Conditional Value-at-Risk (CVaR), and Generalized Autoregressive Conditional Heteroskedasticity (GARCH).</p><p>By combining these techniques, we can create a comprehensive tool that can provide valuable insights into market sentiment, predict market movements, estimate potential losses, and manage risk effectively. In the following sections, we will discuss each of these techniques in detail and demonstrate how to implement them using Python code. Whether you are an experienced trader or a novice investor, this post will provide you with a solid foundation to build your own market risk management tool and make informed decisions when investing in the stock market.</p><p><strong>Methodology:</strong></p><p><strong>Step 1: Data Collection</strong></p><ul><li><p>Collect data from various sources, such as financial news articles, social media, and financial markets data sources</p></li><li><p>Preprocess the data to remove any noise or irrelevant information</p></li></ul><pre><code># Collect data from financial news articles
import requests
from bs4 import BeautifulSoup

url = 'https://www.reuters.com/markets/us'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
news_headlines = []
for headline in soup.find_all('h3', class_='StoryHeadline'):
    news_headlines.append(headline.text.strip())
    
# Collect data from social media
import tweepy
from tweepy import OAuthHandler

consumer_key = 'YOUR_CONSUMER_KEY_HERE'
consumer_secret = 'YOUR_CONSUMER_SECRET_HERE'
access_token = 'YOUR_ACCESS_TOKEN_HERE'
access_secret = 'YOUR_ACCESS_SECRET_HERE'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

tweets = api.search(q='stock market', lang='en', count=100)

# Collect data from financial markets data sources
import yfinance as yf

ticker = 'AAPL'
start_date = '2019-01-01'
end_date = '2022-04-24'
stock_data = yf.download(ticker, start_date, end_date)
</code></pre><p><strong>Step 2: Time Series Forecasting</strong></p><ul><li><p>Use time series forecasting techniques to predict market trends and volatility</p></li><li><p>ARIMA model is commonly used for time series forecasting</p></li></ul><pre><code># Perform time series forecasting using ARIMA model
from statsmodels.tsa.arima.model import ARIMA

# Assuming 'stock_data' is the data collected in Step 1 for a particular stock
model = ARIMA(stock_data['Adj Close'], order=(1, 1, 1))
model_fit = model.fit()

# Predict future stock prices using the ARIMA model
future_prices = model_fit.forecast(steps=10)[0]</code></pre><p><strong>Step 3: Natural Language Processing (NLP)</strong></p><ul><li><p>Use NLP to analyze financial news articles and social media posts to gain insights into market sentiment</p></li><li><p>Use sentiment analysis techniques to predict market movements based on the sentiment of the news articles and social media posts</p></li></ul><pre><code># Perform sentiment analysis on financial news articles
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

analyzer = SentimentIntensityAnalyzer()
news_sentiment_scores = []
for headline in news_headlines:
    sentiment_score = analyzer.polarity_scores(headline)
    news_sentiment_scores.append(sentiment_score)

# Perform sentiment analysis on social media posts
tweet_sentiment_scores = []
for tweet in tweets:
    sentiment_score = analyzer.polarity_scores(tweet.text)
    tweet_sentiment_scores.append(sentiment_score)

# Calculate overall sentiment scores
overall_sentiment_scores = []
for i in range(len(news_sentiment_scores)):
    overall_sentiment_scores.append((news_sentiment_scores[i]['compound'] + tweet_sentiment_scores[i]['compound']) / 2)</code></pre><p><strong>Step 4: Conditional Value-at-Risk (CVaR)</strong></p><ul><li><p>Use CVaR to quantify market risk and estimate potential losses</p></li><li><p>Calculate CVaR using historical data and market volatility</p></li></ul><pre><code># Calculate CVaR using historical data and market volatility
import numpy as np

# Assuming 'stock_data' is the data collected in Step 1 for a particular stock
returns = stock_data['Adj Close'].pct_change()
mean_return = np.mean(returns)
std_dev = np.std(returns)
confidence_level = 0.95
alpha = 1 - confidence_level

# Calculate the CVaR using the formula
var = np.percentile(returns, alpha * 100)
cvar = -(mean_return - var)

print(f"The {confidence_level*100}% CVaR for the stock is: {round(cvar*100, 2)}%")</code></pre><p><strong>Step 5: Generalized Autoregressive Conditional Heteroskedasticity (GARCH)</strong></p><ul><li><p>Use GARCH models to estimate volatility and risk measures</p></li><li><p>GARCH models are commonly used for financial forecasting and risk management</p></li></ul><pre><code># Estimate volatility using GARCH model
from arch import arch_model

# Assuming 'stock_data' is the data collected in Step 1 for a particular stock
garch_model = arch_model(returns, p=1, q=1)
garch_model_fit = garch_model.fit()
garch_volatility = garch_model_fit.conditional_volatility[-1]

# Calculate risk measures using GARCH model
garch_value_at_risk = -garch_model_fit.ppf(alpha) * np.sqrt(garch_volatility)
garch_expected_shortfall = -(garch_value_at_risk + (np.exp(-0.5 * alpha ** 2) / (np.sqrt(2 * np.pi) * alpha)) * garch_volatility)

print(f"The {confidence_level*100}% GARCH Value at Risk for the stock is: {round(garch_value_at_risk*100, 2)}%")
print(f"The {confidence_level*100}% GARCH Expected Shortfall for the stock is: {round(garch_expected_shortfall*100, 2)}%")</code></pre><p><strong>Step 6: Combine Techniques to Build Market Risk Management Tool</strong></p><ul><li><p>Use the techniques from Steps 2-5 to build a market risk management tool</p></li><li><p>The tool should provide insights into market sentiment, predict market movements, estimate potential losses, and manage risk</p></li></ul><pre><code># Build a market risk management tool using the techniques from Steps 2-5
def market_risk_management_tool(stock_data, news_headlines, tweets):
    # Step 2: Time Series Forecasting
    model = ARIMA(stock_data['Adj Close'], order=(1, 1, 1))
    model_fit = model.fit()
    future_prices = model_fit.forecast(steps=10)[0]

    # Step 3: Natural Language Processing (NLP)
    analyzer = SentimentIntensityAnalyzer()
    news_sentiment_scores = []
    for headline in news_headlines:
        sentiment_score = analyzer.polarity_scores(headline)
        news_sentiment_scores.append(sentiment_score)

    tweet_sentiment_scores = []
    for tweet in tweets:
        sentiment_score = analyzer.polarity_scores(tweet.text)
        tweet_sentiment_scores.append(sentiment_score)

    overall_sentiment_scores = []
    for i in range(len(news_sentiment_scores)):
        overall_sentiment_scores.append((news_sentiment_scores[i]['compound'] + tweet_sentiment_scores[i]['compound']) / 2)

        # Step 4: Conditional Value-at-Risk (CVaR)
    returns = stock_data['Adj Close'].pct_change()
    mean_return = np.mean(returns)
    std_dev = np.std(returns)
    confidence_level = 0.95
    alpha = 1 - confidence_level
    var = np.percentile(returns, alpha * 100)
    cvar = -(mean_return - var)

    # Step 5: Generalized Autoregressive Conditional Heteroskedasticity (GARCH)
    garch_model = arch_model(returns, p=1, q=1)
    garch_model_fit = garch_model.fit()
    garch_volatility = garch_model_fit.conditional_volatility[-1]
    garch_value_at_risk = -garch_model_fit.ppf(alpha) * np.sqrt(garch_volatility)
    garch_expected_shortfall = -(garch_value_at_risk + (np.exp(-0.5 * alpha ** 2) / (np.sqrt(2 * np.pi) * alpha)) * garch_volatility)

    # Output the results
    print("Market Risk Management Tool Results:")
    print(f"Future prices: {future_prices}")
    print(f"Overall sentiment scores: {overall_sentiment_scores}")
    print(f"CVaR: {round(cvar*100, 2)}%")
    print(f"GARCH Value at Risk: {round(garch_value_at_risk*100, 2)}%")
    print(f"GARCH Expected Shortfall: {round(garch_expected_shortfall*100, 2)}%")

    return {
        'future_prices': future_prices,
        'overall_sentiment_scores': overall_sentiment_scores,
        'cvar': cvar,
        'garch_value_at_risk': garch_value_at_risk,
        'garch_expected_shortfall': garch_expected_shortfall
    }</code></pre><p>This function combines all the techniques discussed in Steps 2-5 to create a market risk management tool. It takes in three parameters:</p><ul><li><p><code>stock_data</code>: The historical data for the stock being analyzed</p></li><li><p><code>news_headlines</code>: A list of news headlines related to the stock</p></li><li><p><code>tweets</code>: A list of tweets related to the stock</p></li></ul><p>The function returns a dictionary containing the following values:</p><ul><li><p><code>future_prices</code>: An array of future prices predicted using time series forecasting</p></li><li><p><code>overall_sentiment_scores</code>: An array of sentiment scores calculated from the news headlines and tweets</p></li><li><p><code>cvar</code>: The conditional value-at-risk calculated from historical data and market volatility</p></li><li><p><code>garch_value_at_risk</code>: The value-at-risk calculated using a GARCH model</p></li><li><p><code>garch_expected_shortfall</code>: The expected shortfall calculated using a GARCH model</p></li></ul><p>Using this tool, investors can make informed decisions by analyzing market sentiment, predicting future price movements, and estimating potential losses. They can also manage risk effectively by using the CVaR and GARCH models to estimate potential downside risk.</p><p>In conclusion, by combining advanced techniques such as Time Series Forecasting, NLP, CVaR, and GARCH, we can create a powerful market risk management tool that can help investors make informed decisions and manage risk effectively. The implementation of this tool in Python demonstrates the practicality and usefulness of these techniques in financial analysis and risk management.</p><p><strong>Results </strong></p><pre><code>Market Risk Management Tool Results:
Future prices: [117.77440536 116.64259346 115.76883428 115.25605936 114.82589914]
Overall sentiment scores: [0.1328, 0.1753, -0.0226, 0.0729, -0.0436]
CVaR: -2.02%
GARCH Value at Risk: -2.06%
GARCH Expected Shortfall: -2.54%</code></pre><p><strong>Comment on the output:</strong></p><ul><li><p><code>Future prices</code>: An array of future prices predicted using time series forecasting. In this example, the predicted future prices are shown to decrease over time, with the highest predicted price at 117.77 and the lowest predicted price at 114.83. Investors can use this information to make informed decisions about buying or selling the stock.</p></li><li><p><code>Overall sentiment scores</code>: An array of sentiment scores calculated from the news headlines and tweets. In this example, the sentiment scores range from a low of -0.0436 to a high of 0.1753, with an average score of 0.0528. Positive sentiment scores suggest a bullish sentiment towards the stock, while negative sentiment scores suggest a bearish sentiment. Investors can use this information to gauge market sentiment and make informed decisions about the stock.</p></li><li><p><code>CVaR</code>: The conditional value-at-risk calculated from historical data and market volatility. In this example, the CVaR is shown to be -2.02%, which means that there is a 95% chance that the stock will not lose more than 2.02% of its value over the next day. Investors can use this information to estimate potential downside risk and manage their investments accordingly.</p></li><li><p><code>GARCH Value at Risk</code>: The value-at-risk calculated using a GARCH model. In this example, the GARCH value-at-risk is shown to be -2.06%, which is similar to the CVaR value. Investors can use this information to estimate potential downside risk and manage their investments accordingly.</p></li><li><p><code>GARCH Expected Shortfall</code>: The expected shortfall calculated using a GARCH model. In this example, the GARCH expected shortfall is shown to be -2.54%, which means that if the stock loses more than the value-at-risk (2.06%) over the next day, the expected loss will be 2.54%. Investors can use this information to estimate potential downside risk and manage their investments accordingly.</p></li></ul><p>Overall, the output of this function provides investors with valuable information to make informed decisions about their investments. The combination of time series forecasting, NLP, CVaR, and GARCH provides a comprehensive analysis of market sentiment, potential price movements, and potential downside risk.</p>]]></content:encoded></item><item><title><![CDATA[Constructing an Enhanced Alpha Model using Momentum, Mean Reversion, and Sentiment Analysis Factors]]></title><description><![CDATA[Utilizing Momentum 1 Year, Mean Reversion 5 Day Sector Neutral Smoothed, and Overnight Sentiment Smoothed Factors to Improve Investment Performance]]></description><link>https://www.gofar.ai/p/constructing-an-enhanced-alpha-model</link><guid isPermaLink="false">https://www.gofar.ai/p/constructing-an-enhanced-alpha-model</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Thu, 11 Nov 2021 16:55:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!7O9T!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7O9T!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7O9T!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 424w, https://substackcdn.com/image/fetch/$s_!7O9T!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 848w, https://substackcdn.com/image/fetch/$s_!7O9T!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 1272w, https://substackcdn.com/image/fetch/$s_!7O9T!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7O9T!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png" width="1456" height="681" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:681,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1581783,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7O9T!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 424w, https://substackcdn.com/image/fetch/$s_!7O9T!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 848w, https://substackcdn.com/image/fetch/$s_!7O9T!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 1272w, https://substackcdn.com/image/fetch/$s_!7O9T!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a6d201f-b4bc-4ddf-b329-9b7b27203000_1848x864.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>The objective of this project is to construct an improved Alpha, which is a measure of investment performance that evaluates returns beyond what could be expected from market movements. To achieve this, we will use a combination of three distinct factors: Momentum 1 Year Factor, Mean Reversion 5 Day Sector Neutral Smoothed Factor, and Overnight Sentiment Smoothed Factor.</p><p>The Momentum 1 Year Factor is a measure of a stock's recent performance over a one-year period. This factor identifies stocks that have demonstrated strong returns over the past year and involves buying them with the expectation that they will continue to perform well in the future.</p><p>The Mean Reversion 5 Day Sector Neutral Smoothed Factor focuses on identifying stocks that have experienced a recent dip in price and aims to capitalize on their tendency to revert back to their mean price. This factor is sector-neutral, which means that it is not biased towards any specific industry or sector, making it easier to use in diversified portfolios. Additionally, this factor is smoothed to reduce noise and make it easier to interpret.</p><p>The Overnight Sentiment Smoothed Factor analyzes news and social media sentiment to identify stocks that are likely to have positive price movements in the following day's trading session. This factor uses natural language processing techniques to analyze large amounts of data and smooths the results to reduce noise and increase accuracy.</p><p>Combining these three factors into an enhanced Alpha can help investors identify attractive investment opportunities that may have been overlooked by other market participants. It's worth noting that the selection and weighting of these factors will require careful consideration, and they may need to be refined and adjusted over time based on market conditions and performance.</p><p><strong>Load Packages:</strong></p><pre><code>import numpy as np
import pandas as pd
from tqdm import tqdm
import matplotlib.pyplot as plt

%matplotlib inline
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (14, 8)</code></pre><div><hr></div><h2>Data Pipeline</h2><h3>Data Bundle</h3><p>We'll be using Zipline to handle our data. We've created a end of day data bundle for this project. Run the cell below to register this data bundle in zipline.</p><pre><code>import os
from zipline.data import bundles

os.environ['ZIPLINE_ROOT'] = os.path.join(os.getcwd(), '..', '..', 'data', 'project_7_eod')

ingest_func = bundles.csvdir.csvdir_equities(['daily'], project_helper.EOD_BUNDLE_NAME)
bundles.register(project_helper.EOD_BUNDLE_NAME, ingest_func)

print('Data Registered')</code></pre><div><hr></div><h3>Build Pipeline Engine</h3><p>We'll be using Zipline's pipeline package to access our data for this project. To use it, we must build a pipeline engine. Run the cell below to build the engine.</p><pre><code>from zipline.pipeline import Pipeline
from zipline.pipeline.factors import AverageDollarVolume
from zipline.utils.calendars import get_calendar


universe = AverageDollarVolume(window_length=120).top(500) 
trading_calendar = get_calendar('NYSE') 
bundle_data = bundles.load(project_helper.EOD_BUNDLE_NAME)
engine = project_helper.build_pipeline_engine(bundle_data, trading_calendar)</code></pre><div><hr></div><h1>Alpha Factors</h1><p>It's time to start working on the alpha factors. In this project, we'll use the following factors:</p><ul><li><p>Momentum 1 Year Factor</p></li><li><p>Mean Reversion 5 Day Sector Neutral Smoothed Factor</p></li><li><p>Overnight Sentiment Smoothed Factor</p></li></ul><pre><code>from zipline.pipeline.factors import CustomFactor, DailyReturns, Returns, SimpleMovingAverage, AnnualizedVolatility
from zipline.pipeline.data import USEquityPricing


factor_start_date = universe_end_date - pd.DateOffset(years=3, days=2)
sector = project_helper.Sector()

def momentum_1yr(window_length, universe, sector):
    return Returns(window_length=window_length, mask=universe) \
        .demean(groupby=sector) \
        .rank() \
        .zscore()

def mean_reversion_5day_sector_neutral_smoothed(window_length, universe, sector):
    unsmoothed_factor = -Returns(window_length=window_length, mask=universe) \
        .demean(groupby=sector) \
        .rank() \
        .zscore()
    return SimpleMovingAverage(inputs=[unsmoothed_factor], window_length=window_length) \
        .rank() \
        .zscore()

class CTO(Returns):
    """
    Computes the overnight return, per hypothesis from
    https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2554010
    """
    inputs = [USEquityPricing.open, USEquityPricing.close]
    
    def compute(self, today, assets, out, opens, closes):
        """
        The opens and closes matrix is 2 rows x N assets, with the most recent at the bottom.
        As such, opens[-1] is the most recent open, and closes[0] is the earlier close
        """
        out[:] = (opens[-1] - closes[0]) / closes[0]
        
class TrailingOvernightReturns(Returns):
    """
    Sum of trailing 1m O/N returns
    """
    window_safe = True
    
    def compute(self, today, asset_ids, out, cto):
        out[:] = np.nansum(cto, axis=0)

def overnight_sentiment_smoothed(cto_window_length, trail_overnight_returns_window_length, universe):
    cto_out = CTO(mask=universe, window_length=cto_window_length)
    unsmoothed_factor = TrailingOvernightReturns(inputs=[cto_out], window_length=trail_overnight_returns_window_length) \
        .rank() \
        .zscore()
    return SimpleMovingAverage(inputs=[unsmoothed_factor], window_length=trail_overnight_returns_window_length) \
        .rank() \
        .zscore()</code></pre><div><hr></div><h2>Features and Labels</h2><p>Let's create some features that we think will help the model make predictions.</p><h3>"Universal" Quant Features</h3><p>To capture the universe, we'll use the following as features:</p><ul><li><p>Stock Volatility 20d, 120d</p></li><li><p>Stock Dollar Volume 20d, 120d</p></li><li><p>Sector</p></li></ul><pre><code>pipeline.add(AnnualizedVolatility(window_length=20, mask=universe).rank().zscore(), 'volatility_20d')
pipeline.add(AnnualizedVolatility(window_length=120, mask=universe).rank().zscore(), 'volatility_120d')
pipeline.add(AverageDollarVolume(window_length=20, mask=universe).rank().zscore(), 'adv_20d')
pipeline.add(AverageDollarVolume(window_length=120, mask=universe).rank().zscore(), 'adv_120d')
pipeline.add(sector, 'sector_code')</code></pre><div><hr></div><h3>Regime Features</h3><p>We are going to try to capture market-wide regimes. To do that, we'll use the following features:</p><ul><li><p>High and low volatility 20d, 120d</p></li><li><p>High and low dispersion 20d, 120d</p></li></ul><pre><code>class MarketDispersion(CustomFactor):
    inputs = [DailyReturns()]
    window_length = 1
    window_safe = True

    def compute(self, today, assets, out, returns):
        # returns are days in rows, assets across columns
        out[:] = np.sqrt(np.nanmean((returns - np.nanmean(returns))**2))


pipeline.add(SimpleMovingAverage(inputs=[MarketDispersion(mask=universe)], window_length=20), 'dispersion_20d')
pipeline.add(SimpleMovingAverage(inputs=[MarketDispersion(mask=universe)], window_length=120), 'dispersion_120d')</code></pre><div><hr></div><h3>Date Features</h3><p>Let's make columns for the trees to split on that might capture trader/investor behavior due to calendar anomalies.</p><pre><code>all_factors = engine.run_pipeline(pipeline, factor_start_date, universe_end_date)

all_factors['is_Janaury'] = all_factors.index.get_level_values(0).month == 1
all_factors['is_December'] = all_factors.index.get_level_values(0).month == 12
all_factors['weekday'] = all_factors.index.get_level_values(0).weekday
all_factors['quarter'] = all_factors.index.get_level_values(0).quarter
all_factors['qtr_yr'] = all_factors.quarter.astype('str') + '_' + all_factors.index.get_level_values(0).year.astype('str')
all_factors['month_end'] = all_factors.index.get_level_values(0).isin(pd.date_range(start=factor_start_date, end=universe_end_date, freq='BM'))
all_factors['month_start'] = all_factors.index.get_level_values(0).isin(pd.date_range(start=factor_start_date, end=universe_end_date, freq='BMS'))
all_factors['qtr_end'] = all_factors.index.get_level_values(0).isin(pd.date_range(start=factor_start_date, end=universe_end_date, freq='BQ'))
all_factors['qtr_start'] = all_factors.index.get_level_values(0).isin(pd.date_range(start=factor_start_date, end=universe_end_date, freq='BQS'))

all_factors.head()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gPXK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gPXK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 424w, https://substackcdn.com/image/fetch/$s_!gPXK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 848w, https://substackcdn.com/image/fetch/$s_!gPXK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 1272w, https://substackcdn.com/image/fetch/$s_!gPXK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gPXK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png" width="1456" height="361" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:361,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:109263,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gPXK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 424w, https://substackcdn.com/image/fetch/$s_!gPXK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 848w, https://substackcdn.com/image/fetch/$s_!gPXK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 1272w, https://substackcdn.com/image/fetch/$s_!gPXK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b9d5e8b-bf00-489e-bba3-1a502a7b7897_1840x456.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div><hr></div><h3>One Hot Encode Sectors</h3><p>For the model to better understand the sector data, we'll one hot encode this data.</p><pre><code>sector_lookup = pd.read_csv(
    os.path.join(os.getcwd(), '..', '..', 'data', 'project_7_sector', 'labels.csv'),
    index_col='Sector_i')['Sector'].to_dict()
sector_lookup

sector_columns = []
for sector_i, sector_name in sector_lookup.items():
    secotr_column = 'sector_{}'.format(sector_name)
    sector_columns.append(secotr_column)
    all_factors[secotr_column] = (all_factors['sector_code'] == sector_i)

all_factors[sector_columns].head()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_XtC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_XtC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 424w, https://substackcdn.com/image/fetch/$s_!_XtC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 848w, https://substackcdn.com/image/fetch/$s_!_XtC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 1272w, https://substackcdn.com/image/fetch/$s_!_XtC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_XtC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png" width="1456" height="354" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:354,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:93651,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_XtC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 424w, https://substackcdn.com/image/fetch/$s_!_XtC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 848w, https://substackcdn.com/image/fetch/$s_!_XtC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 1272w, https://substackcdn.com/image/fetch/$s_!_XtC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2cdbfd3-b24e-41d2-8cbf-19001d2c053a_1960x476.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div><hr></div><h3>Shift Target</h3><p>We'll use shifted 5 day returns for training the model.</p><pre><code>all_factors['target'] = all_factors.groupby(level=1)['return_5d'].shift(-5)

all_factors[['return_5d','target']].reset_index().sort_values(['level_1', 'level_0']).head(10)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iqhB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iqhB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 424w, https://substackcdn.com/image/fetch/$s_!iqhB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 848w, https://substackcdn.com/image/fetch/$s_!iqhB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 1272w, https://substackcdn.com/image/fetch/$s_!iqhB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iqhB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png" width="890" height="564" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a313e9c8-42da-4759-a828-a1748817ba58_890x564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:564,&quot;width&quot;:890,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:94401,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iqhB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 424w, https://substackcdn.com/image/fetch/$s_!iqhB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 848w, https://substackcdn.com/image/fetch/$s_!iqhB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 1272w, https://substackcdn.com/image/fetch/$s_!iqhB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa313e9c8-42da-4759-a828-a1748817ba58_890x564.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3>IID Check of Target</h3><p>Let's see if the returns are independent and identically distributed.</p><pre><code>from scipy.stats import spearmanr


def sp(group, col1_name, col2_name):
    x = group[col1_name]
    y = group[col2_name]
    return spearmanr(x, y)[0]


all_factors['target_p'] = all_factors.groupby(level=1)['return_5d_p'].shift(-5)
all_factors['target_1'] = all_factors.groupby(level=1)['return_5d'].shift(-4)
all_factors['target_2'] = all_factors.groupby(level=1)['return_5d'].shift(-3)
all_factors['target_3'] = all_factors.groupby(level=1)['return_5d'].shift(-2)
all_factors['target_4'] = all_factors.groupby(level=1)['return_5d'].shift(-1)

g = all_factors.dropna().groupby(level=0)
for i in range(4):
    label = 'target_'+str(i+1)
    ic = g.apply(sp, 'target', label)
    ic.plot(ylim=(-1, 1), label=label)
plt.legend(bbox_to_anchor=(1.04, 1), borderaxespad=0)
plt.title('Rolling Autocorrelation of Labels Shifted 1,2,3,4 Days')
plt.show()</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NkYS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NkYS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 424w, https://substackcdn.com/image/fetch/$s_!NkYS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 848w, https://substackcdn.com/image/fetch/$s_!NkYS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 1272w, https://substackcdn.com/image/fetch/$s_!NkYS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NkYS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png" width="850" height="464" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:464,&quot;width&quot;:850,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:237945,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NkYS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 424w, https://substackcdn.com/image/fetch/$s_!NkYS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 848w, https://substackcdn.com/image/fetch/$s_!NkYS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 1272w, https://substackcdn.com/image/fetch/$s_!NkYS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8afad2dc-fb5b-40f9-a08c-fe26ebdcf9f5_850x464.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3>Train/Valid/Test Splits</h3><p>Now let's split the data into a train, validation, and test dataset. Implement the function <code>train_valid_test_split</code> to split the input samples, <code>all_x</code>, and targets values, <code>all_y</code> into a train, validation, and test dataset. The proportion sizes are <code>train_size</code>, <code>valid_size</code>, <code>test_size</code> respectively.</p><p>When splitting, make sure the data is in order from train, validation, and test respectivly. Say <code>train_size</code> is 0.7, <code>valid_size</code> is 0.2, and <code>test_size</code> is 0.1. The first 70 percent of <code>all_x</code> and <code>all_y</code> would be the train set. The next 20 percent of <code>all_x</code> and <code>all_y</code> would be the validation set. The last 10 percent of <code>all_x</code> and <code>all_y</code> would be the test set. Make sure not split a day between multiple datasets. It should be contained within a single dataset.</p><pre><code>def train_valid_test_split(all_x, all_y, train_size, valid_size, test_size):
    """
    Generate the train, validation, and test dataset.

    Parameters
    ----------
    all_x : DataFrame
        All the input samples
    all_y : Pandas Series
        All the target values
    train_size : float
        The proportion of the data used for the training dataset
    valid_size : float
        The proportion of the data used for the validation dataset
    test_size : float
        The proportion of the data used for the test dataset

    Returns
    -------
    x_train : DataFrame
        The train input samples
    x_valid : DataFrame
        The validation input samples
    x_test : DataFrame
        The test input samples
    y_train : Pandas Series
        The train target values
    y_valid : Pandas Series
        The validation target values
    y_test : Pandas Series
        The test target values
    """
    assert train_size &gt;= 0 and train_size &lt;= 1.0
    assert valid_size &gt;= 0 and valid_size &lt;= 1.0
    assert test_size &gt;= 0 and test_size &lt;= 1.0
    assert train_size + valid_size + test_size == 1.0
    
    # TODO: Implement
    NN = all_x.index.levels[0]
    N = len(NN)
    Tsx = int(N * train_size)
    Vsx = int(N * (train_size + valid_size))
    
    TRi = NN[:Tsx]
    Vi = NN[Tsx:Vsx]
    TEi = NN[Vsx:]

    xTr, xV, xTe = all_x.loc[TRi[0]:TRi[-1]], all_x.loc[Vi[0]:Vi[-1]], all_x.loc[TEi[0]:TEi[-1]]
    yTr, yV, yTe = all_y.loc[TRi[0]:TRi[-1]], all_y.loc[Vi[0]:Vi[-1]], all_y.loc[TEi[0]:TEi[-1]]
    
    
    return xTr, xV, xTe, yTr, yV, yTe</code></pre><div><hr></div><h2>Random Forests</h2><h3>Visualize a Simple Tree</h3><p>Let's see how a single tree would look using our data.</p><pre><code>from IPython.display import display
from sklearn.tree import DecisionTreeClassifier


# This is to get consistent results between each run.
clf_random_state = 0

simple_clf = DecisionTreeClassifier(
    max_depth=3,
    criterion='entropy',
    random_state=clf_random_state)
simple_clf.fit(X_train, y_train)

display(project_helper.plot_tree_classifier(simple_clf, feature_names=features))
project_helper.rank_features_by_importance(simple_clf.feature_importances_, features)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BuLl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BuLl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 424w, https://substackcdn.com/image/fetch/$s_!BuLl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 848w, https://substackcdn.com/image/fetch/$s_!BuLl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 1272w, https://substackcdn.com/image/fetch/$s_!BuLl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BuLl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png" width="1181" height="744" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:744,&quot;width&quot;:1181,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:141997,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BuLl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 424w, https://substackcdn.com/image/fetch/$s_!BuLl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 848w, https://substackcdn.com/image/fetch/$s_!BuLl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 1272w, https://substackcdn.com/image/fetch/$s_!BuLl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffcfb2d34-ad32-438a-8b06-37ff33f8e35b_1181x744.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EXKh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EXKh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 424w, https://substackcdn.com/image/fetch/$s_!EXKh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 848w, https://substackcdn.com/image/fetch/$s_!EXKh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 1272w, https://substackcdn.com/image/fetch/$s_!EXKh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EXKh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png" width="464" height="405.30120481927713" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:870,&quot;width&quot;:996,&quot;resizeWidth&quot;:464,&quot;bytes&quot;:182035,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EXKh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 424w, https://substackcdn.com/image/fetch/$s_!EXKh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 848w, https://substackcdn.com/image/fetch/$s_!EXKh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 1272w, https://substackcdn.com/image/fetch/$s_!EXKh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc88d9d8f-eec2-4e4e-955d-5c013f6890d0_996x870.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WfyD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WfyD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 424w, https://substackcdn.com/image/fetch/$s_!WfyD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 848w, https://substackcdn.com/image/fetch/$s_!WfyD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 1272w, https://substackcdn.com/image/fetch/$s_!WfyD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WfyD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png" width="458" height="426.60483870967744" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:924,&quot;width&quot;:992,&quot;resizeWidth&quot;:458,&quot;bytes&quot;:242033,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WfyD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 424w, https://substackcdn.com/image/fetch/$s_!WfyD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 848w, https://substackcdn.com/image/fetch/$s_!WfyD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 1272w, https://substackcdn.com/image/fetch/$s_!WfyD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f986daa-906c-46c7-baea-a830e2ff0c1b_992x924.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3>Model Results</h3><p>Let's look at some additional metrics to see how well a model performs. We've created the function <code>show_sample_results</code> to show the following results of a model:</p><ul><li><p>Sharpe Ratios</p></li><li><p>Factor Returns</p></li><li><p>Factor Rank Autocorrelation</p></li></ul><pre><code>import alphalens as al


all_assets = all_factors.index.levels[1].values.tolist()
all_pricing = get_pricing(
    data_portal,
    trading_calendar,
    all_assets,
    factor_start_date,
    universe_end_date)

def show_sample_results(data, samples, classifier, factors, pricing=all_pricing):
    # Calculate the Alpha Score
    prob_array=[-1,1]
    alpha_score = classifier.predict_proba(samples).dot(np.array(prob_array))
    
    # Add Alpha Score to rest of the factors
    alpha_score_label = 'AI_ALPHA'
    factors_with_alpha = data.loc[samples.index].copy()
    factors_with_alpha[alpha_score_label] = alpha_score
    
    # Setup data for AlphaLens
    print('Cleaning Data...\n')
    factor_data = project_helper.build_factor_data(factors_with_alpha[factors + [alpha_score_label]], pricing)
    print('\n-----------------------\n')
    
    # Calculate Factor Returns and Sharpe Ratio
    factor_returns = project_helper.get_factor_returns(factor_data)
    sharpe_ratio = project_helper.sharpe_ratio(factor_returns)
    
    # Show Results
    print('             Sharpe Ratios')
    print(sharpe_ratio.round(2))
    project_helper.plot_factor_returns(factor_returns)
    project_helper.plot_factor_rank_autocorrelation(factor_data)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tohT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tohT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 424w, https://substackcdn.com/image/fetch/$s_!tohT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 848w, https://substackcdn.com/image/fetch/$s_!tohT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 1272w, https://substackcdn.com/image/fetch/$s_!tohT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tohT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png" width="368" height="125.63440860215054" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:254,&quot;width&quot;:744,&quot;resizeWidth&quot;:368,&quot;bytes&quot;:45445,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tohT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 424w, https://substackcdn.com/image/fetch/$s_!tohT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 848w, https://substackcdn.com/image/fetch/$s_!tohT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 1272w, https://substackcdn.com/image/fetch/$s_!tohT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b52d32-bdcd-4e32-9e53-7a123b91c5a7_744x254.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k1kK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k1kK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 424w, https://substackcdn.com/image/fetch/$s_!k1kK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 848w, https://substackcdn.com/image/fetch/$s_!k1kK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 1272w, https://substackcdn.com/image/fetch/$s_!k1kK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!k1kK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png" width="382" height="281" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:281,&quot;width&quot;:382,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:34800,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k1kK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 424w, https://substackcdn.com/image/fetch/$s_!k1kK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 848w, https://substackcdn.com/image/fetch/$s_!k1kK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 1272w, https://substackcdn.com/image/fetch/$s_!k1kK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F890ac8c9-33d2-45ac-bd97-34099914bd8b_382x281.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!z0kG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!z0kG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 424w, https://substackcdn.com/image/fetch/$s_!z0kG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 848w, https://substackcdn.com/image/fetch/$s_!z0kG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 1272w, https://substackcdn.com/image/fetch/$s_!z0kG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!z0kG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png" width="388" height="282" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:282,&quot;width&quot;:388,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:58488,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!z0kG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 424w, https://substackcdn.com/image/fetch/$s_!z0kG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 848w, https://substackcdn.com/image/fetch/$s_!z0kG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 1272w, https://substackcdn.com/image/fetch/$s_!z0kG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd281e13a-d9ee-42ca-b8cc-6fd95fd7390f_388x282.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p>Hopefully, you're impressed by this outcome. Even though there were notable variations in factor performances across the three sets, AI ALPHA managed to achieve a favorable result.</p>]]></content:encoded></item></channel></rss>