<?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]]></title><description><![CDATA[Your Guide to AI & Quant Finance]]></description><link>https://www.gofar.ai</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</title><link>https://www.gofar.ai</link></image><generator>Substack</generator><lastBuildDate>Sun, 03 May 2026 17:01:12 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[LLMs vs. Human Mind: Understanding the Creativity Gap.]]></title><description><![CDATA[Exploring What Sets Human Imagination Apart from LLMs and AI's Logic.]]></description><link>https://www.gofar.ai/p/llms-vs-human-mind-understanding</link><guid isPermaLink="false">https://www.gofar.ai/p/llms-vs-human-mind-understanding</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Fri, 19 Jul 2024 20:20:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!lHpR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg" 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_!lHpR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lHpR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lHpR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:532364,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&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_!lHpR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!lHpR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff84623c2-59f7-4211-aa7a-8a5c732a3a7c_1024x1024.jpeg 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><h3><strong>Introduction</strong></h3><p>In the exciting world of artificial intelligence, autoregressive large language models (LLMs) like GPT-4 have changed the way we think about machines talking and writing like humans. These models can create text that feels incredibly human-like, from stories to computer code, making it hard at times to tell if a human or a machine wrote something.</p><p>Even with these amazing advances, there's a big difference between what LLMs can do and what the human mind can achieve, especially in planning for the future, taking risks, and being truly creative. This blog post will look at these differences. We'll see how, although LLMs can copy the way we use words, they don't quite match the human mind's ability to think deeply and come up with new ideas.</p><p>The story of LLMs shows how clever human beings have been in creating these models. But as we get closer to what seems like the dream of creating a machine as smart as a person, we also see the big gaps that remain. These models show us that making a machine that truly thinks and feels like a human is not only hard but might require us to think in new ways.</p><p>In this blog post, we'll take a closer look at how LLMs work and what they've achieved. We'll also talk about where they don't quite measure up to human thinking and creativity. We hope to shed some light on what the future might hold for AI, the ethical questions it brings up, and how human creativity and decision-making fit into a world where machines can write and speak. Let's explore the world of LLMs together, looking at the good, the challenges, and what lies ahead.</p><p><em><strong>Note: Detailed equations and experiments will be shared in a follow-up publication. This post is non-technical and includes simplified equations to help illustrate the core concepts.</strong></em></p><h3><strong>Understanding Autoregressive Language Models</strong></h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = p(y_t | y_{t-1}, y_{t-2}, ..., y_1) = \\text{softmax}\\left( \\frac{QK^T}{\\sqrt{d_k}} \\right)V\n&quot;,&quot;id&quot;:&quot;GUKANCOOLS&quot;}" data-component-name="LatexBlockToDOM"></div><p>Let's start by talking about what autoregressive language models, or LLMs for short, actually are. Imagine you're writing a sentence and you pause, not sure what word should come next. If you've ever used a phone or computer that suggests the next word for you, you've got a basic idea of what these models do. But LLMs like GPT-3 are like those suggestions on steroids. They don't just look at the last few words you typed; they consider everything you've written so far to guess what comes next. And they're really good at it.</p><p>These models are trained on a massive amount of text from the internet&#8212;books, articles, websites, you name it. This training helps them learn how words and phrases naturally fit together. Because they've seen so many examples, they can write text that sounds quite human. They can finish a story, write an essay, or even generate new ideas for a movie script.</p><p>One of the coolest things about LLMs is their flexibility. They're not just stuck on one topic; they can write about anything from space travel to baking cakes because they've learned from a wide range of sources. This ability has made them super popular for a bunch of different tasks, like helping writers come up with ideas, assisting students with homework, or even writing code for programmers.</p><p>But even though they can do all these things, it's important to remember that LLMs are still just guessing what word comes next based on what they've seen before. They don't really "understand" what they're writing in the way we do. They're like parrots, repeating things they've heard without really getting the meaning behind them.</p><p></p><h3><strong>Taking LLMs Further with Knowledge Retrieval</strong></h3><p></p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p(y_t | y_{t-1}, y_{t-2}, ..., y_1, D) = \\text{softmax}\\left( \\frac{Q(K^T + R^T)}{\\sqrt{d_k}} \\right)V&quot;,&quot;id&quot;:&quot;TYOHXPXZRW&quot;}" data-component-name="LatexBlockToDOM"></div><p>The evolution of autoregressive language models (LLMs) like GPT has taken a significant leap forward with the addition of knowledge retrieval capabilities. This enhancement allows models not just to generate text based on internal data but also to access external information in real-time, much like consulting a vast online library for facts or detailed insights.</p><p>Enter Retrieval-Augmented Generation (RAG) models, which marry the linguistic skill of LLMs with the ability to pull in specific facts from a large database or the internet. When faced with questions requiring detailed knowledge, these models search for relevant information outside their training data, then integrate this into their responses.</p><p>This blending of creativity and precision transforms how AI understands and generates text, making it not only more relevant but also more informative. It's a step closer to AI that truly interacts with the breadth of human knowledge.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\n&quot;,&quot;id&quot;:&quot;VACWRHNXKX&quot;}" data-component-name="LatexBlockToDOM"></div><p></p><h3><strong>Limitations of LLMs Compared to the Human Mind</strong></h3><h4>Lack of Forward Planning</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p(y_t | s_t, D) = \\text{softmax}\\left( \\frac{Q(s_t, y, D)}{\\sqrt{d_k}} \\right)V \\quad \\\n&quot;,&quot;id&quot;:&quot;SYYHSPQCNZ&quot;}" data-component-name="LatexBlockToDOM"></div><p>When we talk or write, we often have a goal or an end point in mind. We think about what we want to say next, sometimes planning several steps ahead to make our point clearly. But LLMs don't work like this. They focus on the moment, choosing the next word based only on what's been said before. They don't plan ahead or think about the end goal of a conversation or a piece of writing.</p><p>For example, if you're writing a story with an LLM, it can help you write the next sentence or paragraph, but it doesn't have an overall plot or message in mind. It's like writing without knowing how the story will end. This is a big difference from how humans think and create, where we often start with an end goal or a message we want to convey.</p><h4>Risk-Aversion and Predictability</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\arg \\max_{y} \\left[ Q(s_t, y) + \\lambda \\cdot U(y) \\right]\n&quot;,&quot;id&quot;:&quot;QXJXTSYMKI&quot;}" data-component-name="LatexBlockToDOM"></div><p>LLMs tend to play it safe. Since they're built to predict the most likely next word based on their training, they usually go for the option that's been seen most often. This means their responses can be quite predictable and sometimes boring. They're not great at taking risks or trying something new and unexpected.</p><p>Humans, on the other hand, can decide to take a creative leap or introduce a twist that no one sees coming. We value originality and the ability to surprise, which is something LLMs struggle with. When we make art, write stories, or solve problems, we often do so by stepping into the unknown, taking risks, and experimenting. This is how new ideas and innovations come about.</p><h4>The Creative Gap</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\arg \\max_{y} \\left[ Q(s_t, y) + \\lambda \\cdot U(y) + \\mu \\cdot I(y) \\right]\n\n\n&quot;,&quot;id&quot;:&quot;QJNEWDCIGF&quot;}" data-component-name="LatexBlockToDOM"></div><p>Creativity involves coming up with something new, whether it's an idea, a solution to a problem, or a piece of art. While LLMs can generate text that might seem creative because it puts words together in new ways, they're really just remixing bits and pieces of what they've been trained on. They don't have the ability to think outside the box or come up with truly novel ideas from scratch.</p><p>Humans are capable of imagination&#8212;thinking of things that don't exist yet or that they've never experienced. We can dream up entirely new worlds, invent new technologies, or create art that expresses unique emotions and perspectives. This level of creativity is something LLMs currently can't match because they lack the ability to generate truly original ideas or feel emotions.</p><p></p><h3><strong>The Role of Training Data</strong></h3><p>One of the biggest reasons LLMs have limitations comes down to their training data. Think of training data like the books, conversations, movies, and all other kinds of information a person might learn from throughout their life. But instead of a lifetime, LLMs get crammed with this information all at once during their training phase. They learn from texts found online, which means they're learning from things that have already been created by humans.</p><p>This reliance on existing data means LLMs are great at giving back what they've seen in new combinations, but they can't really come up with something totally new. They're like a chef who can only cook with ingredients they've used before, unable to invent new ingredients or imagine new flavors beyond what they've tasted.</p><p>Moreover, because they learn from what's already out there, they can also pick up and repeat biases or errors found in their training materials. This is a challenge for people who make and use LLMs because it means they have to be very careful about the data they use to train these models. They want to make sure LLMs are helpful and fair, not just repeating the mistakes of the past.</p><h4>Challenges in Emulating Risk-Taking and Forward-Thinking</h4><p>Trying to get LLMs to think ahead, take risks, or be truly creative is hard because these actions often require understanding context, having goals, and being able to imagine outcomes that don't exist yet. LLMs don't have personal experiences or desires; they don't want anything. They don't get excited about a risky idea or feel proud of a creative solution. Because of this, designing LLMs to emulate such complex human behaviors without directly copying from specific examples in their training data is a big challenge.</p><p>Researchers are working on ways to improve LLMs, like teaching them to follow certain rules or goals, or using feedback from humans to guide them toward more creative and varied responses. But there's still a long way to go before they can truly mimic the depth of human creativity and foresight.</p><p></p><h3><strong>Ethical and Practical Implications</strong></h3><p>Using autoregressive language models (LLMs) raises some important questions about ethics and practicality, especially as these tools become more integrated into our daily lives and work. The limitations of LLMs, such as their inability to plan ahead, take risks, or create genuinely new ideas, have implications for how we use them and what we expect from them.</p><h4>Ethical Considerations</h4><p>One major ethical concern is the potential for LLMs to propagate biases found in their training data. Since LLMs learn from a vast array of online texts, they can inadvertently learn and replicate societal biases. This raises ethical questions about fairness and representation, especially when LLMs are used in decision-making processes or creating content that reaches a wide audience.</p><p>Furthermore, as LLMs become more capable of generating human-like text, there's a risk of misinformation or impersonation. Ensuring that generated content is accurately represented as machine-generated is crucial to maintaining trust and integrity in information.</p><h4>Practical Implications for Industries</h4><p>For industries that rely on innovation and creativity, the limitations of LLMs mean that they can't fully replace human creativity. While LLMs can assist in brainstorming sessions, content creation, and even some aspects of design and engineering, they still require human oversight to ensure originality and alignment with goals.</p><p>However, LLMs also offer opportunities to automate repetitive tasks, provide inspiration for human creators, and process large amounts of information more quickly than a human could. This can lead to more efficient workflows and free up human workers to focus on tasks that require genuine creativity, emotional intelligence, and strategic planning.</p><h3><strong>Future Directions</strong></h3><p>Despite their limitations, the development of LLMs is ongoing, and researchers are constantly looking for ways to overcome these challenges. Future advancements may include models that can better understand context, simulate planning, or more effectively incorporate feedback to generate truly novel and valuable outputs.</p><h4>Incorporating Models for Planning and Risk Assessment</h4><p>One area of research focuses on integrating models that can simulate planning or assess potential outcomes based on certain actions. This could help LLMs better mimic the human ability to plan ahead and consider the implications of their choices.</p><h4>Enhancing Creativity Through Diverse Training and Feedback Loops</h4><p>Improving the creativity of LLMs might involve diversifying the training data and developing more sophisticated feedback mechanisms. By exposing LLMs to a wider range of creative outputs and allowing them to learn from human feedback, there's potential for these models to produce more varied and innovative content.</p><h3><strong>Conclusion</strong></h3><p>As we've explored the capabilities and limitations of autoregressive language models, it's clear that while they represent a significant technological advancement, they are not yet close to replicating the full scope of human intelligence and creativity. Understanding these limitations is crucial as we continue to integrate AI into various aspects of our lives and work.</p><p>The journey toward more advanced AI, possibly even artificial general intelligence, is ongoing. By acknowledging the gaps in current models and focusing on ethical and innovative research, we can move closer to creating AI that complements human capabilities, encourages creativity, and benefits society as a whole.</p><p>Let's remain curious and open-minded, appreciating the advancements made so far while striving for the breakthroughs that lie ahead. In the ever-evolving relationship between humans and machines, the future holds endless possibilities for collaboration, innovation, and discovery.</p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[RAG, REALM, RETRO & Beyond: The Evolution of Retrieval-Augmented Models]]></title><description><![CDATA[Breaking Down the Basics: A Simple Guide to RAG, REALM, and RETRO AI Models.]]></description><link>https://www.gofar.ai/p/rag-realm-retro-and-beyond-the-evolution</link><guid isPermaLink="false">https://www.gofar.ai/p/rag-realm-retro-and-beyond-the-evolution</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sun, 12 Nov 2023 08:15:14 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!VeQK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Language models have really changed recently. Now, they use extra information from external sources to become smarter. This makes models like RAG, REALM, and RETRO better than the usual large language models we used before. In this post, we'll look at how RAG, REALM, and RETRO work, what's good and bad about them, how they're different from each other, and how they're better than traditional LLMs. We'll also think about what might come next in this area.</p><h3><strong>RAG (Retrieval-Augmented Generation):</strong> </h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VeQK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VeQK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 424w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 848w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1272w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VeQK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png" width="1072" height="334" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/db382620-ad44-444f-b148-3b5460405bcc_1072x334.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:334,&quot;width&quot;:1072,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:136074,&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_!VeQK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 424w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 848w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.png 1272w, https://substackcdn.com/image/fetch/$s_!VeQK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb382620-ad44-444f-b148-3b5460405bcc_1072x334.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><figcaption class="image-caption">Paper: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks <a href="https://arxiv.org/abs/2005.11401">[Here]</a></figcaption></figure></div><p>RAG is a special kind of language model. It combines a language model with a system that finds information from a large group of documents. This helps RAG give better and more accurate answers, especially when you need specific facts. But, RAG's answers depend on how good its information sources are. Sometimes, it struggles to mix the information it finds into its answers smoothly.</p><h4><strong>RAG Mechanism:</strong></h4><p>Understanding the Retrieval-Augmented Generation (RAG) model requires diving into its unique mechanism that combines retrieval and generation based on conditional probability. At its core, RAG operates by calculating the probability of generating an appropriate answer <em>A</em> given a specific query Q and a set of retrieved documents D1, D2, &#8230;, Dn. This process is represented mathematically as:</p><p></p><p>This equation signifies the model&#8217;s effort to find the likelihood of the answer <em>A</em> when we have a query <em>Q</em> and relevant documents D1, D2, &#8230;, Dn. The RAG model achieves this in two primary steps:</p><ol><li><p><strong>Document Retrieval</strong>: For a given query <em>Q</em>, the model first retrieves a set of documents that are likely to contain relevant information. This step involves estimating:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p_n(D_1, D_2, ..., D_n|q)&quot;,&quot;id&quot;:&quot;RZOBLLCIPS&quot;}" data-component-name="LatexBlockToDOM"></div><p>which is the probability of each document being relevant to the query.</p></li></ol><p></p><ol start="2"><li><p><strong>Response Generation</strong>: Following retrieval, the model uses both the query and the gathered documents to generate an answer (A). This crucial step involves approximating</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p_\\theta(A|q,D_1,D_2, ..., D_n) &quot;,&quot;id&quot;:&quot;NPTVQFSNNK&quot;}" data-component-name="LatexBlockToDOM"></div><p>the probability of generating the answer considering both the query and the retrieved documents. So the overall expression of the RAG process:</p></li></ol><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;p_{RAG}(y|x) \\approx \\sum_{d \\in top-k(p(\\cdot|x))} p_n(d|q)p_\\theta(A|q,d)&quot;,&quot;id&quot;:&quot;TODMKKPBUU&quot;}" data-component-name="LatexBlockToDOM"></div><p>Through this sophisticated approach, the RAG model effectively maximizes the chances of producing the most relevant and accurate answer <em>A</em> based on the query <em>Q</em> and the information extracted from the selected documents D1, D2, &#8230;, Dn.  This mechanism showcases the innovative blend of retrieval and generation, pushing the boundaries of what language models can achieve.</p><h4><strong>A Simple Way to Use RAG with Hugging Face:</strong></h4><p>For those looking to get started with the RAG model, a straightforward approach is to use the implementation provided by Hugging Face's Transformers library. This popular library simplifies the process, allowing you to leverage RAG's capabilities with just a few lines of code.</p><p>Hugging Face has pre-trained the RAG model on a vast dataset, making it readily available for various tasks, especially question-answering. The model combines a powerful retriever and a language generation model, offering high-quality responses by fetching relevant information from a large dataset.</p><p>To use RAG from Hugging Face, you first need to install the Transformers and Torch libraries. Once installed, you can initialize the RAG components &#8211; the tokenizer, retriever, and the model itself &#8211; directly from Hugging Face's model repository.</p><p>The process is as follows:</p><ol><li><p><strong>Initialize the Tokenizer</strong>: This component converts text inputs into a format that the model can understand.</p></li><li><p><strong>Set Up the Retriever</strong>: The retriever fetches relevant documents based on the input query.</p></li><li><p><strong>Load the RAG Model</strong>: The model uses the retrieved documents and the input query to generate an answer.</p></li></ol><p>Here's a basic example of how you can use RAG for a simple question:</p><pre><code># Import necessary classes
from transformers import RagTokenizer, RagTokenForGeneration, RagRetriever
import torch

