{"id":3181,"date":"2025-04-18T07:02:22","date_gmt":"2025-04-18T07:02:22","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/04\/18\/when-physics-meets-finance-using-ai-to-solve-black-scholes\/"},"modified":"2025-04-18T07:02:22","modified_gmt":"2025-04-18T07:02:22","slug":"when-physics-meets-finance-using-ai-to-solve-black-scholes","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/04\/18\/when-physics-meets-finance-using-ai-to-solve-black-scholes\/","title":{"rendered":"When Physics Meets Finance: Using AI to Solve Black-Scholes"},"content":{"rendered":"<p>    When Physics Meets Finance: Using AI to Solve Black-Scholes<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong><em>DISCLAIMER<\/em><\/strong>: This is not financial advice. I\u2019m a PhD in Aerospace Engineering with a strong focus on Machine Learning: I\u2019m <strong>not<\/strong> a financial advisor. This article is intended solely to demonstrate the power of Physics-Informed Neural Networks (PINNs) in a financial context.<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\"><mdspan datatext=\"el1744948982397\" class=\"mdspan-comment\">When I was 16<\/mdspan>, I fell in love with Physics. The reason was simple yet powerful: I thought Physics was <strong><em>fair<\/em><\/strong>.<\/p>\n<p class=\"wp-block-paragraph\" id=\"e^x\">It never happened that I got an exercise wrong because the speed of light changed overnight, or because suddenly e<sup>x<\/sup> could be negative. Every time I read a physics paper and thought, <em>\u201cThis doesn\u2019t make sense,<\/em>\u201d it turned out <strong><em>I was the one not making sense.<\/em><\/strong><\/p>\n<p class=\"wp-block-paragraph\">So, Physics is always fair, and because of that, it\u2019s always <strong><em>perfect<\/em><\/strong>. And Physics displays this perfection and fairness through its set of rules, which are known as <strong>differential equations<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">The simplest differential equation I know is this one:<\/p>\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" height=\"559\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/Differential_Equation-1-1024x559.png?resize=1024%2C559&#038;ssl=1\" alt=\"\" class=\"wp-image-601628\" style=\"width:409px;height:auto\"><figcaption class=\"wp-element-caption\">Image made by author<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Very simple: we start here, x<sub>0<\/sub>=0, at time t=0, then we move with a constant speed of 5 m\/s. This means that after 1 second, we are 5 meters (or miles, if you like it best) away from the origin; after 2 seconds, we are 10 meters away from the origin; after 43128 seconds\u2026 I think you got it.<\/p>\n<p class=\"wp-block-paragraph\">As we were saying, this is written in stone: perfect, ideal, and unquestionable. Nonetheless, imagine this in real life. Imagine you are out for a walk or driving. Even if you try your best to go at a target speed, you will never be able to keep it constant. Your mind will race in certain parts; maybe you will get distracted, maybe you will stop for red lights, most likely a combination of the above. So maybe the simple differential equation we mentioned earlier is not enough. What we could do is to try and predict your location from the differential equation, <strong><em>but<\/em> with the help of <a href=\"https:\/\/towardsdatascience.com\/tag\/artificial-intelligence\/\" title=\"Artificial Intelligence\">Artificial Intelligence<\/a><\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">This idea is implemented in <a href=\"https:\/\/www.sciencedirect.com\/science\/article\/pii\/S0021999118307125\">Physics Informed Neural Networks<\/a> (PINN). We will describe them later in detail, but the idea is that we try to match <em>both<\/em> the data and what we know from the differential equation that describes the phenomenon. This means that we enforce our solution to generally meet what we expect from Physics. I know it sounds like black magic, I promise it will be clearer throughout the post.<\/p>\n<p class=\"wp-block-paragraph\">Now, the big question:<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">What does Finance have to do with Physics and Physics Informed Neural Networks?<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\">Well, it turns out that differential equations are not only useful for nerds like me who are interested in the laws of the natural universe, but they can be useful in <strong>financial models<\/strong> as well. For example, the <strong>Black-Scholes <\/strong>model uses a differential equation to set the price of a call option to have, given certain quite strict assumptions, a <strong>risk-free portfolio<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">The goal of this very convoluted introduction was twofold:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Confuse you just a little, so that you will keep reading <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f642.png?ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\">\n<\/li>\n<li class=\"wp-block-list-item\">Spark your curiosity just enough to see where this is all going.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Hopefully I managed <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f601.png?ssl=1\" alt=\"\ud83d\ude01\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\">. If I did, the rest of the article would follow these steps:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">We will discuss the <strong>Black-Scholes model<\/strong>, its assumptions, and its differential equation<\/li>\n<li class=\"wp-block-list-item\">We will talk about <strong>Physics Informed Neural Networks (PINNs),<\/strong> where they come from, and why they are helpful<\/li>\n<li class=\"wp-block-list-item\">We will develop our algorithm that trains a PINN on Black-Scholes using <strong>Python, Torch, <\/strong>and<strong> OOP.<\/strong>\n<\/li>\n<li class=\"wp-block-list-item\">We will show the results of our algorithm. <\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">I\u2019m excited! To the lab! <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f9ea.png?ssl=1\" alt=\"\ud83e\uddea\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><\/p>\n<h2 class=\"wp-block-heading\">1. Black Scholes Model<\/h2>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">If you are curious about the original paper of Black-Scholes, you can find it <a href=\"https:\/\/www.cs.princeton.edu\/courses\/archive\/fall09\/cos323\/papers\/black_scholes73.pdf\">here<\/a>. It\u2019s definitely worth it <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f642.png?ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\">Ok, so now we have to understand the <a href=\"https:\/\/towardsdatascience.com\/tag\/finance\/\" title=\"Finance\">Finance<\/a> universe we are in, what the variables are, and what the laws are.<\/p>\n<p class=\"wp-block-paragraph\">First off, in Finance, there is a powerful tool called a call<strong> option<\/strong>. The call option gives you the right (not the obligation) to buy a stock at a certain price in the fixed future (let\u2019s say a year from now), which is called the strike<strong> price<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">Now let\u2019s think about it for a moment, shall we? Let\u2019s say that today the given stock price is $100. Let us also assume that we hold a call option with a $100 strike price. Now let\u2019s say that in one year the stock price goes to $150. That\u2019s amazing! We can use that call option to buy the stock and then immediately resell it! We just made $150 \u2013 $150-$100 = $50 profit. On the other hand, if in one year the stock price goes down to $80, then we can\u2019t do that. Actually, we are better off not exercising our right to buy at all, not to lose money.<\/p>\n<p class=\"wp-block-paragraph\">So now that we think about it, the idea of <strong>buying a stock<\/strong> and <strong>selling an option<\/strong> turns out to be <strong>perfectly complementary<\/strong>. What I mean is the randomness of the stock price (the fact that it goes up and down) can actually be <strong>mitigated<\/strong> by holding the right number of options. This is called <strong>delta hedging.<\/strong><\/p>\n<p class=\"wp-block-paragraph\">Based on a set of assumptions, we can derive the <strong>fair option price<\/strong> in order to have a <strong>risk-free <\/strong>portfolio. <\/p>\n<p class=\"wp-block-paragraph\">I don\u2019t want to bore you with all the details of the derivation (they are honestly not that hard to follow in the original paper), but the differential equation of the risk-free portfolio is this:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" height=\"129\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/Differential_Equation_BS-1024x129.png?resize=1024%2C129&#038;ssl=1\" alt=\"\" class=\"wp-image-601650\"><\/figure>\n<p class=\"wp-block-paragraph\">Where:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<code>C<\/code> is the price of the option at time t<\/li>\n<li class=\"wp-block-list-item\">\n<code>sigma<\/code> is the volatility of the stock<\/li>\n<li class=\"wp-block-list-item\">\n<code>r<\/code> is the risk-free rate<\/li>\n<li class=\"wp-block-list-item\">\n<code>t<\/code> is time (with t=0 now and T at expiration)<\/li>\n<li class=\"wp-block-list-item\">\n<code>S<\/code> is the current stock price<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">From this equation, we can derive the fair price of the call option to have a risk-free portfolio. The equation is closed and analytical, and it looks like this:<\/p>\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" height=\"100\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/Black_Scholes_Solution-1024x100.png?resize=1024%2C100&#038;ssl=1\" alt=\"\" class=\"wp-image-601654\" style=\"width:486px;height:auto\"><\/figure>\n<p class=\"wp-block-paragraph\">With:<\/p>\n<figure class=\"wp-block-image aligncenter size-large is-resized\" datatext=\"\"><img data-recalc-dims=\"1\" height=\"378\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/d1_and_d2-1024x378.png?resize=1024%2C378&#038;ssl=1\" alt=\"\" class=\"wp-image-601655\" style=\"width:377px;height:auto\"><\/figure>\n<p class=\"wp-block-paragraph\">Where N(x) is the cumulative distribution function (CDF) of the standard normal distribution, K is the strike price, and T is the expiration time.<\/p>\n<p class=\"wp-block-paragraph\">For example, this is the plot of the <strong>Stock Price (x) <\/strong>vs<strong> <strong>Call Option<\/strong> (y)<\/strong>, according to the Black-Scholes model.<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-49.png?ssl=1\" alt=\"\" class=\"wp-image-601656\"><figcaption class=\"wp-element-caption\">Image made by author<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Now this looks cool and all, but what does it have to do with Physics and PINN? It looks like the equation is analytical, so why PINN? Why AI? Why am I reading this at all? The answer is below <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f447.png?ssl=1\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\">:<\/p>\n<h2 class=\"wp-block-heading\">2. Physics Informed Neural Networks<\/h2>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">If you are curious about Physics Informed Neural Networks, you can find out in the original paper <a href=\"http:\/\/physics%20informed%20neural%20networks\/\">here<\/a>. Again, worth a read. <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f642.png?ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> <\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\">Now, the equation above is <strong>analytical<\/strong>, but again, that is an equation of a fair price in an ideal scenario. What happens if we ignore this for a moment and try to guess the price of the option given the stock price and the time? For example, we could use a Feed Forward Neural Network and train it through backpropagation. <\/p>\n<p class=\"wp-block-paragraph\">In this training mechanism, we are minimizing the error <\/p>\n<p class=\"wp-block-paragraph\"><code>L = |Estimated C - Real C|<\/code>:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" height=\"645\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-50-1024x645.png?resize=1024%2C645&#038;ssl=1\" alt=\"\" class=\"wp-image-601671\"><figcaption class=\"wp-element-caption\">Image made by author<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">This is fine, and it is the simplest Neural Network approach you could do. The issue here is that we are completely ignoring the Black-Scholes equation. So, is there another way? Can we possibly integrate it?<\/p>\n<p class=\"wp-block-paragraph\">Of course, we can, that is, if we set the error to be<\/p>\n<p class=\"wp-block-paragraph\"><code>L = |Estimated C - Real C|+ PDE(C,S,t)<\/code><\/p>\n<p class=\"wp-block-paragraph\">Where PDE(C,S,t) is <\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" height=\"129\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/Differential_Equation_BS-1024x129.png?resize=1024%2C129&#038;ssl=1\" alt=\"\" class=\"wp-image-601650\"><\/figure>\n<p class=\"wp-block-paragraph\">And it needs to be as close to 0 as possible:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" height=\"638\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-52-1024x638.png?resize=1024%2C638&#038;ssl=1\" alt=\"\" class=\"wp-image-601673\"><figcaption class=\"wp-element-caption\">Image made by author <\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">But the question still stands. Why is this \u201cbetter\u201d than the simple Black-Scholes? Why not just use the differential equation? Well, because sometimes, in life, solving the differential equation doesn\u2019t guarantee you the \u201creal\u201d solution. Physics is usually approximating things, and it is doing that in a way that could create a difference between what we expect and what we see. That is why the PINN is an amazing and fascinating tool: you try to match the physics, but you are strict in the fact that the results have to match what you \u201csee\u201d from your dataset. <\/p>\n<p class=\"wp-block-paragraph\">In our case, it might be that, in order to obtain a risk-free portfolio, we find that the theoretical Black-Scholes model doesn\u2019t fully match the noisy, biased, or imperfect market data we\u2019re observing. Maybe the volatility isn\u2019t constant. Maybe the market isn\u2019t efficient. Maybe the assumptions behind the equation just don\u2019t hold up. That is where an approach like PINN can be helpful. We not only find a solution that meets the Black-Scholes equation, but we also \u201ctrust\u201d what we see from the data.<\/p>\n<p class=\"wp-block-paragraph\">Ok, enough with the theory. Let\u2019s code. <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f468-200d-1f4bb.png?ssl=1\" alt=\"\ud83d\udc68\u200d\ud83d\udcbb\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><\/p>\n<h2 class=\"wp-block-heading\">3. Hands On Python Implementation<\/h2>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">The whole code, with a cool README.md, a fantastic notebook and a super clear modular code, can be found <a href=\"https:\/\/github.com\/PieroPaialungaAI\/BlackScholesPINN\">here<\/a><\/p>\n<p class=\"wp-block-paragraph\">P.S.  This will be a little intense (a lot of code), and if you are not into software, feel free to skip to the next chapter. I will show the results in a more friendly way <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f642.png?ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\">Thank you a lot for getting to this point <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/2764.png?ssl=1\" alt=\"\u2764\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><br \/>Let\u2019s see how we can implement this. <\/p>\n<h3 class=\"wp-block-heading\">3.1 Config.json file<\/h3>\n<p class=\"wp-block-paragraph\">The whole code can run with a very simple configuration file, which I called <strong>config.json<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">You can place it wherever you like, as we will see.<\/p>\n<div class=\"wp-block-tds-gist-embed\">\n\t<script src=\"https:\/\/gist.github.com\/PieroPaialungaAI\/53e4c4f4fc457c4d78a1a200d02da930.js\"><\/script>\n<\/div>\n<p class=\"wp-block-paragraph\">This file is crucial, as it defines all the parameters that govern our simulation, data generation, and model training. Let me quickly walk you through what each value represents:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<code>K<\/code>: the <strong>strike price<\/strong> \u2014 this is the price at which the option gives you the right to buy the stock in the future. <\/li>\n<li class=\"wp-block-list-item\">\n<code>T<\/code>: the <strong>time to maturity<\/strong>, in years. So <code>T = 1.0<\/code> means the option expires one unit (for example, one year) from now.<\/li>\n<li class=\"wp-block-list-item\">\n<code>r<\/code>: the <strong>risk-free interest rate is<\/strong> used to discount future values. This is the interest rate we are setting in our simulation.<\/li>\n<li class=\"wp-block-list-item\">\n<code>sigma<\/code>: the <strong>volatility<\/strong> of the stock, which quantifies how unpredictable or \u201crisky\u201d the stock price is. Again, a simulation parameter.<\/li>\n<li class=\"wp-block-list-item\">\n<code>N_data<\/code>: the number of <strong>synthetic data points<\/strong> we want to generate for training. This will condition the size of the model as well.<\/li>\n<li class=\"wp-block-list-item\">\n<code>min_S<\/code> and <code>max_S<\/code>: the <strong>range of stock prices<\/strong> we want to sample when generating synthetic data. Min and max in our stock price.<\/li>\n<li class=\"wp-block-list-item\">\n<code>bias<\/code>: an optional <strong>offset added to the option prices<\/strong>, to simulate a systemic shift in the data. This is done to create a discrepancy between the real world and the Black-Scholes data<\/li>\n<li class=\"wp-block-list-item\">\n<code>noise_variance<\/code>: the <strong>amount of noise<\/strong> added to the option prices to simulate measurement or market noise. This parameter is add for the same reason as before. <\/li>\n<li class=\"wp-block-list-item\">\n<code>epochs<\/code>: how many <strong>iterations<\/strong> the model will train for. <\/li>\n<li class=\"wp-block-list-item\">\n<code>lr<\/code>: the <strong>learning rate<\/strong> of the optimizer. This controls how fast the model updates during training.<\/li>\n<li class=\"wp-block-list-item\">\n<code>log_interval<\/code>: how often (in terms of epochs) we want to <strong>print logs<\/strong> to monitor training progress.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Each of these parameters plays a specific role, some shape the financial world we\u2019re simulating, others control how our neural network interacts with that world. Small tweaks here can lead to very different behavior, which makes this file both powerful and delicate. Changing the values of this JSON file will radically change the output of the code. <\/p>\n<h3 class=\"wp-block-heading\">3.2 main.py<\/h3>\n<p class=\"wp-block-paragraph\">Now let\u2019s look at how the rest of the code uses this config in practice.<\/p>\n<p class=\"wp-block-paragraph\">The main part of our code comes from <strong><em>main.py<\/em><\/strong>, train your PINN using Torch, and <strong><em>black_scholes<\/em>.py<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">This is main.py:<\/p>\n<div class=\"wp-block-tds-gist-embed\">\n\t<script src=\"https:\/\/gist.github.com\/PieroPaialungaAI\/14dc8c3bbe314216382574442fde37a9.js\"><\/script>\n<\/div>\n<p class=\"wp-block-paragraph\">So what you can do is:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Build your config.json file <\/li>\n<li class=\"wp-block-list-item\">Run <code>python main.py --config config.json<\/code>\n<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">main.py uses a lot of other files.<\/p>\n<h3 class=\"wp-block-heading\">3.3 black_scholes.py and helpers<\/h3>\n<p class=\"wp-block-paragraph\">The implementation of the model is inside <strong>black_scholes.py<\/strong>:<\/p>\n<div class=\"wp-block-tds-gist-embed\">\n\t<script src=\"https:\/\/gist.github.com\/PieroPaialungaAI\/f8bf7da09c50d95c1b8b9ce56fb07307.js\"><\/script>\n<\/div>\n<p class=\"wp-block-paragraph\">This can be used to build the model, train, export, and predict. <br \/>The function uses some helpers as well, like data.py, loss.py, and model.py. <br \/>The torch model is inside <strong>model.py<\/strong>:<\/p>\n<div class=\"wp-block-tds-gist-embed\">\n\t<script src=\"https:\/\/gist.github.com\/PieroPaialungaAI\/469667d1f17ee6a35978f653b9cd007f.js\"><\/script>\n<\/div>\n<p class=\"wp-block-paragraph\">The data builder (given the config file) is inside <strong>data<\/strong><em><strong>.<\/strong><\/em><strong>py<\/strong>:<\/p>\n<div class=\"wp-block-tds-gist-embed\">\n\t<script src=\"https:\/\/gist.github.com\/PieroPaialungaAI\/785655bc73b8d82773b0fbbd3f3d7680.js\"><\/script>\n<\/div>\n<p class=\"wp-block-paragraph\">And the beautiful loss function that incorporates the value of is <strong>loss.py<\/strong><\/p>\n<div class=\"wp-block-tds-gist-embed\">\n\t<script src=\"https:\/\/gist.github.com\/PieroPaialungaAI\/15dc3747c9a0de02913b562b26e4c961.js\"><\/script>\n<\/div>\n<h3 class=\"wp-block-heading\">4. Results<\/h3>\n<p class=\"wp-block-paragraph\">Ok, so if we run main.py, our FFNN gets trained, and we get this.<\/p>\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" height=\"201\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-16-at-6.40.02-PM-1024x201.png?resize=1024%2C201&#038;ssl=1\" alt=\"\" class=\"wp-image-601676\"><figcaption class=\"wp-element-caption\">Image made by author<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you notice, the model error is not quite 0, but the PDE of the model is much smaller than the data. That means that the model is (naturally) aggressively forcing our predictions to meet the differential equations. This is exactly what we said before: we optimize both in terms of the data that we have and in terms of the Black-Scholes model. <\/p>\n<p class=\"wp-block-paragraph\">We can notice, qualitatively, that there is a great match between the noisy + biased real-world (rather realistic-world lol) dataset and the PINN. <\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/04\/image-53.png?ssl=1\" alt=\"\" class=\"wp-image-601677\"><figcaption class=\"wp-element-caption\">Image made by author<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">These are the results when t = 0, and the Stock price changes with the Call Option at a fixed t. Pretty cool, right? But it\u2019s not over! You can explore the results using the code above in two ways:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Playing with the multitude of <strong>parameters<\/strong> that you have in config.json<\/li>\n<li class=\"wp-block-list-item\">Seeing the predictions at <strong>t&gt;0<\/strong>\n<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">Have fun! <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f642.png?ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> <\/p>\n<h3 class=\"wp-block-heading\">5. Conclusions<\/h3>\n<p class=\"wp-block-paragraph\">Thank you so much for making it all the way through. Seriously, this was a long one <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f605.png?ssl=1\" alt=\"\ud83d\ude05\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><br \/>Here\u2019s what you\u2019ve seen in this article:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong>We started with Physics<\/strong>, and how its rules, written as differential equations, are fair, beautiful, and (usually) predictable.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>We jumped into Finance<\/strong>, and met the Black-Scholes model \u2014 a differential equation that aims to price options in a risk-free way.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>We explored Physics-Informed Neural Networks (PINNs)<\/strong>, a type of neural network that doesn\u2019t just fit data but respects the underlying differential equation.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>We implemented everything in Python<\/strong>, using PyTorch and a clean, modular codebase that lets you tweak parameters, generate synthetic data, and train your own PINNs to solve Black-Scholes.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>We visualized the results<\/strong> and saw how the network learned to match not only the noisy data but also the behavior expected by the Black-Scholes equation.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">Now, I know that digesting all of this at once is not easy. In some areas, I was necessarily short, maybe shorter than I needed to be. Nonetheless, if you want to see things in a clearer way, again, give a look at the <a href=\"https:\/\/github.com\/PieroPaialungaAI\/BlackScholesPINN\/tree\/main\">GitHub folder.<\/a> Even if you are not into software, there is a clear README.md and a simple <strong>example\/BlackScholesModel.ipynb <\/strong>that explains the project step by step.<\/p>\n<h3 class=\"wp-block-heading\">6. About\u00a0me!<\/h3>\n<p class=\"wp-block-paragraph\">Thank you again for your time. It means a lot\u00a0<img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/2764.png?ssl=1\" alt=\"\u2764\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><\/p>\n<p class=\"wp-block-paragraph\">My name is Piero Paialunga, and I\u2019m this guy here:<\/p>\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" data-dominant-color=\"a6a1a0\" data-has-transparency=\"true\" style=\"--dominant-color: #a6a1a0;\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/0_w9Y8ftqBkR5kNWR5-1024x1024.png?resize=1024%2C1024&#038;ssl=1\" alt=\"\" class=\"wp-image-597454 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/0_w9Y8ftqBkR5kNWR5-1024x1024.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/0_w9Y8ftqBkR5kNWR5-300x300.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/0_w9Y8ftqBkR5kNWR5-150x150.png 150w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/0_w9Y8ftqBkR5kNWR5-768x768.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/0_w9Y8ftqBkR5kNWR5.png 1080w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\n<p class=\"wp-block-paragraph\">I am a Ph.D. candidate at the University of Cincinnati Aerospace Engineering Department. I talk about AI, and <a href=\"https:\/\/towardsdatascience.com\/tag\/machine-learning\/\" title=\"Machine Learning\">Machine Learning<\/a> in my blog posts and on LinkedIn and here on TDS. If you liked the article and want to know more about machine learning and follow my studies you can:<\/p>\n<p class=\"wp-block-paragraph\">A. Follow me on\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/pieropaialunga\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Linkedin<\/strong><\/a>, where I publish all my stories<br \/>B. Follow me on <a href=\"https:\/\/github.com\/PieroPaialungaAI\"><strong>GitHub<\/strong><\/a>, where you can see all my code<br \/>C. Send me an email: <em><strong>piero.paialunga@hotmail.com<\/strong><\/em><br \/>D. Want to work with me? Check my rates and projects on\u00a0<a href=\"https:\/\/www.upwork.com\/freelancers\/~017f9a75d13c030610\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Upwork<\/strong><\/a>!<\/p>\n<p class=\"wp-block-paragraph\">Ciao. <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/2764.png?ssl=1\" alt=\"\u2764\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"><\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">P.S. My PhD is ending and I\u2019m considering my next step for my career! If you like how I work and you want to hire me, don\u2019t hesitate to reach out. <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f642.png?ssl=1\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> <\/p>\n<\/blockquote>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/when-physics-meets-finance-using-ai-to-solve-black-scholes\/\">When Physics Meets Finance: Using AI to Solve Black-Scholes<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Piero Paialunga<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/when-physics-meets-finance-using-ai-to-solve-black-scholes\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When Physics Meets Finance: Using AI to Solve Black-Scholes DISCLAIMER: This is not financial advice. I\u2019m a PhD in Aerospace Engineering with a strong focus on Machine Learning: I\u2019m not a financial advisor. This article is intended solely to demonstrate the power of Physics-Informed Neural Networks (PINNs) in a financial context. When I was 16, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,69,88,2401,70,157],"tags":[2402,1918,12],"class_list":["post-3181","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-artificial-intelligence","category-deep-learning","category-finance","category-machine-learning","category-python","tag-differential","tag-physics","tag-was"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/3181"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=3181"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/3181\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=3181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=3181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=3181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}