{"id":1582,"date":"2020-10-04T03:12:01","date_gmt":"2020-10-04T03:12:01","guid":{"rendered":"http:\/\/www.financial-insurance.com\/2020-daily-trail-markers-how-ads-from-the-two-campaigns-differ-in-the-final-sprint\/"},"modified":"2023-02-18T13:14:51","modified_gmt":"2023-02-18T13:14:51","slug":"2020-day-by-day-path-markers-how-advertisements-from-the-2-campaigns-differ-within-the-ultimate-dash","status":"publish","type":"post","link":"http:\/\/www.financial-insurance.com\/?p=1582","title":{"rendered":"Python and Finance: Why Use Python for Finance"},"content":{"rendered":"<p>1.3.1 Finance and Python syntax<br \/>\nMost people who take their first steps in using Python in a financial environment are likely to be tackling some algorithmic problem. This is similar to the scientist who wants to solve differential equations, find integrals, or visualize certain data. In general, there is not much demand for formal development processes, testing, documentation, or deployment at this stage. However, this phase seems to be when people fall in love with Python in particular, mainly because Python&#8217;s syntax in general is fairly close to the mathematical syntax used to describe scientific problems or financial algorithms.<br \/>\nWe can illustrate this phenomenon with a simple financial algorithm &#8211; estimating the value of a European call option by means of a Monte Carlo simulation. We will consider the Black-Scholes-Merton (BSM) model, in which the potential risk of the option follows a geometric Brownian motion.<br \/>\nIt is assumed that we use the following numerical parameters for valuation.<br \/>\n\uf0b7 initial stock index level S0 = 100.<br \/>\n\uf0b7 the strike price K=105 for the European call option.<br \/>\n\uf0b7 maturity time T=1 year.<br \/>\n\uf0b7 fixed risk-free short-term interest rate r=5%.<br \/>\n\uf0b7 fixed volatility \u03c3=20%.<br \/>\nIn the BSM model, the level of the expiration index is a random variable given by Equation 1-1, where z is a standard normally distributed random variable.<br \/>\nEquation 1-1 Black-Scholes-Merton (1973) Expiration Index Level<\/p>\n<p>&nbsp;<\/p>\n<p>The following is an algorithmic description of the Monte Carlo valuation process.<br \/>\n(1) Obtain I (pseudo) random numbers z(i) from a standard normal distribution, i \u2208 {1, 2, &#8230;, I}.<br \/>\n(2) Compute all expiration index levels ST(i) for the given z(i) and Equation 1-1.<br \/>\n(3) Calculate all intrinsic values hT(i)=max(ST(i)-K,0) for the expiration period right.<br \/>\n(4) Estimate the present value of the option through the Monte Carlo estimation function given in Equation 1-2.<br \/>\nEquation 1-2 Monte Carlo estimation function for Euclidean options<\/p>\n<p>&nbsp;<\/p>\n<p>Now, we need to translate this problem and algorithm into Python code. The following code will implement some of the necessary steps.<br \/>\nIn [6]: import math<br \/>\nimport numpy as np \u2776<\/p>\n<p>In [7]: S0 = 100. \u2777<br \/>\nK = 105. \u2777<br \/>\nT = 1.0 \u2777<br \/>\nr = 0.05 \u2777<br \/>\nsigma = 0.2 \u2777<\/p>\n<p>In [8]: I = 100000 \u2777<\/p>\n<p>In [9]: np.random.seed(1000) \u2778<br \/>\nIn [10]: z = np.random.standard_normal(I) \u2779<\/p>\n<p>In [11]: ST = S0 * np.exp((r &#8211; sigma ** 2 \/ 2) * T + sigma * math.sqrt(T) * z) \u277a<\/p>\n<p>In [12]: hT = np.maximum(ST &#8211; K, 0) \u277b<\/p>\n<p>In [13]: C0 = math.exp(-r * T) * np.mean(hT) \u277c<\/p>\n<p>In [14]: print(&#8216;Value of the European call option: {:5.3f}.&#8217; .format(C0)) \u277d<br \/>\nValue of the European call option: 8.019.<br \/>\n\u2776 NumPy is used here as the main program package.<br \/>\n\u2777 Define the model and simulate the parameter values.<br \/>\n\u2778 The random number generator seed value is fixed.<br \/>\n\u2779 Extract standard normally distributed random numbers.<br \/>\n\u277a Simulate the end-of-period values.<br \/>\n\u277b Calculate the option maturity return.<br \/>\n\u277c Calculate the Monte Carlo estimation function.<br \/>\n\u277d Print out the estimation result.<br \/>\nThe following 3 aspects are worth noting.<br \/>\nSyntax<br \/>\nPython syntax is quite close to mathematical syntax, such as the aspect of parameter assignment.<br \/>\nTranslation<br \/>\nEach mathematical or algorithmic statement can generally be translated into a single line of Python code.<br \/>\nVectorization<br \/>\nOne of the strengths of NumPy is the compact vectorization syntax, which allows, for example, 100,000 calculations to be performed in a single line of code.<br \/>\nThis code can be used in interactive environments such as IPython or Jupyter Notebook. However, code that needs to be reused frequently is generally organized as so-called modules (or scripts), which are Python (text) files with a .py suffix. The module for this example is shown in Example 1-1, and can be saved as a file named bsm_msc_euro.py.<br \/>\nExample 1-1 Monte Carlo Valuation of a European Call Option<br \/>\n#<br \/>\n# Monte Carlo valuation of European call option<br \/>\n# in Black-Scholes-Merton model<br \/>\n# bsm_mcs_euro.py<br \/>\n#<br \/>\n# Python for Finance, 2nd ed.<br \/>\n# (c) Dr. Yves J. Hilpisch<br \/>\n# in<br \/>\nimport math<br \/>\nimport numpy as np<\/p>\n<p># Parameter Values<br \/>\nS0 = 100. # initial index level<br \/>\nK = 105. # strike price<br \/>\nT = 1.0 # time-to-maturity<br \/>\nr = 0.05 # riskless short rate<br \/>\nsigma = 0.2 # volatility<\/p>\n<p>I = 100000 # number of simulations<\/p>\n<p># Valuation Algorithm<br \/>\nz = np.random.standard_normal(I) # pseudo-random numbers<br \/>\n# index values at maturity<br \/>\nST = S0 * np.exp((r &#8211; 0.5 * sigma ** 2) * T + sigma * math.sqrt(T) * z)<br \/>\nhT = np.maximum(ST &#8211; K, 0) # payoff at maturity<br \/>\nC0 = math.exp(-r * T) * np.mean(hT) # Monte Carlo estimator<\/p>\n<p># Result Output<br \/>\nprint(&#8216;Value of the European call option %5.3f.&#8217; % C0)<br \/>\nThe simple algorithm example in this subsection illustrates that Python&#8217;s basic syntax is well suited to complement the classic scientific language duo of English and mathematics. Adding Python to a portfolio of scientific languages can make it even more comprehensive. We now have.<br \/>\n\uf0b7 for writing and talking about science, finance, and other issues in English.<br \/>\n\uf0b7 mathematics for succinctly and precisely describing and modeling abstract features, algorithms, complex numbers, etc.<br \/>\n\uf0b7 Python for technically modeling and implementing abstract features, algorithms, complex numbers, etc.<\/p>\n<p>Mathematics and Python syntax<br \/>\nThere is hardly any programming language that comes as close to mathematical syntax as Python. As a result, numerical algorithms are easily translated from mathematical representations to Python implementations. With Python, we can prototype, develop, and maintain code in these domains efficiently.<br \/>\nIn some domains, it is common practice to use pseudocode, which introduces a 4th language family member. As an example, the task of pseudocode is to represent financial algorithms in a more technical way, close not only to the mathematical representation, but also to the technical implementation. In addition to the algorithm itself, the pseudocode takes into account the working principles of the computer.<br \/>\nThis approach is generally adopted because, with most programming languages, the distance between the technical implementation and the formal mathematical representation is quite &#8220;distant&#8221;. Most programming languages must contain many elements that are only technically necessary, but it is difficult to see the equivalent in mathematics and code.<br \/>\nToday, Python is often used as pseudocode because its syntax is similar to mathematics and the technical &#8220;overhead&#8221; can be kept to a minimum. This is achieved through the high-level concepts embodied in the language, which not only have their advantages, but also entail risks and other costs. But to be sure, we can use Python as the need arises, following from the outset the rigorous implementation and coding methods that other languages might require. In this sense, Python can provide the best balance between two worlds: high level abstraction and strict implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.3.1 Finance and Python syntax Most people who take their first steps in using Python in a financial environment are likely to be tackling some algorithmic problem. This is similar to the scientist who wants to solve differential equations, find integrals, or visualize certain data. In general, there is not much demand for formal development processes, testing, documentation, or deployment at this stage. However, this phase seems to be when people fall in love with Python in particular, mainly because Python&#8217;s syntax in general is fairly close to the mathematical syntax used to describe scientific problems or financial algorithms. We [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2505,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"fifu_image_url":"http:\/\/www.financial-insurance.com\/wp-content\/uploads\/2020\/10\/flying-g2b691e24b_1280.jpg","fifu_image_alt":"2020 Day by day Path Markers: How advertisements from the 2 campaigns differ within the ultimate dash","footnotes":""},"categories":[3],"tags":[136,137,76,131,86,88,77,87,135],"class_list":["post-1582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-financial","tag-2020-united-states-presidential-election","tag-ads","tag-branch-financial","tag-donald-trump","tag-financial","tag-financial-advice","tag-financial-news","tag-financial-tips","tag-joe-biden"],"_links":{"self":[{"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/posts\/1582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1582"}],"version-history":[{"count":3,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/posts\/1582\/revisions"}],"predecessor-version":[{"id":2504,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/posts\/1582\/revisions\/2504"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=\/wp\/v2\/media\/2505"}],"wp:attachment":[{"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1582"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.financial-insurance.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}