# Function to generate an answer to a question
def generate_answer(question):
    # Initialize tokenizer, retriever, and model
    tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
    retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
    model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)

    # Process the question
    inputs = tokenizer(question, return_tensors="pt")

    # Generate the answer
    with torch.no_grad():
        generated_ids = model.generate(input_ids=inputs["input_ids"])

    # Decode and return the answer
    return tokenizer.decode(generated_ids[0], skip_special_tokens=True)

# Example usage
question = "What is the capital of Saudi Arabia?"
print(generate_answer(question))</code></pre><p>This code demonstrates how easily you can implement RAG for question-answering tasks. When you run this with a question like "What is the capital of Saudi Arabia?", the model should return "Riyadh" as the answer.</p><h3><strong>REALM (Retrieval-Augmented Language Model):</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aIOb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aIOb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 424w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 848w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1272w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aIOb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png" width="1456" height="432" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:432,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:169637,&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_!aIOb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 424w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 848w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.png 1272w, https://substackcdn.com/image/fetch/$s_!aIOb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77cd682b-bdd8-4989-bd47-49bb1e3591c4_1670x496.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><figcaption class="image-caption">Paper: REALM: Retrieval-Augmented Language Model Pre-Training <a href="https://arxiv.org/abs/2002.08909">[Here]</a></figcaption></figure></div><p>REALM is similar to RAG but works a bit differently. It learns to find useful documents and make answers at the same time. This makes REALM really good at giving relevant and correct answers. The downside is that it can be complex and needs a lot of computing power. Like RAG, it also depends on having good sources of information.</p><h4><strong>REALM Mechanism:</strong></h4><p>REALM, much like RAG, carries out its retrieval and generation processes in a sequential manner:</p><ol><li><p><strong>Retrieval Phase</strong>: In the first phase, REALM identifies relevant documents or information snippets that can assist in answering a query. This process is rooted in the concept of information retrieval, where the model is trained to search a vast database of texts to find the most pertinent pieces of information relative to the input query.</p></li><li><p><strong>Language Modeling Phase</strong>: Once relevant documents are retrieved, REALM uses this information, combined with the original query, to generate a response. This step involves a language model that synthesizes information from both the query and the retrieved texts to construct an answer.</p></li></ol><p>The retrieval phase can be represented in a probabilistic framework as follows:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(D|q)&quot;,&quot;id&quot;:&quot;SPSJJHFVJL&quot;}" data-component-name="LatexBlockToDOM"></div><p>This equation calculates the probability of a document <em>D</em> being relevant given a query <em>Q</em>. REALM optimizes this retrieval probability to ensure that only the most pertinent documents are considered in the next phase.</p><p>The language modeling phase, which follows the retrieval, can be represented as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(A&#8739;Q,D)&quot;,&quot;id&quot;:&quot;YDJOUBBUZM&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, <em>A</em> is the generated answer, <em>Q</em> is the input query, and <em>D</em> represents the retrieved documents. This equation signifies how the model calculates the probability of generating a specific answer based on both the query and the retrieved information.</p><p>Together, these two phases enable REALM to not only understand and process the input query but also to enhance its responses by integrating external, contextually relevant information. This makes REALM particularly effective in scenarios where an in-depth understanding and up-to-date information are crucial for generating accurate responses.</p><p>REALM and RAG differ in their process integration: REALM uses a two-step approach, first retrieving documents with <em>P</em>(<em>D</em>&#8739;<em>Q</em>) and then generating an answer with <em>P</em>(<em>A</em>&#8739;<em>Q</em>,<em>D</em>), while RAG combines these steps into one <em>P</em>(<em>A</em>&#8739;<em>Q</em>,<em>D</em>1&#8203;,<em>D</em>2&#8203;,&#8230;,<em>Dn</em>&#8203;), blending retrieval and generation more directly.</p><h4><strong>Using REALM in a Simple Way with Hugging Face:</strong></h4><p>Incorporating the REALM into your projects can be straightforward, especially when utilizing resources from Hugging Face's Transformers library. Hugging Face simplifies the process of applying advanced models like REALM, making it accessible even to those new to the field of AI.</p><p>REALM stands out for its ability to retrieve relevant information as part of its learning process, enhancing the quality of its responses. This is a significant step up from traditional language models, as it allows REALM to provide more accurate and context-aware answers.</p><p>To get started with REALM using Hugging Face, follow these basic steps:</p><ol><li><p><strong>Install the Necessary Libraries</strong>: Make sure you have Transformers and Torch installed in your environment. You can install them using pip if you haven't already.</p></li><li><p><strong>Initialize the Components</strong>: Similar to using RAG, you will need to initialize the tokenizer and the model. Hugging Face provides pre-trained versions that you can use directly.</p></li><li><p><strong>Load the REALM Model</strong>: Hugging Face's model repository includes versions of REALM that are pre-trained and ready to use. This saves you the time and effort of training the model from scratch.</p></li></ol><p>Here&#8217;s a simple code snippet demonstrating how to use REALM for a basic task:</p><pre><code># Import the required classes
from transformers import RealmTokenizer, RealmForOpenQA

