<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Siddhant on Medium]]></title>
        <description><![CDATA[Stories by Siddhant on Medium]]></description>
        <link>https://medium.com/@SSiddhant?source=rss-bf30ac99274d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oJs2eT70Gm2bMRhVkP7AIw@2x.jpeg</url>
            <title>Stories by Siddhant on Medium</title>
            <link>https://medium.com/@SSiddhant?source=rss-bf30ac99274d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 15:25:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@SSiddhant/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Simple Introduction to XOR Linked Lists]]></title>
            <link>https://medium.com/@SSiddhant/a-simple-introduction-to-xor-linked-lists-da038ecc897e?source=rss-bf30ac99274d------2</link>
            <guid isPermaLink="false">https://medium.com/p/da038ecc897e</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[xor]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Siddhant]]></dc:creator>
            <pubDate>Fri, 30 Aug 2024 02:49:24 GMT</pubDate>
            <atom:updated>2025-02-19T02:29:41.110Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XvQLEs4WrdftBCYA" /><figcaption>(Photo by <a href="https://unsplash.com/@hgudka97?utm_source=medium&amp;utm_medium=referral">Harshil Gudka</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>)</figcaption></figure><p>XOR Linked Lists are data structures which provide the functionality of doubly linked lists but use less memory. This is achieved through utilising a clever trick with the <em>bitwise XOR </em>operation to move both forward and backwards without having two pointers.</p><p>A doubly linked list allows for both forward <em>and </em>backward traversal. This is typically achieved through pointers. Below is a standard implementation of a doubly linked list node (in Python 3).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddf3b12076b2626d1b1388de82242232/href">https://medium.com/media/ddf3b12076b2626d1b1388de82242232/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dd_fyZL0WjnPvPKH5zh_Qw.png" /><figcaption>An example doubly linked list (showing how to insert a node at the front of a doubly linked list)</figcaption></figure><p>Assuming a 64 bit system, Python 3 uses 8 bytes for a pointer reference, so 16 bits in total will be used for the prev and next pointers. For a doubly linked list that needs to store 10,000 nodes, <strong>160kb</strong> of space would be needed (this is ignoring the space needed to store data, which could change depending on what the list is used for).</p><h3><strong>The XOR Operator</strong></h3><p>The XOR operator represents the <em>inequality function</em>, where the output is true if the inputs are not alike, and false otherwise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*ww2cbc4PfeHKUGIp7z_P-A.png" /><figcaption>XOR Truth Table</figcaption></figure><p>XOR also has the following properties (which will be useful for the implementation):</p><ol><li>A ⊕ A = 0</li><li>A ⊕ 0 = A</li><li>A ⊕ B = B ⊕ A (XOR is commutative)</li><li>(A ⊕ B) ⊕ C = A ⊕ (B ⊕ C) (XOR is Transitive)</li></ol><h3><strong>XOR Linked Lists</strong></h3><p>A XOR linked list node is of the form</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b0f59212ec1189d2468604c3a9d4275/href">https://medium.com/media/0b0f59212ec1189d2468604c3a9d4275/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w8R71P-lCK2Q9u5WvTSXpQ.png" /><figcaption>An example XOR Linked List</figcaption></figure><p><strong>The XOR Trick</strong></p><p>For the linked list above, let’s assume that we are at node C and we want to move to the node B. Using the following fact:</p><blockquote>(B ⊕ D) ⊕ D = B (as D ⊕ D = 0)</blockquote><p>We can perform the ⊕ operation on C’s <em>ptrdiff</em> with D, which would give B.</p><p>Similarly, if we want to move to D, then we can apply ⊕ to C’s <em>ptrdiff</em> with B.</p><blockquote>(B ⊕ D) ⊕ B = D (as B⊕ B = 0)</blockquote><p>If this concept seems confusing, a helpful approach is to draw the XOR linked list by hand and do the movements numerically, using hexadecimal representations for the pointers (e.g node A has a value of 0x100, node B has a value of 0x200, node C has a value of 0x300, etc.)</p><h3><strong>Storage Requirements</strong></h3><p>We only need one pointer per node for this implementation, hence assuming a 64 bit system, for a XOR linked list that needs to store 10,000 nodes, only <strong>80kb</strong> of space would be needed (half the size of the 160kb needed before).</p><h3><strong>Disadvantages</strong></h3><p>Certain disadvantages (other than code complexity) are reasons why XOR linked lists are not universally preferred over traditional doubly linked lists, these are:</p><ul><li><strong>Garbage Collection Issues</strong>: In traditional languages like Python 3 or Java, which use a reference counting/tracing style garbage collection method, it is hard for the collector to work with data structures that do not contain literal pointers. If nodes were to be deleted, there would most likely be memory leaks</li><li><strong>Lack of Debugging: </strong>Common debugging tools (i.e gdb or LLDB) do not work with XOR linked lists, and only work with doubly linked lists.</li><li><strong>Lack of Functionality when Deleting Nodes: </strong>It’s not possible to delete a node from the list knowing only the node’s address (which is possible in doubly linked lists)</li></ul><h3><strong>Closing Thoughts</strong></h3><p>XOR linked lists are useful alternatives to doubly linked lists when a space-efficient solution is needed. XOR linked lists can traverse a list going forwards and backwards while only using one pointer per node.</p><p>However, it is important to consider the incompatibility present between XOR linked lists and garbage collectors, which make this data structure highly volatile for languages that have automatic garbage collection.</p><p><strong>References</strong>:</p><p>Data Structures &amp; Algorithmic Thinking with Python by Narasimha Karumanchi</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da038ecc897e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Mathematics of Neural Networks — A complete example]]></title>
            <link>https://medium.com/@SSiddhant/the-mathematics-of-neural-networks-a-complete-example-65f2b12cdea2?source=rss-bf30ac99274d------2</link>
            <guid isPermaLink="false">https://medium.com/p/65f2b12cdea2</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[math]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Siddhant]]></dc:creator>
            <pubDate>Sat, 22 Jun 2024 07:36:45 GMT</pubDate>
            <atom:updated>2024-06-22T07:36:45.027Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Mathematics of Neural Networks — A complete example</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*C6eOzWVuG8-tC4tW" /><figcaption>Photo by <a href="https://unsplash.com/@sutirtab?utm_source=medium&amp;utm_medium=referral">sutirta budiman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Neural Networks are a method of artificial intelligence in which computers are taught to process data in a way similar to the human brain. Neural networks learn through being fed multiple instances of data as input, predicting an output, finding the error from the actual answer to the machine’s answer, and then fine-tuning it’s weights to reduce this error.</p><p>Whilst a neural network might seem very complex, it is actually a clever utilisation of linear algebra and multivariate calculus. This article aims to go through a full iteration of the maths that undermines a neural network.</p><h3>Assumptions &amp; Pre-Knowledge</h3><p>Neural networks require a solid understanding of a college-level of <a href="https://www.khanacademy.org/math/multivariable-calculus">calculus</a> and <a href="https://www.khanacademy.org/math/linear-algebra">linear algebra</a>. Great refreshers can be found on Khan Academy website (linked in the previous sentence). An algorithm that is imperative for this example is <strong>gradient descent</strong>, <a href="https://www.youtube.com/watch?v=sDv4f4s2SB8&amp;ab_channel=StatQuestwithJoshStarmer">which is explained well in this video.</a></p><p>For a course that is more relevant on neural networks, <a href="https://www.youtube.com/watch?v=Ixl3nykKG9M&amp;t=199s&amp;ab_channel=AdamDhalla">this video by Adam Dhalla</a> teaches you only the necessary areas of calculus and linear algebra that are needed for this example.</p><h3>Neural Network Basics</h3><p>The example we will be using is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CAqB1h-zZm3ujuNjCFiiag.png" /></figure><p>Typically, the <strong>input layer</strong> (green) are the <strong>input variables</strong> from a dataset, and the <strong>output layer</strong> (red) is the neural network’s<strong> prediction value</strong>. Within the hidden and output layers, a <strong>weighted sum </strong>(denoted by <em>s</em>) is taken for the each node, followed by the application of an <strong>activation function </strong>(denoted by <em>a</em>) which normalises the value according to a desired <a href="https://en.wikipedia.org/wiki/Activation_function">activation function</a>.</p><p>The process of feeding data through a network from input to output is called <strong>forward propagation. </strong>The process of observing the error rate of the forward propagation and feeding the error backward to fine-tune the weights of the neural network is called <strong>back propagation</strong>. We forward propagate before we back propagate.</p><h3>Forward Propagation</h3><p>Note: I am using the <strong>sigmoid</strong> function as the activation function for this example (activation functions are used as mappings for inputs to be within certain ranges — in the case of sigmoid, the range is (0, 1)).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yHPMOt_eFOXAMeHUn5dZCg.png" /></figure><h3><strong>Hidden Layer</strong></h3><p>Hidden Layer 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TVvv4bp1fjQq3tdavzqVTg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n2N2PHuOxVnMendz8NZwKQ.png" /></figure><p>Hidden Layer 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vLTNtiui6e4A0WTXv3-hZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBBPzeS4JxVd55L5GtTBCQ.png" /></figure><p>Hidden Layer 3:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFgejZ4XTBkCJrMVmAJnXA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eXLhnUUqHdg53s1oGh3MmA.png" /></figure><h3>Output Layer</h3><p>Output Layer 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XgNyKpsbvq6K966Jpb73RA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bkW-H5Is8jTbKwab9kLuyA.png" /></figure><p>Output Layer 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DXVi55F1gQyP6AWGU9mX6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pdAMfdsZyyHSFdrh_J0TNQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zakTvZhHBUoidD7tEfCY2Q.png" /></figure><h3>Mean Squared Error (MSE) Calculation</h3><p>The mean squared error is a measure of the difference between the expected and actual outputs. We are looking for a low MSE score, which indicates a better fit of the model to the data. We will be using <strong>gradient descent </strong>as a way to decrease this value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6yTL90jgzoPZAx_iiThCyA.png" /></figure><h3>Back Propagation</h3><p>Now that the predicted value is calculated, the neural network needs to adjust its weights based on the prediction error. This is done through back propagation.</p><p>For this example, consider a learning rate of 0.1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3LRFE_vbRW0S5GluUmHW9A.png" /></figure><p>The general mathematical idea behind back-propagation is to apply the chain rule to find the change in the error function over the change in a weight. Consider weight 7 for this example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QgEDy_WR36ajuol0RyX_4w.png" /></figure><p>All three partial equations can be derived from our work.</p><p>Firstly,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bHSDnrbPpfBr4QrgkqN2HQ.png" /></figure><p>Secondly,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tx4PeDlFA_lhyu8NkCdwrA.png" /></figure><p>And lastly,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPU57RA78WJAo-LqX0H65w.png" /></figure><p>Hence, putting all three terms together,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dC-XRh17kTT8PlYwky9EAQ.png" /></figure><p>This formula can be done for all weights connecting the hidden layer to the output layer.</p><p><em>note: often authors may write the equation using a delta: δ₀₁= (a₀₁−expected₁) × a₀₁ × (1−a₀₁), so the equation can be written as ∂E₀₁ / ∂w₇ = δ₀₁ × aₕ₁</em></p><p>Now we have the gradient of the error function.</p><p>We want to apply gradient descent to get a new value of weight w₇. The new w₇ (we can symbolise this w₇’) can be obtained by subtracting the learning rate multiplied by the gradient from w₇.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*81OHeDisAJE7NcXemSuCrg.png" /></figure><p>So in general, for an output neuron:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JklQcGyl8hVJx4BZxNfnvg.png" /></figure><h3>Output Layer</h3><p>Now, applying real numbers from the example to find new values of w₇ through w₁₂</p><p>Output Layer 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9I6TSPX_-nSRsx5btyEjNg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-VLSAIv1VoayXSUsw02png.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xIlk2hMS5lny3t-tqm-ztQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VQy24lxuCRUFvOqaZytfNg.png" /></figure><p>Output Layer 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N-2BswG_5qUCXEBe6_fQew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H__VDUISgLWhzYlddloDWg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_zFOPaZNMhM9Go4sYbGlsA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yF-FG_n4d0VPFVt90eEpZQ.png" /></figure><h3>The Hidden Layer (Derivation)</h3><p>Finding a way to optimise the weights for the hidden layer has a much much larger derivation — none of this section is relevant to the calculations, so feel free to skip this part if need be.</p><p>Consider updating the weights for w₁ — in principle, updating any weight will have the same style of formula in terms of revolving around partial differentiations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MmPow-gPI3AYpp8v3TicZA.png" /></figure><p>However this time around, we are a lot further away from the output neurons — hence, to find the values of the individual components of the RHS of this equation, there is going to be a lot more “chaining”…</p><p>For the first derivative:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UG7nbOafOeF8mkL5eVlXfQ.png" /></figure><p>Where:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sYKcyaJ1_rcJycHnoICAyQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ydAi8Ynh26G1FHf8_UTh2Q.png" /></figure><p>Now, since we have calcuated δ₀₁ and δ₀₂ previously (see the calculations made in the output layer section of this article), we can substitute in the values of these deltas into the equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zulj8q38t10RL_I4hF9r7A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pv4QmLWinBGWXqMi8CvGug.png" /></figure><p>Hence, the derivative of the weighted sum with respect to the previous layer’s neurons is essentially just the corresponding weight.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CGcQOMhlGO2p53_ZeJ7A6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EvJtZSRIP9Lzy_0Y4ZSmRg.png" /></figure><p>Now, substituting these values in for the partial error term:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pkf2FKEP1hl4c6GUTa_OJA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xljJzVPH1i-IphLQF-peNg.png" /></figure><p>The value of ∂aₕ₁ / ∂sₕ₁ would just be the derivative of the sigmoid function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KHuW8gkn_LgVtI5lobwsyA.png" /></figure><p>And the value of ∂sₕ₁ / ∂w₁ is the output of the previous layer neuron (which in this case, is the input layer neuron since there is only one hidden layer)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w5OksFQRk_mlm6g9FPg4Rw.png" /></figure><p>So putting it all together:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hmWwZNYLjvI3yeqZLHSjwA.png" /></figure><p>I hope you can see what has occurred in these steps — a similar working process can be done to find the formula of all of the weights (which I won’t show).</p><p>But in essence, to find the value of an updated weight, first calculate delta of the weight’s output neuron, and then subtract the old weight from the delta, multiplied by the delta, multiplied by the previous value of the weight’s input neuron.</p><p>If that is difficult to understand, then the calculations below may help you see what is occurring numerically.</p><h3>The Hidden Layer (Calculation)</h3><p>Previous delta values calculated:<br>δ₀₁ = -0.0984<br>δ₀₂ = 0.1479</p><p>Hidden layer 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*404nY92ibTdCk0gNs_npQQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7In0NSoR57bgXrgnr0jCKg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kdX-QqQGeZ66llxpyRMKLg.png" /></figure><p>Hidden layer 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9S8sP3Qpc3e0eFzjwxZyaA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hAdKKLBrtIJdInnI6p12dg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*88cf_2AgGeShZu7oE13Mxg.png" /></figure><p>Hidden layer 3:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_PD9C-aStjc6eIgmLQYCVA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZJniTOEZE9hMqNc6-jQsFg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*avnCa1D3AvQb115uswV0UQ.png" /></figure><p>And we’re done!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bdW3Q7xGJyURB5h0NFxA6Q.png" /><figcaption>Neural network with updated weights</figcaption></figure><h3>Closing Thoughts</h3><p>The following was a complete example of a forward and back propagation for a neural network with 3 layers.</p><p>Typically neural networks are trained on multiple instances of data and can also be trained for multiple iterations (we call these <strong>epochs)</strong>. Doing this will gradually increase/decrease the weights depending on the instance, until a neural network is optimised for a set of instances.</p><p>This process was <em>very </em>laborious and math heavy — thankfully that is why we have computers simulate all of this work. Libraries like <a href="https://pytorch.org/tutorials/beginner/basics/buildmodel_tutorial.html">PyTorch</a> abstract many of the mathematical complexities and should definitely be used for any sort of model training.</p><p>Nonetheless, a complete walkthrough of the maths would definitely help reinforce the understanding needed when implementing this model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65f2b12cdea2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deciphering Stances: Analysing COVID-19 Vaccination Tweets with a Zero-Shot Classification Model]]></title>
            <link>https://faun.pub/deciphering-stances-analysing-covid-19-vaccination-tweets-with-a-zero-shot-classification-model-97de3921be06?source=rss-bf30ac99274d------2</link>
            <guid isPermaLink="false">https://medium.com/p/97de3921be06</guid>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[classification]]></category>
            <dc:creator><![CDATA[Siddhant]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 20:39:53 GMT</pubDate>
            <atom:updated>2024-02-12T20:39:53.004Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/612/0*iAX5qgQERKZw9JIO" /><figcaption>Source: Unsplash</figcaption></figure><p>In 2021, the global effort to vaccinate individuals against COVID-19 elicited polarising responses, with individuals expressing divided opinions on various facets of the vaccine process spanning from its inception to its distribution, . These divergent views typically manifested on social media platforms like Twitter. The aim of this article is to find out the stance of these users as to whether they were for or against vaccination.</p><h3>Zero-Shot Classification</h3><p><strong>Zero-Shot Classification </strong>is a part of natural language processing where a model is initially trained on a set of labeled examples, but is then able to classify new examples from previously unseen labels. In zero-shot classification, a model is provided with a prompt and a sequence of text that describes what we want our model to do.</p><p>We can use the zero-shot classification technique as a form of <strong>stance detection</strong> to determine whether an individual is supportive or opposing vaccination for COVID-19.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GPCSbMjoTAAuw_q5DBfh8Q.png" /><figcaption>Source: From the author — a simplified diagram showing a high-level overview of zero-shot classification</figcaption></figure><p>The model used is this example is the <em>pre-trained </em><a href="https://huggingface.co/MoritzLaurer/deberta-v3-large-zeroshot-v1.1-all-33"><em>DeBERTa-v3-large-zeroshot model</em></a> by Moritz Laurer. DeBERTa stands for Decoding-enhanced BERT with disentangled Attention and is a refinement of the <a href="https://huggingface.co/docs/transformers/model_doc/bert">BERT model</a>. The pre-trained zero-shot model has been trained in such a way that “any classification task can be reformulated” into the model.</p><p>A great practical example of this can be seen on the <a href="https://huggingface.co/MoritzLaurer/deberta-v3-large-zeroshot-v1.1-all-33">Hugging Face site</a> which displays the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*Y1y117PZXv6hY-gs7dSebg.png" /><figcaption>Input for the model (From the Hugging Face website)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*8nJUOEl2ed32SRBW9qtvwQ.png" /><figcaption>Output from the model (From the Hugging Face website)</figcaption></figure><p>The dataset used is the <a href="https://www.kaggle.com/datasets/gpreda/all-covid19-vaccines-tweets"><em>COVID-19 Vaccines Tweets Dataset</em></a>, which has 228k tweets regarding vaccinations posted between December 2020 and December 2021<em>.</em> The dataset contains useful parameters such as account information, date, time, and tweet content. Through this, we can find insights and trends by examining the total tweet count across various time periods. This allows an understanding of the fluctuations in public sentiment over time, providing a nuanced understanding of shifts in people’s overall stance towards COVID-19 vaccination.</p><h3>Implementation</h3><p>A Jupyter notebook was used for the coding. Pytorch needs to be installed before running.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd5ece0158f19861d6ff24bae117d274/href">https://medium.com/media/cd5ece0158f19861d6ff24bae117d274/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*hzfD59GwfnaLfCcXgoaLtQ.png" /><figcaption>Output for the above code</figcaption></figure><p>Many of these columns are not needed. In this example, only the user’s name, location, description, date, tweet, and is_retweet features will be kept</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60b52f0797b916d57730bfc4ba161f15/href">https://medium.com/media/60b52f0797b916d57730bfc4ba161f15/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DL0EbFJl5jvtmqFctKzzmA.png" /><figcaption>Output for the above code</figcaption></figure><p>Now the data needs to be filtered. In this example we are going to:</p><ul><li>remove retweets</li><li>remove media/news tweets by removing any tweets from the dataframe which contain “media” or “news” in the user_name or user_description</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6402bdeb7db52a90fb97b3c51990bdc1/href">https://medium.com/media/6402bdeb7db52a90fb97b3c51990bdc1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0jUqVKvvHPCiiAMO6qsZUA.png" /><figcaption>Output for the above code</figcaption></figure><p>We can now load the classifier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0096fdaf8a5243e520d4e3336c868c87/href">https://medium.com/media/0096fdaf8a5243e520d4e3336c868c87/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ENrerkdv8PfZdBVJRAglbQ.png" /><figcaption>Output for the above code</figcaption></figure><p>The classifier always prints the class with the highest value first. To keep consistency, we can use a function to output the scores in the form (pro-vaccine, anti-vaccine).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b749327459167546a991d497fe74ad3/href">https://medium.com/media/4b749327459167546a991d497fe74ad3/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*uWt3g9QJtc222YRXl3miFw.png" /><figcaption>Output for the above code</figcaption></figure><p>Unfortunately I don’t have the processing power to classify all 187k tweets in the dataset. For time-saving purposes, I randomly sampled 10k tweets to perform classification on. The values are stored in the df as ‘pro-vaccine’ and ‘anti-vaccine’.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc7fd7cda1109bfcc6b41bad0d08f8b5/href">https://medium.com/media/dc7fd7cda1109bfcc6b41bad0d08f8b5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*LMBbjh_GXelqM5n3hhBevQ.png" /></figure><p>Now our df should be looking something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f4Tk_fl0OHJ1392oRdCgBw.png" /><figcaption>Output for the above code</figcaption></figure><h3>Analysis</h3><p>Now that we have our stances, we can plot a histogram to see the general distribution of both the pro and anti-vaccine features</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c33347cbdf2033db2f2c2e6f758227dd/href">https://medium.com/media/c33347cbdf2033db2f2c2e6f758227dd/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zFWwnew3js9zbTsUQt5yaQ.png" /><figcaption>The distribution of anti (left) and pro (right) vaccine scores for 10,000 tweets using zero-shot classification</figcaption></figure><p>Referring to the second chart, immediately, we can see that the overwhelming majority of tweets are largely pro-vaccine, with the highest bin being over 0.95. There are a small number of tweets shown between the range of 0.6 and 0.85, and there seems to be a small peak at 0.5 for perfectly neutral tweets. There are virtually no tweets that lie in the range of 0.1 to 0.4 for but there are a small number of tweets which are very anti-vaccine (0–0.05).</p><p>For further analysis, it would be useful to classify these results into “Positive”, “Neutral”, or “Negative”. Through my judgement, I picked the following (for pro-vaccine):<br><strong>Positive: 0.6–1.0<br>Neutral: 0.4–0.6<br>Negative: 0–0.4</strong></p><p>In addition to this, grouping the tweets by month would be useful in gaining insights as to whether the sentiment changed over time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa4f77ddea5f5578e3162f527f6c0d0c/href">https://medium.com/media/aa4f77ddea5f5578e3162f527f6c0d0c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6HdsIVksg7Hh64eVBU5zFA.png" /><figcaption>Output for the above code</figcaption></figure><p>Now we can group the data by month and look at the total values</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45666150def942aeeacfc112f9262b90/href">https://medium.com/media/45666150def942aeeacfc112f9262b90/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/1*AcRMwIE8r-GpMp0AbDstfw.png" /></figure><p>We can also view it as a percentage</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d184e1be1a42312fe0f020b4e6a6a06a/href">https://medium.com/media/d184e1be1a42312fe0f020b4e6a6a06a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*O-0mfsoWZOlideW7bXQejw.png" /><figcaption>Percentage data outputted from the above code</figcaption></figure><p>And view this as a graph</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff22e364e39da9a5519868aaebc76cda/href">https://medium.com/media/ff22e364e39da9a5519868aaebc76cda/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ofYotxtuaKpLRnVX9wIZIA.png" /></figure><h3>Inferences</h3><p>We can see that according to the data, dicussions revolving vaccinations began occuring in March 2021 and continued rising till June 2021, after which interest steadily declined.</p><p>A trend can be observed that during the months of April — August, the number of conversations revolving vaccines on Twitter increased <a href="https://ourworldindata.org/covid-vaccinations">proportional to the number of civilians around the world who received their vaccination.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f3rm5ujFqa85l8ezJU4BHQ.png" /><figcaption>(Left) Bar graph of the number of positive, neutral, and negative vaccine tweets by month. (Right) The 7-day rolling average of daily COVID-19 Vaccine doses administered between March 1st 2021 and December 1st 2021</figcaption></figure><p>This trend is noticable but tends but stops after the month of August, as vaccinations are still at a reasonably high level yet the conversation revolving vaccines peter out.</p><p>Another interesting observation is that largely throughout all of the months, the number of negative tweets were always dwarved by the number of positive or neutral tweets, but remained largely constant — implying that anti-vaccine sentiment was everpresent throughout the majority of 2021 — albeit a very small amount.</p><p>Further insights are hard to find due to the sheer scale of the data. The dataset comprises of tweets from many different people from different countries which all had differing policies and strategies towards fighting COVID-19. As the classification is performed on tweets around the world — comparative data becomes much harder to contrast to the results obtained as much of what is publicly accessible pertains to one particular country, rather than the whole world.</p><h3>Improvements</h3><p>This analysis is by no means perfect. There are numerous improvements that can be made to improve the data received and to obtain a better understanding of public sentiment of this topic. These improvements can be:</p><ol><li>Focusing on tweets from a particular country to guage a more accurate sentiment which is more relevant to said country.</li><li>Using a different dataset that encompasses tweets about vaccines using different methods rather than just tracking hashtags.</li><li>Running the model on the entire dataset rather than just a subset of 10k tweets to get a better sample</li><li>Using tweets that explicitly stated their stance regarding vaccination. Many of the tweets in the dataset are very reactionary and do not provide any substantial comment or opinion — however, this is what would be expected from a low-effort publishing medium such as a tweet.</li><li>The definition of “Neutral” in this model was completely arbitrary and the values for which a tweet was netural was selected with no discernible empirical bias</li><li>Different insights could have been achieved through testing with different model labels rather than exclusively using “pro-vaccine” and “anti-vaccine”.</li></ol><h3>Closing Notes</h3><p>Using the tremendous power of zero-shot classification, we are able to measure the public sentiment of many complex topics and discussions. When applying this technique to the topic of the vaccine-debate, we can see that the number of online users who support vaccination greatly outnumber the rest. Whilst improvements can be made, the conclusions from this experiment can be informative to statisticians and serve as a greater way to understand public sentiment.</p><h3>References</h3><ul><li><a href="https://www.youtube.com/watch?v=QpzMWQvxXWk">Great video explaining sentiment analysis and implementing basic sentiment analysis</a></li><li><a href="https://www.google.com/search?q=zero-shot+classification&amp;rlz=1C1VDKB_enAU1086AU1086&amp;oq=zero-shot+classification&amp;gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIGCAEQRRg7MgYIAhBFGDsyBggDEEUYOzIGCAQQRRg9MgYIBRBFGD0yBggGEEUYPNIBCDI0MDdqMGo3qAIAsAIA&amp;sourceid=chrome&amp;ie=UTF-8">An explanation of zero-shot classification</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*A_4t6MKnqmug04qw.png" /></figure><p>👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇</p><p>🚀<a href="http://from.faun.to/r/8zxxd">Join FAUN Developer Community &amp; Get Similar Stories in your Inbox Each Week</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97de3921be06" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/deciphering-stances-analysing-covid-19-vaccination-tweets-with-a-zero-shot-classification-model-97de3921be06">Deciphering Stances: Analysing COVID-19 Vaccination Tweets with a Zero-Shot Classification Model</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing Black-Scholes Model for Option Chains using Python]]></title>
            <link>https://medium.com/@SSiddhant/implementing-black-scholes-model-for-option-chains-using-python-6978662bfbb6?source=rss-bf30ac99274d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6978662bfbb6</guid>
            <category><![CDATA[black-scholes-model]]></category>
            <category><![CDATA[trading]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[math]]></category>
            <category><![CDATA[options]]></category>
            <dc:creator><![CDATA[Siddhant]]></dc:creator>
            <pubDate>Fri, 19 Jan 2024 10:25:22 GMT</pubDate>
            <atom:updated>2024-01-19T10:25:22.590Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Black-Scholes Model in Python</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*SL_E3sM9Fn2kTKASyHpGGw.jpeg" /><figcaption>Random walks: the foundation of the Black-Scholes Model</figcaption></figure><p>An option is a financial derivative that provides the holder the right, but not the obligation, to buy or sell a specified underlying asset at a predetermined price (strike price) at a specified future period.</p><p>A <strong>call option</strong> gives the holder the right to <em>buy</em> an asset by a certain date for a certain price. A <strong>put option</strong> gives the holder the right to <em>sell</em> an asset by a certain date for a certain price. There are two sides to every options exchange, the buyer (holder) and the seller (writer).</p><p>The two major styles of options in the market are <strong>European options</strong> and <strong>American options</strong>. A European option can be exercised only on the maturity date, whereas an American option can be exercised at any time during it’s life.</p><h3>Black-Scholes Model</h3><p>The Black-Scholes model (BSM) is a widespread option-pricing model which has been pivotal to the growth and success of financial engineering. The model estimates the theoretical value of European call and put options based on other financial instruments. There are 5 parameters for the BSM: the strike price of an option, the current stock price, the time to expiration, the risk-free rate, and the volatility.</p><p><strong>Assumptions</strong></p><ul><li>The options being traded are European</li><li>The stock price behaviour corresponds to the <a href="https://en.wikipedia.org/wiki/Log-normal_distribution">log-normal distribution</a> with a constant μ and σ</li><li>There are no transaction costs or taxes. All securities are perfectly divisible</li><li>There are no dividends on the stock during the life of the option (see closing notes)*</li><li>There are no riskless arbitrage opportunities</li><li>Investors can borrow or lend at the same risk-free rate of interest</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*I8hWnvzSV6XreExdamTzNA.jpeg" /><figcaption>The equation for the Black-Scholes model</figcaption></figure><p><strong>c: </strong><em>call price of option</em><br><strong>p: </strong><em>put price of option<br></em><strong>N:<em> </em></strong><em>CDF (cumulative distribution function) of the normal distribution<br></em><strong>S₀: </strong><em>Initial stock price of asset at time of trade</em><br><strong>K: </strong><em>Strike price</em><br><strong>r: </strong><em>risk-free interest rate</em><br><strong>t: </strong><em>time to maturity</em><br><strong>σ: </strong><em>Volatility of the asset</em></p><h3>Implementation</h3><p>This version of the BSM model only works for non-dividend-paying assets (here I have chosen AAPL stock). Ensure you use an asset that does not pay dividends for these calculations.</p><p>The code provided is from a Jupyter Notebook and is an example of applying the Black-Scholes’ model to a euro call. The necessary functions are provided for put options in the program.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7149440336a7bf6a7029047ee290b4db/href">https://medium.com/media/7149440336a7bf6a7029047ee290b4db/href</a></iframe><p>Using the YFinance API, we can find the option dates for a specific ticker</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a02f731da5109db1705933829d68f10/href">https://medium.com/media/6a02f731da5109db1705933829d68f10/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/141/1*djqL3DCXPqHR7VNFBEToIA.png" /><figcaption>Output for above code</figcaption></figure><p>The above code generates all possible expiry dates for options of the underlying asset. Inputting one of the dates above into the date variable (I chose 2024–06–21) and running the code below will fetch the call and put option chains for the chosen asset.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a844c6549ec526d8b4d1d75d0a7cae6/href">https://medium.com/media/9a844c6549ec526d8b4d1d75d0a7cae6/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0lTzwqFInKGB2L8--mRlyA.png" /><figcaption>Output dataframe</figcaption></figure><p>For the Black Scholes formula, we need 5 inputs: S, K, t, r, and σ. From the above dataframe we can only extract the strike values (K) — so we need to get the four other data input values.</p><p>For the r value, I chose to use the 10 year US treasury rate.</p><p>The volatility calculated in this example is the <strong>annualized volatility</strong>, which is determined by calculating the average returns of the stock price and multiplying it by the square root of 252 (the number of trading days in a year).</p><p>An alternative to this is <strong>implied volatility</strong>, which relies on the current value of the call/put option to estimate volatility. This method employs five inputs — call/put price, stock price, strike price, time to maturity, and risk-free rate — and outputs the value for σ. Implied volatility is valuable due to its ability to capture the real-time market expectations of the volatility. We can obtain the implied volatility from the above database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e489ec96317f05956b328cf4edb50315/href">https://medium.com/media/e489ec96317f05956b328cf4edb50315/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*ocMGs2J2Rekmy1e64hpvAA.png" /><figcaption>Output for the above code</figcaption></figure><p>Now, as we have all of our variables, we can calculate the BSM valuation for each option and add the valuation as a feature to our dataframe</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea218a3b0aa74d4b5db353a248907274/href">https://medium.com/media/ea218a3b0aa74d4b5db353a248907274/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yFWfCPO1HtMCuYMo425d9w.png" /><figcaption>Resulting dataframe</figcaption></figure><h3>Measuring Sensitivity with the Greeks</h3><p>The greeks are a set of symbols that assist in measuring different dimensions to the risk in an option position. The aim of a trader is to manage the greeks so that all the risks are acceptable.</p><p><strong>Delta(Δ)</strong>: <em>The change in option’s premium for every $1 change in the underlying stock’s price.</em></p><p><strong>Gamma(</strong>Γ<strong>)</strong>: <em>The change in the option’s delta (</em>Δ<em>) for every $1 change in the underlying stock price.</em></p><p><strong>Theta(Θ)</strong>: <em>The change in the option’s premium for every day that passes, representing the time decay. Theta reflects the rate at which the option’s premium decreases over time.</em></p><p><strong>Vega(ν)</strong>: <em>Change in the option’s premium for every 1% change in implied volatility. Vega measures the sensitivity of the option’s price to changes in the implied vol.</em></p><p><strong>Rho(ρ)</strong>: <em>Change in the option’s premium for every 1% change in the risk-free rate. Rho measures the sensitivity of the option’s price to changes in the interest rate.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*-88FQkd8jzqjV4AXH9XHEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*Wp3zMk2ka28Q5U31UK4doQ.png" /></figure><h3><strong>Implementation</strong></h3><p>The below code adds the columns of delta, gamma, vega, theta, and rho to the dataframe.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e79746de95f14cec77a58f03e08faf19/href">https://medium.com/media/e79746de95f14cec77a58f03e08faf19/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/748e0c1c1bf2e1ea784a13ab47a54ed0/href">https://medium.com/media/748e0c1c1bf2e1ea784a13ab47a54ed0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dEZbCnqgtyb7xbmY3HSSEA.png" /></figure><h3>Closing Notes</h3><p>I mentioned earlier that this model is applicable exclusively to non-dividend paying European calls and puts. Nonetheless, equations tailored to dividends exist. These equations operate by discounting the dividend value to the present day.</p><p>The Black Scholes model is specifically designed for European options. In the case of American options, alternative models like the <a href="https://www.investopedia.com/terms/b/binomialoptionpricing.asp">binomial option model</a> become relevant. Similarly, for exotic options, techniques like <a href="https://www.investopedia.com/articles/investing/112514/monte-carlo-simulation-basics.asp#:~:text=Monte%20Carlo%20is%20used%20for,to%20get%20the%20option%20price.">Monte Carlo simulation</a> can be effective.</p><p><strong>References</strong>:</p><p>Fundamentals of Futures and Options Markets (Ninth Edition) by John Hull</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6978662bfbb6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>