{"id":743,"date":"2022-08-30T15:06:26","date_gmt":"2022-08-30T15:06:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/python-binomial-option-pricing-code-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:26","modified_gmt":"2022-08-30T15:06:26","slug":"python-binomial-option-pricing-code-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/python-binomial-option-pricing-code-closed-collection-of-common-programming-errors\/","title":{"rendered":"Python &#8211; Binomial Option Pricing Code [closed]-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to finish this code, but I keep getting an unknown error message. I don&#8217;t understand what I am doing wrong. Sorry, I am new to python!<\/p>\n<p>I would appreciate any help!!<\/p>\n<pre><code>import math\n\ndef nCr(n,r):\n    f = math.factorial\n    return f(n) \/ f(r) \/ f(n-r)\n\nclass Option(object):\n\n    def __init__(self,s0,u,d,r,t,strike):\n        self.s0=s0\n        self.u=u\n        self.d=d\n        self.r=r\n        self.t=t\n        self.strike=strike\n\n    def price(self):\n        q = (self.r - self.d) \/ (self.u - self.d)\n        prc = 0\n        temp_stock = 0\n        temp_payout = 0\n        for x in range(1,self.t+1):\n            temp_stock = self.strike*(self.u**(4-x))*(self.d**(x-1))\n            temp_payout = max(temp_stock-self.strike,0)\n            prc += nCr(self.t,x-1)*(q**(4-x))*((1-q)**(x-1))*temp_payout\n        prc = prc \/ (self.r**self.t)\n        return prc\n\n\nnewOption = Option(100,1.07,0.93458,1.01,3,100)\nprint newOption.price()\n<\/code><\/pre>\n<ol>\n<li>\n<p>There are at least the following problems with your code:<\/p>\n<ul>\n<li>\n<p>you have to include the math module at the beginning of your program:<\/p>\n<p>import math<\/p>\n<\/li>\n<li>\n<p>you have to replace the &#8220;^&#8221; operator by &#8220;**&#8221; operator.<\/p>\n<\/li>\n<\/ul>\n<p>(If that isn&#8217;t enough check the indentation, as your indentation in this message has a small problem: the function price() is shifted to the left)<\/p>\n<p>With these corrections your program produces an output: 6.77357485848.<\/p>\n<p>Check if that&#8217;s your desired output.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:22:41. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to finish this code, but I keep getting an unknown error message. I don&#8217;t understand what I am doing wrong. Sorry, I am new to python! I would appreciate any help!! import math def nCr(n,r): f = math.factorial return f(n) \/ f(r) \/ f(n-r) class Option(object): def __init__(self,s0,u,d,r,t,strike): self.s0=s0 self.u=u self.d=d self.r=r [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-743","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/743\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}