# Initialize the tokenizer and model
tokenizer = RealmTokenizer.from_pretrained("google/realm-cc-news-pretrained-bert")
model = RealmForOpenQA.from_pretrained("google/realm-cc-news-pretrained-bert")

# Your query
query = "What is the tallest mountain in the world?"

# Tokenize and process the query
inputs = tokenizer(query, return_tensors="pt")
output = model(inputs)

# Process and display the result
answer = tokenizer.decode(output, skip_special_tokens=True)
print(f"Answer: {answer}")</code></pre><p>This example is a basic illustration of using REALM for question answering. When you input a query, the model retrieves information and generates an answer. Keep in mind that REALM's performance is highly dependent on the quality of the retrieval and the relevance of the pre-trained model to your task.</p><p></p><h3><strong>RETRO (Retrieval-Enhanced Transformer):</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3I38!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3I38!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 424w, https://substackcdn.com/image/fetch/$s_!3I38!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 848w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1272w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3I38!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png" width="1258" height="526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:526,&quot;width&quot;:1258,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:156576,&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_!3I38!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 424w, https://substackcdn.com/image/fetch/$s_!3I38!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 848w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.png 1272w, https://substackcdn.com/image/fetch/$s_!3I38!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cf4e02e-2aad-4785-b98b-9700e5e25b57_1258x526.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><figcaption class="image-caption">Paper: Improving language models by retrieving from trillions of tokens <a href="https://arxiv.org/abs/2112.04426">[Here]</a></figcaption></figure></div><p> RETRO also uses a language model and a system to find information. But it tries to be efficient, meaning it works well without using too much computing power. RETRO is great for giving detailed answers to complicated questions. The trade-off is that it sometimes doesn't go as deep into the information as it could because it wants to stay efficient.</p><h4><strong>RETRO Mechanism:</strong></h4><p>Unlike models that solely rely on pre-trained knowledge or integrate retrieval in a multi-step process, RETRO embeds retrieval into the core of its language generation mechanism.</p><p>At its core, RETRO operates through a mechanism that involves two key components:</p><ol><li><p><strong>Retrieval Component</strong>: This part of RETRO is responsible for scanning through a vast database of text segments to find pieces of information relevant to a given query. The retrieval process is designed to be highly efficient, focusing on extracting the most pertinent information with minimal computational overhead.</p></li><li><p><strong>Language Generation Component</strong>: Following the retrieval of relevant text segments, the language model component of RETRO takes over. It uses the retrieved information along with the original query to construct a coherent and contextually appropriate response.</p></li></ol><p>The operational equation for RETRO can be represented as follows:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(A|Q,R_1,R_2, ..., R_n) &quot;,&quot;id&quot;:&quot;RMXCNMDNUL&quot;}" data-component-name="LatexBlockToDOM"></div><p>In this equation, <em>A</em> represents the answer generated by the model, <em>Q</em> is the input query, and <em>R</em>1&#8203;,<em>R</em>2&#8203;,&#8230;,<em>Rn</em>&#8203; are the retrieved text segments relevant to the query. This formulation underscores RETRO's focus on efficiently leveraging external information (the retrieved segments) in concert with the query to produce a relevant response.</p><p>RETRO&#8217;s mechanism is distinct in its emphasis on the efficiency of retrieval and the integration of this retrieval directly into the response generation process. By doing so, RETRO aims to deliver highly contextual and accurate answers, even for complex queries, while maintaining computational efficiency. This balance makes RETRO a unique and valuable addition to the landscape of advanced language models.</p><h4><strong>Effortlessly Implementing RETRO with Hugging Face</strong></h4><p>For those interested in experimenting with the RETRO model, a convenient and effective approach is to utilize the resources offered by Hugging Face's Transformers library. </p><p>To start using RETRO with Hugging Face, here are some basic steps:</p><ol><li><p><strong>Installation</strong>: Ensure that the Transformers and Torch libraries are installed in your Python environment. You can easily install these using pip.</p></li><li><p><strong>Initialization</strong>: As with other models, you will need to initialize the necessary components &#8211; the tokenizer and the RETRO model itself. Hugging Face provides pre-trained models that are ready for immediate use.</p></li><li><p><strong>Load the RETRO Model</strong>: Accessing Hugging Face's model repository, you can load a version of RETRO that has been pre-trained on a wide range of data. This pre-training makes the model robust and versatile for various applications.</p></li></ol><p>Here's a simple example to illustrate basic usage:</p><pre><code># Import necessary classes
from transformers import RetroTokenizer, RetroForCausalLM

# Initialize the tokenizer and model
tokenizer = RetroTokenizer.from_pretrained("google/retro-large")
model = RetroForCausalLM.from_pretrained("google/retro-large")

# Example query
query = "What are the benefits of renewable energy?"

# Tokenize and process the query
inputs = tokenizer(query, return_tensors="pt")
output = model.generate(inputs.input_ids)

# Decode and display the response
response = tokenizer.decode(output[0], skip_special_tokens=True)
print(f"Response: {response}")
</code></pre><p></p><p></p><p><strong>Comparison and Future Directions:</strong></p><p> RAG, REALM, and RETRO all try to improve language models by using outside information. They do it in different ways: RAG adds extra info to what it already knows, REALM learns to find info and answer questions simultaneously, and RETRO focuses on being efficient. In the future, we might see models that can use the latest info, utilizing semantic search methodologies to understand context better.</p><p>RAG, REALM, and RETRO are important steps in making language models that understand and use information better. Each one has its own strengths. They show us how AI can use outside information to improve. Later in another post, I will write about the coding part (which is easy, since it will use HuggingFace libraries)</p>]]></content:encoded></item><item><title><![CDATA[Connecting Stochastic Calculus and Deep Learning in Finance]]></title><description><![CDATA[One Way to Explore the Magic of Combining Fields for Better Understanding]]></description><link>https://www.gofar.ai/p/connecting-stochastic-calculus-and</link><guid isPermaLink="false">https://www.gofar.ai/p/connecting-stochastic-calculus-and</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Tue, 01 Aug 2023 08:36:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Meub!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg" 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_!Meub!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Meub!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Meub!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Meub!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg" width="1456" height="1092" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3095985,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&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_!Meub!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Meub!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Meub!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6afedccf-3f28-40b5-a618-5c8e386f8583_4032x3024.jpeg 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><figcaption class="image-caption">Image by Author.</figcaption></figure></div><p>One effective way to grasp complex fields is by applying them in conjunction with other disciplines, and vice versa. The process of exploring two different fields can lead to a more efficient understanding of both. In my case, I endeavored to establish a connection between stochastic calculus (specifically, quantitative finance) and machine learning (specifically, deep learning).</p><p>This journey proved to be enriching, and I relied on just three reference books throughout the process. I highly recommend these books to anyone interested in the quantitative field, even if they are not specifically interested in stochastic calculus. Interestingly, these references can directly relate to the Bayesian approach in deep reinforcement learning and natural language processing as well. The exciting part is experimenting jointly, like estimating volatility in the Black-Scholes model using GARCH and using it as input for an LSTM network.</p><p>Let me briefly introduce the three books:</p><ol><li><p><strong>Brownian Motion Calculus:</strong> After exploring various books, I found this to be the most accessible one, allowing for easy absorption of knowledge in all aspects of stochastic calculus, including topics like change of measure and change of numerire in probability theory.</p></li><li><p><strong>The Volatility Smile:</strong> This is an advanced yet practical book suitable for experienced quants, covering both fundamental concepts and derivations of the Jump-Diffusion model, along with solutions for advanced stochastic volatility models. I would suggest apporach this book after finishing the first book &#8220;Browninan motion calculus.</p></li><li><p><strong>ML with SciKit-Learn, Keras, and TensorFlow: </strong>This amazing book serves as a great reference for both Machine Learning and Deep Learning in TensorFlow. It comprehensively covers ML basics and even extends to transformer architectures. I used this book alongside stochastic calculus as a coding reference, helping me develop algorithmic codes for my work.</p></li></ol><p>Overall, these three books provided invaluable insights and contributed significantly to my understanding and practical applications.</p>]]></content:encoded></item><item><title><![CDATA[Moving Beyond Language Models: Why Deep Reinforcement Learning is Key to Achieving AGI]]></title><description><![CDATA[Moving Beyond Pre-Trained Models and Fostering Independent Thought through Deep Reinforcement Learning, with DRL example.]]></description><link>https://www.gofar.ai/p/moving-beyond-language-models-why</link><guid isPermaLink="false">https://www.gofar.ai/p/moving-beyond-language-models-why</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Tue, 02 May 2023 04:14:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!WSPu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.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_!WSPu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WSPu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 424w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 848w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1272w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WSPu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png" width="1456" height="701" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:701,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2333756,&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_!WSPu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 424w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 848w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.png 1272w, https://substackcdn.com/image/fetch/$s_!WSPu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb78bda1-1326-48d3-a46e-907913bfa42a_2234x1076.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>Artificial Intelligence (AI) has come a long way since its inception. We have witnessed remarkable advances in natural language processing (NLP) and computer vision, among other areas. However, despite these achievements, AI still has a long way to go before it can truly be considered intelligent. In particular, while AI models such as GPT, BARTm, and their ilk have been able to perform impressive tasks, they are limited by their reliance on past experiences and their inability to generate truly original ideas. In order to attain true artificial general intelligence (AGI), we must look beyond these models and explore other avenues that can enable machines to develop independent thinking and creativity. One such avenue is deep reinforcement learning (DRL).</p><p>DRL is a type of machine learning that enables an agent to learn by interacting with its environment. In contrast to pre-trained models like GPT, which have a fixed set of parameters, DRL-based agents can adapt and learn from their experiences, enabling them to perform complex tasks that require a degree of creativity and independent thought. DRL is based on the principle of reward-based learning, where an agent is rewarded for taking actions that lead to positive outcomes, and penalized for taking actions that lead to negative outcomes. Over time, the agent learns to take actions that maximize its reward, thereby achieving its goals.</p><p>One of the key advantages of DRL is its ability to generate truly novel ideas. Unlike pre-trained models, which are limited by their dependence on past experiences, DRL-based agents can explore new and uncharted territory. By interacting with their environment and experiencing unique situations, DRL-based agents can develop a level of creativity and independent thought that is not possible with pre-trained models.</p><p>Another advantage of DRL is its ability to learn from its mistakes. In traditional machine learning, agents are trained using a large dataset of examples. While this approach can be effective, it does not allow for the agent to learn from its own mistakes. DRL, on the other hand, enables the agent to learn from its own experiences, allowing it to adapt and improve over time.</p><p>DRL has already been applied successfully in a number of domains, including robotics, gaming, and finance. In robotics, DRL-based agents have been able to perform complex tasks such as grasping objects and navigating through environments. In gaming, DRL-based agents have been able to outperform human players in games such as Go and chess. In finance, DRL-based agents have been able to make investment decisions that outperform human traders.</p><p>Despite the potential of DRL, there are still significant challenges that need to be overcome before it can be used to develop AGI. One of the biggest challenges is the need for large amounts of data. DRL requires a significant amount of data to train the agent effectively. This data must be diverse and representative of the real-world environment that the agent will be operating in. Another challenge is the need for efficient algorithms that can learn from this data in a reasonable amount of time. Finally, there is the challenge of designing a reward function that accurately reflects the agent's goals and objectives.</p><p>In conclusion, while pre-trained models like GPT have brought us closer to the goal of AGI, they are only a small step on the path towards true intelligence. To achieve AGI, we must look beyond these models and explore other avenues that can enable machines to develop independent thinking and creativity. DRL is one such avenue. By allowing machines to interact with their environment and experience unique situations, DRL-based agents can develop a level of creativity and independent thought that is not possible with pre-trained models. While there are still significant challenges that need to be overcome, DRL has the potential to revolutionize the field of AI and bring us closer to achieving true AGI.</p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Customizing Deep Neural Networks with TensorFlow: Creating a Custom Layer]]></title><description><![CDATA[In this post, we will explore how to create a custom layer in TensorFlow, which can be useful when we need to add a specific functionality that is not available in the existing layers.]]></description><link>https://www.gofar.ai/p/customizing-deep-neural-networks</link><guid isPermaLink="false">https://www.gofar.ai/p/customizing-deep-neural-networks</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Mon, 19 Sep 2022 05:02:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!geLi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this post, we will explore how to create a custom layer in TensorFlow, which can be useful when we need to add a specific functionality that is not available in the existing layers.</p><p>TensorFlow is a powerful deep learning library that provides a wide range of pre-built layers such as convolutional, pooling, and dense layers. However, in some cases, we might need to implement a custom layer with a specific functionality.</p><p>To create a custom layer, we need to define a class that inherits from the <code>tf.keras.layers.Layer</code> class. This class provides several methods that need to be implemented:</p><ul><li><p><code>__init__()</code>: This method initializes the layer and defines its parameters.</p></li><li><p><code>build()</code>: This method creates the layer's variables, which are the weights and biases that will be learned during training.</p></li><li><p><code>call()</code>: This method performs the forward pass of the layer, which computes the output of the layer given its input.</p></li></ul><p>Let's dive into an example of creating a custom layer in TensorFlow.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!geLi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!geLi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 424w, https://substackcdn.com/image/fetch/$s_!geLi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 848w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!geLi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png" width="1348" height="1008" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1008,&quot;width&quot;:1348,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:227275,&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_!geLi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 424w, https://substackcdn.com/image/fetch/$s_!geLi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 848w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.png 1272w, https://substackcdn.com/image/fetch/$s_!geLi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97644cfb-60e8-40ea-b9bd-e5a30c5cdd44_1348x1008.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>In this code, we define a custom layer in TensorFlow called <code>CustomLayer</code>. The layer takes an input tensor and returns the result of multiplying it by a weight matrix. The size of the weight matrix is specified by the <code>output_dim</code> parameter, which is passed to the layer's constructor.</p><p>The layer's <code>build</code> method is called when the layer is first used in a model. In this method, we create the weight matrix by calling the <code>add_weight</code> method of the layer, and set its shape and initializer. We also call the <code>build</code> method of the parent class to finish building the layer.</p><p>The layer's <code>call</code> method is called when the layer is applied to an input tensor. In this method, we multiply the input tensor by the weight matrix using TensorFlow's <code>matmul</code> function.</p><p>Finally, the layer's <code>compute_output_shape</code> method is called to determine the output shape of the layer based on the input shape.</p><p>Overall, this custom layer allows us to define a new type of layer in TensorFlow that can be used in deep neural networks. We can customize the behavior of the layer by modifying its <code>build</code> and <code>call</code> methods to suit our needs.</p>]]></content:encoded></item><item><title><![CDATA[Quantifying the Uncertainty in Deep Bayesian Q-Networks for Robust Decision Making]]></title><description><![CDATA[Deep Q-Networks (DQN) are a powerful class of reinforcement learning algorithms that have been successfully used in various applications, such as robotics, game playing, and finance.]]></description><link>https://www.gofar.ai/p/quantifying-the-uncertainty-in-deep</link><guid isPermaLink="false">https://www.gofar.ai/p/quantifying-the-uncertainty-in-deep</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Wed, 29 Jun 2022 04:52:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!dmGC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.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_!dmGC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dmGC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 424w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 848w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1272w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dmGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png" width="1456" height="531" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:531,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:979753,&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_!dmGC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 424w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 848w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.png 1272w, https://substackcdn.com/image/fetch/$s_!dmGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F990d5198-de62-4111-a244-58d3dcee0ed2_2984x1088.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>Deep Q-Networks (DQN) are a powerful class of reinforcement learning algorithms that have been successfully used in various applications, such as robotics, game playing, and finance. However, one challenge with DQNs is their lack of robustness to uncertainties in the environment, which can result in suboptimal or unsafe decisions. In this blog post, we will discuss how to quantify the uncertainty in DQNs using Bayesian deep learning, and how to use this uncertainty to make more robust decisions.</p><p><strong>Bayesian Deep Learning</strong></p><p>Bayesian deep learning is a framework that combines deep learning with Bayesian inference to quantify the uncertainty in neural network models. In Bayesian deep learning, we treat the weights of the neural network as random variables, and we define a prior distribution over these weights. We then use Bayes' rule to update the prior distribution to a posterior distribution, given the observed data. The posterior distribution represents our updated belief about the weights, given the data.</p><p><strong>Uncertainty in DQNs</strong></p><p>In DQNs, the uncertainty arises from two sources: the stochasticity of the environment, and the uncertainty in the neural network model. The stochasticity of the environment refers to the randomness in the outcomes of the actions taken by the agent, due to the inherent randomness in the environment. The uncertainty in the neural network model refers to our uncertainty about the optimal actions given the current state of the environment, which is represented by the Q-values predicted by the neural network.</p><p><strong>Bayesian DQN</strong></p><p>To quantify the uncertainty in DQNs, we can use Bayesian deep learning to model the uncertainty in the neural network weights. Specifically, we can use a Bayesian neural network (BNN), which is a neural network with weights treated as random variables. We can then use Monte Carlo dropout (MC dropout) to approximate the Bayesian inference process. MC dropout involves adding dropout at test time and sampling multiple predictions from the network to estimate the distribution of the predictions.</p><p>The loss function for training the Bayesian DQN is the negative log-likelihood of the observed data, which includes the rewards received and the transitions between states. The loss function is modified to include a penalty term for the entropy of the distribution over the Q-values, which encourages exploration and reduces overconfidence in the predictions.</p><p><strong>Python Implementation</strong></p><p>To implement a Bayesian DQN in Python using TensorFlow, we can start with the standard DQN implementation and modify it to use a BNN and MC dropout. The following code shows an example of how to modify the Q-network in a DQN to use a BNN and MC dropout:</p><pre><code>class BayesianQNetwork(tf.keras.Model):
    def __init__(self, num_actions, num_hidden_units):
        super(BayesianQNetwork, self).__init__()
        self.num_actions = num_actions
        self.dense1 = tf.keras.layers.Dense(num_hidden_units, activation='relu')
        self.dense2 = tf.keras.layers.Dense(num_hidden_units, activation='relu')
        self.logits = tf.keras.layers.Dense(num_actions)

    def call(self, inputs):
        x = self.dense1(inputs)
        x = self.dense2(x)
        logits = self.logits(x)
        return logits

    def sample_predictions(self, inputs, num_samples=10):
        outputs = []
        for _ in range(num_samples):
            outputs.append(self(inputs))
        return tf.stack(outputs)</code></pre><div><hr></div><p>In this code, the Q-network is defined as a BNN with two hidden layers and a softmax output layer. The <code>sample_predictions</code> function is used to sample predictions from the network using MC dropout.</p><p>To modify the loss function to include the penalty term for entropy, we can use the following code:</p><pre><code>def bayesian_loss(model, states, targets, num_samples=10):
    """
    Computes the Bayesian loss of a model given the states and targets.
    
    Arguments:
    model -- the deep Q-network model
    states -- a batch of input states (numpy array of shape (batch_size, state_size))
    targets -- a batch of target Q-values (numpy array of shape (batch_size, num_actions))
    num_samples -- the number of samples to draw from the posterior distribution (default 10)
    
    Returns:
    The Bayesian loss (scalar).
    """
    # Compute the predicted Q-values and log variance for each state-action pair
    q_values = []
    log_variances = []
    for i in range(num_samples):
        q_values_i, log_variances_i = model(states, sample=True)
        q_values.append(q_values_i)
        log_variances.append(log_variances_i)
    q_values = tf.stack(q_values)  # shape: (num_samples, batch_size, num_actions)
    log_variances = tf.stack(log_variances)  # shape: (num_samples, batch_size, num_actions)
    
    # Compute the mean and variance of the predicted Q-values and log variances
    q_mean = tf.reduce_mean(q_values, axis=0)  # shape: (batch_size, num_actions)
    q_var = tf.math.reduce_variance(q_values, axis=0)  # shape: (batch_size, num_actions)
    log_var_mean = tf.reduce_mean(log_variances, axis=0)  # shape: (batch_size, num_actions)
    
    # Compute the Bayesian loss
    precision = tf.exp(-log_var_mean)
    loss = 0.5 * precision * tf.reduce_sum(tf.square(targets - q_mean), axis=-1) + \
           0.5 * tf.math.log(1 + q_var * precision)  # shape: (batch_size,)
    loss = tf.reduce_mean(loss)  # take the mean over the batch
    
    return loss
</code></pre><div><hr></div>]]></content:encoded></item><item><title><![CDATA[Supervised vs Unsupervised Machine Learning: What's the Difference?]]></title><description><![CDATA[Machine learning is a powerful tool that has revolutionized many industries.]]></description><link>https://www.gofar.ai/p/supervised-vs-unsupervised-machine</link><guid isPermaLink="false">https://www.gofar.ai/p/supervised-vs-unsupervised-machine</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sat, 16 Apr 2022 05:07:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!cuFZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.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_!cuFZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cuFZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 424w, https://substackcdn.com/image/fetch/$s_!cuFZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 848w, https://substackcdn.com/image/fetch/$s_!cuFZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!cuFZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cuFZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png" width="1456" height="576" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:576,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:622703,&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_!cuFZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 424w, https://substackcdn.com/image/fetch/$s_!cuFZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 848w, https://substackcdn.com/image/fetch/$s_!cuFZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!cuFZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb17f510d-7ebd-4efb-98d4-95d54e6c40bf_2650x1048.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>Machine learning is a powerful tool that has revolutionized many industries. One of the main goals of machine learning is to teach computers how to learn patterns from data. There are two main types of machine learning: supervised and unsupervised.</p><p><strong>Supervised Machine Learning</strong></p><p>Supervised machine learning is a type of machine learning where the computer is given a labeled dataset to learn from. In other words, the computer is provided with input data (also called features) and corresponding output data (also called labels or targets) to learn from. The goal of supervised learning is to learn a mapping function that can accurately predict the output for new input data.</p><p>For example, let's say you want to build a machine learning model to predict the price of a house based on its size, location, and number of bedrooms. You would start by collecting a dataset of houses that includes their sizes, locations, number of bedrooms, and their corresponding prices. This dataset is the labeled dataset that you would use to train your supervised learning model.</p><p>The supervised learning algorithm would take this labeled dataset as input and learn a mapping function between the input features (size, location, and number of bedrooms) and the output label (price). Once the model is trained, you can use it to predict the price of a new house based on its size, location, and number of bedrooms.</p><p>Some popular examples of supervised learning algorithms include linear regression, decision trees, random forests, and neural networks.</p><p><strong>Unsupervised Machine Learning</strong></p><p>Unsupervised machine learning, on the other hand, is a type of machine learning where the computer is given an unlabeled dataset to learn from. In other words, the computer is provided with input data but without corresponding output data. The goal of unsupervised learning is to find patterns and structure in the input data.</p><p>For example, let's say you want to analyze customer data to identify different groups of customers based on their purchasing behavior. You would start by collecting a dataset of customer transactions that includes information such as purchase date, item purchased, and price. This dataset is the unlabeled dataset that you would use to train your unsupervised learning model.</p><p>The unsupervised learning algorithm would take this unlabeled dataset as input and analyze the patterns and structure in the data to identify different groups of customers based on their purchasing behavior. Once the model is trained, you can use it to segment your customers into different groups based on their purchasing behavior.</p><p>Some popular examples of unsupervised learning algorithms include clustering, principal component analysis (PCA), and autoencoders.</p><p><strong>Conclusion</strong></p><p>In summary, supervised and unsupervised machine learning are two main types of machine learning. Supervised learning involves learning from labeled data, while unsupervised learning involves learning from unlabeled data. Both types of machine learning have their own unique applications and use cases, and choosing the right type of machine learning algorithm depends on the specific problem you are trying to solve.</p>]]></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[The Basics of Python: SciKit-Learn]]></title><description><![CDATA[Scikit-learn is a Python library that is widely used for machine learning tasks such as classification, regression, and clustering.]]></description><link>https://www.gofar.ai/p/the-basics-of-python-scikit-learn</link><guid isPermaLink="false">https://www.gofar.ai/p/the-basics-of-python-scikit-learn</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sun, 06 Mar 2022 14:41:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!_E-j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.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_!_E-j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_E-j!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 424w, https://substackcdn.com/image/fetch/$s_!_E-j!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 848w, https://substackcdn.com/image/fetch/$s_!_E-j!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 1272w, https://substackcdn.com/image/fetch/$s_!_E-j!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_E-j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png" width="1456" height="908" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:908,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:761071,&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_!_E-j!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 424w, https://substackcdn.com/image/fetch/$s_!_E-j!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 848w, https://substackcdn.com/image/fetch/$s_!_E-j!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.png 1272w, https://substackcdn.com/image/fetch/$s_!_E-j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f84162-a6ec-41a7-a000-ff975aad853b_2122x1323.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>Scikit-learn is a Python library that is widely used for machine learning tasks such as classification, regression, and clustering. It provides a wide range of machine learning algorithms and tools for data preprocessing, model selection, and evaluation.</p><p>Some of the most important functions in scikit-learn include:</p><ol><li><p><code>sklearn.model_selection.train_test_split()</code>: splits data into training and testing sets for model training and evaluation</p></li><li><p><code>sklearn.preprocessing.StandardScaler()</code>: scales data to have a mean of 0 and a standard deviation of 1</p></li><li><p><code>sklearn.pipeline.Pipeline()</code>: chains together multiple machine learning steps into a single pipeline</p></li><li><p><code>sklearn.linear_model.LogisticRegression()</code>: performs logistic regression for binary classification problems</p></li><li><p><code>sklearn.ensemble.RandomForestClassifier()</code>: performs random forest classification for both binary and multiclass problems</p></li><li><p><code>sklearn.cluster.KMeans()</code>: performs K-means clustering for unsupervised learning problems</p></li><li><p><code>sklearn.metrics.accuracy_score()</code>: computes the accuracy of a machine learning model on test data</p></li><li><p><code>sklearn.metrics.precision_score()</code>: computes the precision of a machine learning model on test data</p></li><li><p><code>sklearn.metrics.recall_score()</code>: computes the recall of a machine learning model on test data</p></li><li><p><code>sklearn.metrics.f1_score()</code>: computes the F1 score (harmonic mean of precision and recall) of a machine learning model on test data</p></li></ol><p>Here's an example of how to use scikit-learn to build and evaluate a machine learning model:</p><pre><code>import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# load data from a CSV file
data = pd.read_csv('data.csv')

# split data into features (X) and target (y)
X = data.drop(columns='target')
y = data['target']

# split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# create a pipeline with scaling and logistic regression steps
pipeline = Pipeline(steps=[('scaler', StandardScaler()), ('logreg', LogisticRegression())])

# fit the pipeline on the training data
pipeline.fit(X_train, y_train)

# predict the target values for the test data
y_pred = pipeline.predict(X_test)

# evaluate the accuracy of the model on the test data
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)</code></pre>]]></content:encoded></item><item><title><![CDATA[The Basics of Python: Pandas]]></title><description><![CDATA[An Introduction to Python's Pandas Library for Data Manipulation and Analysis]]></description><link>https://www.gofar.ai/p/the-basics-of-python-pandas</link><guid isPermaLink="false">https://www.gofar.ai/p/the-basics-of-python-pandas</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Wed, 23 Feb 2022 01:59:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!F8V_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.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_!F8V_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!F8V_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 424w, https://substackcdn.com/image/fetch/$s_!F8V_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 848w, https://substackcdn.com/image/fetch/$s_!F8V_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 1272w, https://substackcdn.com/image/fetch/$s_!F8V_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!F8V_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png" width="1200" height="485" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:485,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:23707,&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_!F8V_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 424w, https://substackcdn.com/image/fetch/$s_!F8V_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 848w, https://substackcdn.com/image/fetch/$s_!F8V_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.png 1272w, https://substackcdn.com/image/fetch/$s_!F8V_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b68ce5-9ebc-473c-b70b-26637b16d7e2_1200x485.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>Pandas is a Python library that is widely used for data manipulation and analysis. It provides powerful data structures and functions for working with structured data.</p><p>Some of the most important functions in Pandas include:</p><ol><li><p><code>pd.DataFrame()</code>: creates a Pandas DataFrame from a Python dictionary or array</p></li><li><p><code>df.head()</code>: returns the first few rows of a DataFrame</p></li><li><p><code>df.tail()</code>: returns the last few rows of a DataFrame</p></li><li><p><code>df.info()</code>: returns information about the data types and null values in a DataFrame</p></li><li><p><code>df.describe()</code>: returns descriptive statistics about the data in a DataFrame</p></li><li><p><code>df.groupby()</code>: groups data in a DataFrame by one or more columns</p></li><li><p><code>df.merge()</code>: merges two DataFrames based on a common column</p></li><li><p><code>df.sort_values()</code>: sorts a DataFrame by one or more columns</p></li><li><p><code>df.drop()</code>: drops rows or columns from a DataFrame</p></li><li><p><code>df.fillna()</code>: fills null values in a DataFrame with a specified value or method</p></li></ol><p>Here's an example of how to create a Pandas DataFrame and use some of these functions:</p><pre><code>import pandas as pd

# create a DataFrame from a Python dictionary
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
        'Age': [25, 30, 35, 40, 45],
        'City': ['New York', 'Paris', 'London', 'Tokyo', 'Sydney']}
df = pd.DataFrame(data)

# print the first few rows of the DataFrame
print(df.head())

# print information about the data types and null values in the DataFrame
print(df.info())

# compute descriptive statistics about the data in the DataFrame
print(df.describe())

# group the data by the City column and compute the mean of the Age column
print(df.groupby('City').mean())

# create a second DataFrame with additional data
data2 = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
         'Salary': [50000, 60000, 70000, 80000, 90000]}
df2 = pd.DataFrame(data2)

# merge the two DataFrames based on the Name column
merged_df = df.merge(df2, on='Name')

# sort the merged DataFrame by the Age column
sorted_df = merged_df.sort_values(by='Age')

# drop the City column from the sorted DataFrame
final_df = sorted_df.drop(columns='City')

# fill null values in the Salary column with the mean of the column
final_df['Salary'] = final_df['Salary'].fillna(final_df['Salary'].mean())

# print the final DataFrame
print(final_df)</code></pre>]]></content:encoded></item><item><title><![CDATA[The Basics of Python: Numpy]]></title><description><![CDATA[An Introduction to NumPy: The Essential Python Library for Scientific Computing]]></description><link>https://www.gofar.ai/p/the-basics-of-python-numpy</link><guid isPermaLink="false">https://www.gofar.ai/p/the-basics-of-python-numpy</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Wed, 02 Feb 2022 22:30:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!3eXM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.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_!3eXM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3eXM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 424w, https://substackcdn.com/image/fetch/$s_!3eXM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 848w, https://substackcdn.com/image/fetch/$s_!3eXM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 1272w, https://substackcdn.com/image/fetch/$s_!3eXM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3eXM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp" width="560" height="315" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/124cdec4-4918-493d-9905-107309671de2_560x315.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:315,&quot;width&quot;:560,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2798,&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_!3eXM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 424w, https://substackcdn.com/image/fetch/$s_!3eXM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 848w, https://substackcdn.com/image/fetch/$s_!3eXM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.webp 1272w, https://substackcdn.com/image/fetch/$s_!3eXM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F124cdec4-4918-493d-9905-107309671de2_560x315.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>NumPy is a Python library that is widely used for numerical and scientific computing. It provides a powerful array object and functions for working with these arrays.</p><p>Some of the most important functions in NumPy include:</p><ol><li><p><code>np.array()</code>: creates a NumPy array from a Python list or tuple</p></li><li><p><code>np.zeros()</code>: creates an array filled with zeros</p></li><li><p><code>np.ones()</code>: creates an array filled with ones</p></li><li><p><code>np.arange()</code>: creates an array with a range of values</p></li><li><p><code>np.linspace()</code>: creates an array with a specific number of values evenly spaced between two endpoints</p></li><li><p><code>np.random.rand()</code>: creates an array with random values between 0 and 1</p></li><li><p><code>np.random.randn()</code>: creates an array with random values from a normal distribution</p></li><li><p><code>np.min()</code>: returns the minimum value of an array</p></li><li><p><code>np.max()</code>: returns the maximum value of an array</p></li><li><p><code>np.mean()</code>: returns the mean of an array</p></li><li><p><code>np.std()</code>: returns the standard deviation of an array</p></li><li><p><code>np.sum()</code>: returns the sum of an array</p></li></ol><p>Here's an example of how to create a NumPy array and use some of these functions:</p><pre><code>import numpy as np

# create a 1-dimensional array from a Python list
my_array = np.array([1, 2, 3, 4, 5])

# create a 2-dimensional array filled with zeros
zeros_array = np.zeros((3, 4))

# create a 1-dimensional array with values from 0 to 9
range_array = np.arange(10)

# create a 1-dimensional array with 5 values between 0 and 1
linspace_array = np.linspace(0, 1, 5)

# create a 3x3 array with random values between 0 and 1
random_array = np.random.rand(3, 3)

# compute the minimum value of an array
min_value = np.min(my_array)

# compute the mean of an array
mean_value = np.mean(random_array)</code></pre>]]></content:encoded></item><item><title><![CDATA["Go Far" Philosophy: A Dual-Level Journey through AI Fundamentals and Advanced Topics. ]]></title><description><![CDATA[Explore the World of AI with Our Comprehensive Dual-Level Guide]]></description><link>https://www.gofar.ai/p/go-far-philosophy-a-dual-level-journey</link><guid isPermaLink="false">https://www.gofar.ai/p/go-far-philosophy-a-dual-level-journey</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Fri, 28 Jan 2022 01:46:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!71Uo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.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_!71Uo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!71Uo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 424w, https://substackcdn.com/image/fetch/$s_!71Uo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 848w, https://substackcdn.com/image/fetch/$s_!71Uo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 1272w, https://substackcdn.com/image/fetch/$s_!71Uo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!71Uo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png" width="1456" height="711" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:711,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4524651,&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_!71Uo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 424w, https://substackcdn.com/image/fetch/$s_!71Uo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 848w, https://substackcdn.com/image/fetch/$s_!71Uo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.png 1272w, https://substackcdn.com/image/fetch/$s_!71Uo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b8e3c99-08d8-4fc9-a4fb-8a67c4df2b74_2232x1090.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><figcaption class="image-caption">Artwork by <strong>Irina Lisichkina</strong></figcaption></figure></div><p>Our Blog: A Dual-Level Journey through AI Fundamentals and Advanced Topics.</p><p>At 'Go Far,' we cater to both beginners and seasoned experts in the field of Artificial Intelligence. Our blog consists of two levels: Zero and Hero.</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><p>The Zero level focuses on the essential foundations of AI theory and coding, making it an indispensable resource for novices. It is also a valuable refresher for experts who wish to revisit their knowledge from time to time.</p><p>The Hero level caters to ML engineers with a strong foundational understanding of AI. It delves into advanced, up-to-date topics, providing valuable insights and cutting-edge developments in the field.</p><p>Join us on this journey through AI, and take your skills to the next level with 'Go Far.'</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[Go Far: Exploring Advanced AI Topics with Strong Foundations"]]></title><description><![CDATA[Foundational Focus: An Excursion through Advanced AI Topics in NLP, DRL, and Computer Vision.]]></description><link>https://www.gofar.ai/p/go-far-exploring-advanced-ai-topics</link><guid isPermaLink="false">https://www.gofar.ai/p/go-far-exploring-advanced-ai-topics</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Sun, 16 Jan 2022 18:58:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!55IC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.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_!55IC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!55IC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 424w, https://substackcdn.com/image/fetch/$s_!55IC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 848w, https://substackcdn.com/image/fetch/$s_!55IC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 1272w, https://substackcdn.com/image/fetch/$s_!55IC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!55IC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png" width="1456" height="721" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:721,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:390823,&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_!55IC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 424w, https://substackcdn.com/image/fetch/$s_!55IC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 848w, https://substackcdn.com/image/fetch/$s_!55IC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.png 1272w, https://substackcdn.com/image/fetch/$s_!55IC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f54e4c6-78f7-4e14-98a2-b054342f8154_1632x808.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><figcaption class="image-caption">This artwork by <strong>Anton Egorov</strong></figcaption></figure></div><p>Upon the completion of my master's degree in 2014, I delved into the realm of machine learning, particularly its applications in quantitative finance and trading. Over the next few years, I employed machine learning algorithms in my personal trading ventures and family business, eventually developing a passion for more advanced topics in AI, such as natural language processing (NLP) and deep reinforcement learning (DRL).</p><p>In 2020, I made the decision to shift my focus to the broader field of AI, rather than solely employing these algorithms in the context of trading. Consequently, I began exploring the domains of computer vision and stable diffusion. The remarkable aspect of the machine learning field that I have discovered is that its scope is vast, and one can never claim to have fully mastered all its intricacies. I believe that one way to stay abreast of the latest developments in AI is to establish a strong foundation of fundamental concepts, and then specialize in a particular domain while constantly revisiting and reinforcing the foundational knowledge.</p><p>With this approach in mind, I conceived the idea of creating a blog named "Go Far." The blog aims to provide a comprehensive reference point for the fundamental concepts of AI while simultaneously delving deeply into specific subfields of the domain. The name "Go Far" reflects the notion of pushing boundaries and continuously striving to advance one's knowledge in the field of AI.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.gofar.ai/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.gofar.ai/subscribe?"><span>Subscribe now</span></a></p><p></p><p>. </p><div><hr></div><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 Ali&#8217;s Substack! 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><p></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><item><title><![CDATA[Recap the Fundamentals of Data Analysis with Pandas: A Simple Code-Driven Approach]]></title><description><![CDATA[Let's review the basic of Pandas with these basic functions.]]></description><link>https://www.gofar.ai/p/recap-the-fundamentals-of-data-analysis</link><guid isPermaLink="false">https://www.gofar.ai/p/recap-the-fundamentals-of-data-analysis</guid><dc:creator><![CDATA[Ali Atiah Alzahrani]]></dc:creator><pubDate>Mon, 04 May 2020 06:26:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!TmQp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.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_!TmQp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TmQp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 424w, https://substackcdn.com/image/fetch/$s_!TmQp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 848w, https://substackcdn.com/image/fetch/$s_!TmQp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 1272w, https://substackcdn.com/image/fetch/$s_!TmQp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TmQp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png" width="1456" height="534" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e095401a-e91e-4535-97e2-14992be4b26a_2024x742.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;:1689864,&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_!TmQp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 424w, https://substackcdn.com/image/fetch/$s_!TmQp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 848w, https://substackcdn.com/image/fetch/$s_!TmQp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.png 1272w, https://substackcdn.com/image/fetch/$s_!TmQp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe095401a-e91e-4535-97e2-14992be4b26a_2024x742.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>As a data scientist, it can be easy to get lost in the overwhelming number of advanced Python libraries available. However, mastering the basics is essential for building a strong foundation in data science. In this blog post, we will explore the powerful yet elegant Pandas library and provide code examples for some of its fundamental functions. Whether you are a beginner or an experienced practitioner, these examples will help you reinforce your understanding of Pandas and improve your data analysis skills.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!q8ff!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!q8ff!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 424w, https://substackcdn.com/image/fetch/$s_!q8ff!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 848w, https://substackcdn.com/image/fetch/$s_!q8ff!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 1272w, https://substackcdn.com/image/fetch/$s_!q8ff!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!q8ff!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png" width="856" height="152" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:152,&quot;width&quot;:856,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:27818,&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;: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_!q8ff!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 424w, https://substackcdn.com/image/fetch/$s_!q8ff!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 848w, https://substackcdn.com/image/fetch/$s_!q8ff!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 1272w, https://substackcdn.com/image/fetch/$s_!q8ff!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18f8e5c3-ee4f-436f-a3e4-4e900b2ab0dd_856x152.png 1456w" sizes="100vw"></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_!2wL4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2wL4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 424w, https://substackcdn.com/image/fetch/$s_!2wL4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 848w, https://substackcdn.com/image/fetch/$s_!2wL4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 1272w, https://substackcdn.com/image/fetch/$s_!2wL4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2wL4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png" width="1160" height="344" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:344,&quot;width&quot;:1160,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:73517,&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;: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_!2wL4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 424w, https://substackcdn.com/image/fetch/$s_!2wL4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 848w, https://substackcdn.com/image/fetch/$s_!2wL4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 1272w, https://substackcdn.com/image/fetch/$s_!2wL4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ac831dc-c788-4b9f-a647-1b71b91cb300_1160x344.png 1456w" sizes="100vw"></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" target="_blank" href="https://substackcdn.com/image/fetch/$s_!K_0b!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!K_0b!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 424w, https://substackcdn.com/image/fetch/$s_!K_0b!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 848w, https://substackcdn.com/image/fetch/$s_!K_0b!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 1272w, https://substackcdn.com/image/fetch/$s_!K_0b!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!K_0b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png" width="912" height="152" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:152,&quot;width&quot;:912,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29585,&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;: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_!K_0b!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 424w, https://substackcdn.com/image/fetch/$s_!K_0b!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 848w, https://substackcdn.com/image/fetch/$s_!K_0b!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 1272w, https://substackcdn.com/image/fetch/$s_!K_0b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab2bfa1c-6c12-4dfd-bbe2-0cbac2c166db_912x152.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><pre><code>df_head:
        name  age  gender   income  married
0      Sara   25  female  50000.0    False
1  Mohammed   30    male  60000.0     True</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!arwu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!arwu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 424w, https://substackcdn.com/image/fetch/$s_!arwu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 848w, https://substackcdn.com/image/fetch/$s_!arwu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 1272w, https://substackcdn.com/image/fetch/$s_!arwu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!arwu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png" width="900" height="152" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:152,&quot;width&quot;:900,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:28035,&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;: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_!arwu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 424w, https://substackcdn.com/image/fetch/$s_!arwu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 848w, https://substackcdn.com/image/fetch/$s_!arwu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 1272w, https://substackcdn.com/image/fetch/$s_!arwu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55c4c4c3-9495-4bfb-87eb-71c5086729ce_900x152.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><pre><code>df_tail:
      name  age  gender   income  married
2     Ali   35    male      NaN     True
3  Khaled   40    male  80000.0    False
4   Asmaa   45  female  90000.0     True</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!q1L4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!q1L4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 424w, https://substackcdn.com/image/fetch/$s_!q1L4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 848w, https://substackcdn.com/image/fetch/$s_!q1L4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 1272w, https://substackcdn.com/image/fetch/$s_!q1L4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!q1L4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png" width="882" height="146" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:882,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:27114,&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_!q1L4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 424w, https://substackcdn.com/image/fetch/$s_!q1L4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 848w, https://substackcdn.com/image/fetch/$s_!q1L4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 1272w, https://substackcdn.com/image/fetch/$s_!q1L4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F385793aa-1154-4cd7-bebf-74c62a449caa_882x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 5 entries, 0 to 4
Data columns (total 5 columns):
 #   Column   Non-Null Count  Dtype  
---  ------   --------------  -----  
 0   name     5 non-null      object 
 1   age      5 non-null      int64  
 2   gender   5 non-null      object 
 3   income   4 non-null      float64
 4   married  5 non-null      bool   
dtypes: bool(1), float64(1), int64(1), object(2)
memory usage: 293.0+ bytes
df_info:
 None</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HhLL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HhLL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 424w, https://substackcdn.com/image/fetch/$s_!HhLL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 848w, https://substackcdn.com/image/fetch/$s_!HhLL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 1272w, https://substackcdn.com/image/fetch/$s_!HhLL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HhLL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png" width="946" height="138" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f7f0f737-758f-4ae4-89c9-f71147727075_946x138.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:138,&quot;width&quot;:946,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:30705,&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_!HhLL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 424w, https://substackcdn.com/image/fetch/$s_!HhLL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 848w, https://substackcdn.com/image/fetch/$s_!HhLL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 1272w, https://substackcdn.com/image/fetch/$s_!HhLL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7f0f737-758f-4ae4-89c9-f71147727075_946x138.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_desc:
              age        income
count   5.000000      4.000000
mean   35.000000  70000.000000
std     7.905694  18257.418584
min    25.000000  50000.000000
25%    30.000000  57500.000000
50%    35.000000  70000.000000
75%    40.000000  82500.000000
max    45.000000  90000.00000</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Br9O!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Br9O!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 424w, https://substackcdn.com/image/fetch/$s_!Br9O!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 848w, https://substackcdn.com/image/fetch/$s_!Br9O!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 1272w, https://substackcdn.com/image/fetch/$s_!Br9O!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Br9O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png" width="1348" height="146" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:1348,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:44364,&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_!Br9O!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 424w, https://substackcdn.com/image/fetch/$s_!Br9O!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 848w, https://substackcdn.com/image/fetch/$s_!Br9O!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 1272w, https://substackcdn.com/image/fetch/$s_!Br9O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd3b3d3-02cf-457d-ab9b-2d6ee569e5bb_1348x146.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_sort:
        name  age  gender   income  married
0      Sara   25  female  50000.0    False
1  Mohammed   30    male  60000.0     True
2       Ali   35    male      NaN     True
3    Khaled   40    male  80000.0    False
4     Asmaa   45  female  90000.0     True</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Likg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Likg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 424w, https://substackcdn.com/image/fetch/$s_!Likg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 848w, https://substackcdn.com/image/fetch/$s_!Likg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 1272w, https://substackcdn.com/image/fetch/$s_!Likg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Likg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png" width="1228" height="144" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:144,&quot;width&quot;:1228,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:44856,&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_!Likg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 424w, https://substackcdn.com/image/fetch/$s_!Likg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 848w, https://substackcdn.com/image/fetch/$s_!Likg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 1272w, https://substackcdn.com/image/fetch/$s_!Likg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5269588d-2f38-443d-9e1c-ac3ed8614faf_1228x144.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_group:
 gender
female    70000.0
male      70000.0
Name: income, dtype: float64</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gkgN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gkgN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 424w, https://substackcdn.com/image/fetch/$s_!gkgN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 848w, https://substackcdn.com/image/fetch/$s_!gkgN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 1272w, https://substackcdn.com/image/fetch/$s_!gkgN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gkgN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png" width="1218" height="182" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/00194d34-8426-4940-9fa9-7b422f538533_1218x182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:182,&quot;width&quot;:1218,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:54032,&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_!gkgN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 424w, https://substackcdn.com/image/fetch/$s_!gkgN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 848w, https://substackcdn.com/image/fetch/$s_!gkgN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 1272w, https://substackcdn.com/image/fetch/$s_!gkgN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F00194d34-8426-4940-9fa9-7b422f538533_1218x182.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_pivot:
 married    False    True 
gender                   
female   50000.0  90000.0
male     80000.0  60000.0</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ohku!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ohku!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 424w, https://substackcdn.com/image/fetch/$s_!Ohku!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 848w, https://substackcdn.com/image/fetch/$s_!Ohku!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 1272w, https://substackcdn.com/image/fetch/$s_!Ohku!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ohku!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png" width="1188" height="140" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:140,&quot;width&quot;:1188,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:43930,&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_!Ohku!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 424w, https://substackcdn.com/image/fetch/$s_!Ohku!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 848w, https://substackcdn.com/image/fetch/$s_!Ohku!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 1272w, https://substackcdn.com/image/fetch/$s_!Ohku!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb686f3c2-0f3e-4276-8fdf-e4c43506f80c_1188x140.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_apply:
    age    income
0   50  100000.0
1   60  120000.0
2   70       NaN
3   80  160000.0
4   90  180000.0</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FM_4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FM_4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 424w, https://substackcdn.com/image/fetch/$s_!FM_4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 848w, https://substackcdn.com/image/fetch/$s_!FM_4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 1272w, https://substackcdn.com/image/fetch/$s_!FM_4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FM_4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png" width="1076" height="142" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:142,&quot;width&quot;:1076,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:34905,&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_!FM_4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 424w, https://substackcdn.com/image/fetch/$s_!FM_4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 848w, https://substackcdn.com/image/fetch/$s_!FM_4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 1272w, https://substackcdn.com/image/fetch/$s_!FM_4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F748c7ea4-6be8-4ec6-939c-cc8d378ea260_1076x142.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_fillna:
        name  age  gender   income  married
0      Sara   25  female  50000.0    False
1  Mohammed   30    male  60000.0     True
2       Ali   35    male      0.0     True
3    Khaled   40    male  80000.0    False
4     Asmaa   45  female  90000.0     True</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4Ii3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4Ii3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 424w, https://substackcdn.com/image/fetch/$s_!4Ii3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 848w, https://substackcdn.com/image/fetch/$s_!4Ii3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 1272w, https://substackcdn.com/image/fetch/$s_!4Ii3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4Ii3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png" width="856" height="140" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:140,&quot;width&quot;:856,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31316,&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_!4Ii3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 424w, https://substackcdn.com/image/fetch/$s_!4Ii3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 848w, https://substackcdn.com/image/fetch/$s_!4Ii3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 1272w, https://substackcdn.com/image/fetch/$s_!4Ii3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb6e83e-3354-4543-9b1c-457d6039f3db_856x140.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_isnull:
     name    age  gender  income  married
0  False  False   False   False    False
1  False  False   False   False    False
2  False  False   False    True    False
3  False  False   False   False    False
4  False  False   False   False    False</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Cpid!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Cpid!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 424w, https://substackcdn.com/image/fetch/$s_!Cpid!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 848w, https://substackcdn.com/image/fetch/$s_!Cpid!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 1272w, https://substackcdn.com/image/fetch/$s_!Cpid!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Cpid!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png" width="968" height="138" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:138,&quot;width&quot;:968,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:39264,&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_!Cpid!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 424w, https://substackcdn.com/image/fetch/$s_!Cpid!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 848w, https://substackcdn.com/image/fetch/$s_!Cpid!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 1272w, https://substackcdn.com/image/fetch/$s_!Cpid!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F98b780a5-3b91-49ad-8f24-5abff2013458_968x138.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code>df_drop_duplicates:
        name  age  gender   income  married
0      Sara   25  female  50000.0    False
1  Mohammed   30    male  60000.0     True
2       Ali   35    male      NaN     True
3    Khaled   40    male  80000.0    False
4     Asmaa   45  female  90000.0     True</code></pre><div><hr></div><p>While there are many advanced functions available in Pandas, it's important to master the basics in order to build a strong foundation in data science.</p>]]></content:encoded></item></channel></rss>