IB9JH0 c语言编程

尾调星轨
• 阅读 119

在这项任务中,您将使用C编程语言编写代码,该语言使用CRR二项式对固定年度基础资产的普通欧洲/美国期权进行估值的方法股息收益率。您将使用两种不同的算法来实现二项式方法给出相同的结果,使您能够验证是否正确地实现了它们。此外,您
将验证您的计算解决方案是否接近欧洲选项的分析解决方案如果时间间隔足够小,则由Black-Scholes形式主义给出。您可以在解决方案中使用任何标准库数据结构/函数。但是,不要使用任何不属于标准的第三方库。您的代码应该格式清晰并附有详细评论。在如何实现代码方面,您有一定的灵活性并且经过测试,但是您的解决方案应该包含在
文档,以便对其进行测试。非常重要的是,您的最终提交文件仅包含指定的文件,并且无需修改即可编译这些文件。如果你的代码不能在visualstudio或onlineGDB中编译,您可以使用的标记数量非常有限分数请测试它在以上至少一种情况下是否有效。您将获得的模板文件,让你开始

1. IntrodcutioNiNIntrodcution

IB9JH0 Programming Assignment 1In this assignment you will write code in the C programming language which uses the CRR binomialmethod to value vanilla European/American options for an underlying asset with a fixed annualdividend yield. You will implement the binomial method using two different algorithms which shouldgive the same result, allowing you to verify you have implemented them correctly. In addition, youwill verify that your calculated solution approaches the analytical solutions for European optionsgiven by Black-Scholes formalism if the time intervals are sufficiently small.You may use any standard library data structures/functions in your solution. However, do not useany third-party libraries which are not part of the standard. Your code should be clearly formattedand annotated with detailed comments. You have some flexibility in how the code is implementedand tested, but your solution should contain the file structure and functions specified in thisdocument so that it can be tested. It is very important that your final submission only contains thefiles specified, and these files can be compiled without needing modification. If your code can’t becompiled in either visual studio or onlineGDB, you are very limited in the number of marks you canscore. Please test it works in at least one of the above. You will be provided with templates of thefiles to get you started. Please read the document carefully to make sure your submission is inthecorrect format.Submit the followingfiles:OptionPricingFunctions.h OptionPricingFunctions.c Testing. Analysis.c

2. Black Scholes

Black Scholes (European options):For European options, you should find that using larger trees/more timesteps causes the binomialalgorithm to approach the analytical solution for the value of the option in the risk-free frameworkimposed by the Black-Scholes model. The analytical solutions for calls and puts are omitted here asthey are widely known and easy to find. The Black Scholes solution requires one to calculate thenormal cumulative distribution function (). In C this can be done by using the erfc() libraryfunction with the following transform:Forward Recursion method (European options):This method starts from timestep zero and accumulates the option value from the next timestepusing the recurrence relation:is the option value, r is the risk-free interest rate, ▽ is the time increment between eachtimestep, p is the risk-neutral probability of an upwards jump, i is the timestep/tree depth of thecurrent node, and j is height of the current node. This can be visualised in the figure below for a treeof depth 2:

The recurrence relation can be implemented by utilising recursion in C. Note that you will need tokeep track of i and j in your recursive function calls. The recursion terminates at the base of thebinomial tree where i is equal to the depth of the tree (i = 2 in the above figure). For a tree of depthn, the values at the base of the tree are the values of the option at the expiry time, and can becomputed as follows:(2)= max(02 , 0) for call options(3)= max( 02, 0) for put optionsWhere 0 is the asset price at time zero (i = 0) and k is the strike price.Forward Recursion method (American options):The difference in the calculation for American options in comparison to European options is thatthey can be exercised by the contract buyer before the expiry time. This means that the exercisevalue

needs to be calculated at each node:The rest of the algorithm is identical to the version for European options.Backward Induction method:The backward induction method starts from the last timestep and computes the option value at allthe base nodes using equation 2 (calls) or 3 (puts). Next, it uses the recurrence relation fromequation 1 (European options) or equation 6 (American options) to compute the value of all thenodes at the previous timestep. This process is repeated until time zero. You will use a nested loopwith two layers. For a tree of depth n, the outer loop is over timesteps from i = n – 1 down to i = 0.The inner loop is from node height j = 0 to j = i. You need to use an array to store the values at eachtimestep. A single one-dimensional array of size n + 1 is sufficient to price an option with a tree ofdepth n. This is because the values calculated at timestep i will replace the valuescalculated attimestep i + 1. That is, for all j-values at timestep i,will replace +1in the array.

3. Task

Task 1. Implementing the Algorithms:The assignment requires you to complete the functions listed below so they work as described. Youare free to add any additional functions you require in OptionPricingFunctions.c. Do NOT modify thefunction signatures or names. The input variables must stay the same. Moreover, do NOT modifythe header file OptionPricingFunctions.h.

 void price_vanilla_option_european_bs(double S0, double r, double volatility,double strike_price, double dividend_yield, double expiration_time, double*call_price, double* put_price)
 double price_vanilla_option_european_recursion(unsigned int i, unsigned int j,unsigned int depth, double delta_t, double S0, double r,double volatility, double strike_price, double dividend_yield, doubleexpiration_time, option_fxn exercise_profit_calculator)
 double price_vanilla_option_american_recursion(unsigned int i, unsigned int j,unsigned int depth, double delta_t, double S0, double r,double volatility, double strike_price, double dividend_yield, doubleexpiration_time, option_fxn exercise_profit_calculator)
 double price_vanilla_option_european_induction(unsigned int depth, double delta_t, double S0, double r,double volatility, double strike_price, double dividend_yield,double expiration_time, option_fxn exercise_profit_calculator)

double price_vanilla_option_american_induction(unsigned int depth, double delta_t, double S0, double r,double volatility, double strike_price, double dividend_yield,double expiration_time, option_fxn exercise_profit_calculator)The functions should be implemented as follows: price_vanilla_option_european_bs – calculate call and put prices for European options usingthe Black-Scholes analytical solution. The outputs are stored in the call_price and put_pricevariables

 price_vanilla_option_european_recursion – price a European option using the forward-recursion method.

 price_vanilla_option_american_recursion – price an American option using the forward-recursion method.

 price_vanilla_option_european_induction - price a European option using the backward-induction method.

price_vanilla_option_american_induction - price an American option using the backward-induction method.Task 2. Testing:
Using only the functions exposed in the header fileOptionPricingFunctions.h (and any otherstandard C library code), demonstrate that the algorithms from task 1 are correctly implemented bywriting a function which tests the outputs under a range of input values (refer to general advicesection for help with this). The tests should print an alert to the console if an error is found. Save allyour testing code in Testing.c and annotate the tests with comments which explain what the codedoes.Task 3. Analysis:

Using only the functions exposed in the header file OptionPricingFunctions.h (and any otherstandard C library code), write code to investigate the following questions: What is the (time) performance difference between the two algorithms (recursion andinduction) What binomial tree depth is required to be reasonably certain the option values areaccurate to within 5 significant figures? How can I estimate the accuracy of an American option without an analytical solution?Save all your analysis code in Analysis.c and annotate the file with comments to explain what thecode does.

4.General Advice:

When testing/debugging, start with a tree depth of 2 so that you can verify the values inyour code by hand and identify where things are going wrong. Then try with bigger trees. Start by getting European call options correct for both the forward recursion method andthe backward induction method. Then it is relatively straightforward to extend this to putoptions, and finally American call/put options. A key thing to remember with testing is that you have two different methods (recursion andinduction) to calculate the exact same thing. Given the same inputs, the outputs should beidentical within machine precision. If the two different sources agree exactly for a few cases,it verifies that you likely implemented the algorithms correctly. The analytical solution isuseful for checking accuracy with respect to the true solution, but less useful for checkingthat the code implementation is correct. Do not take it for granted that your code will work in all four cases, even if it appears to workin one case. The only way to know for sure that things work is to test them. It is important to start working on the assignment early, so you have time to ask questionsthrough the forum or the support classes if you get stuck or need things clarified. You areencouraged to communicate with others, but do not share actual coded solutions and copyfrom each other (it is usually easy to tell if you did so do not risk it).
WX:codehelp

点赞
收藏
评论区
推荐文章
小尉迟 小尉迟
2年前
graphpad prism教程:如何使用 prism医学绘图分析软件?
1.当您启动Prism时,您将看到欢迎(文件..新建)对话框。您将看到代表八种数据表的八个选项卡。单击列选项卡。然后选择使用样本数据,并选择样本数据进行未配对t检验。笔记:要有效地使用Prism,您需要学习如何选择八种类型的数据表中的哪一种。请注意选择示例
Stella981 Stella981
3年前
Go 用 interface 模拟多态
多态是C这种语言中的概念,是指对不同的子类对象运行自己定义的方法。在Go语言中没有类的概念,但仍然可以使用structinterface来模拟实现类的功能。下面这个例子演示如何使用Go来模拟C中的多态行为。packagemainimport"fmt"//首先定义了一
Wesley13 Wesley13
3年前
MySql实现Oracle的decode方法
在Oracle中使用decode方法可以轻松实现代码和值之间的转换,但是在MySQL中该如何实现类似功能呢?MySQL中没有直接的方法可以使用,但是我们可以通过下面两种方法来实现:1.casewhenthen如:Selecttitle,caseEmergencywhen1then'紧急' else'普通'End as e
Wesley13 Wesley13
3年前
C 语言时隔 5 年重回巅峰,这 20 个热门项目拿去练手!
在上个月的TIOBE编程语言排名中,C语言和Java的差距只有0.01%。在近日TIOBE公布的2020年5月编程语言排行榜中,C语言成功超越了Java,重返第一的王者宝座!!(https://pic1.zhimg.com/v2b0ebb8d95506c0a43900550b0ccc2074_b.jpg)编程语言5
Stella981 Stella981
3年前
Beginning C++17, 5th Edition 免积分下载
!(https://static.oschina.net/uploads/img/201901/06114327_V43X.png)图书说明:了解如何使用更新的C17语言进行编程。您将从基础知识入手,逐步学习如何成为一名正在运行的C程序员。您所需要的只是BeginningC17和任何最新的C编译器,您
Wesley13 Wesley13
3年前
初探 Objective
作者:Cyandev,iOS和MacOS开发者,目前就职于字节跳动0x00前言异常处理是许多高级语言都具有的特性,它可以直接中断当前函数并将控制权转交给能够处理异常的函数。不同语言在异常处理的实现上各不相同,本文主要来分析一下ObjectiveC和C这两个语言。为什么要把ObjectiveC和
Wesley13 Wesley13
3年前
5点改善你的编程逻辑
编程逻辑是成为优秀开发人员的关键。也许,根据您的工作地点,您将使用更多算法。如果你是一名网页设计师,你可能不打算处理复杂的算法,但如果你是前端开发人员,可能会处理一些算法,如果你是一个后端开发人员,你会经常处理复杂算法。在这里,我将向您介绍5个点,_在我看来_,这些点有资源来改进我们的编程逻辑。本文适合所有人,如果我们设法开发出一个好的逻辑,我们
小万哥 小万哥
1年前
Python 中的数字类型与转换技巧
Python中有三种数字类型:int(整数)float(浮点数)complex(复数)当您将值分配给变量时,将创建数字类型的变量:示例:获取您自己的Python服务器Pythonx1inty2.8floatz1jcomplex要验证Python中任何对象的
小万哥 小万哥
1年前
C 语言:类型转换与常量的细致理解
C语言中的类型转换有时,您必须将一种数据类型的值转换为另一种类型。这称为类型转换隐式转换当您将一种类型的值分配给另一种类型的变量时,编译器会自动进行隐式转换。例如,如果您将一个int值分配给一个float类型:c//自动转换:inttofloatfloat
小万哥 小万哥
1年前
C 语言中布尔值的用法和案例解析
C语言中的布尔值在编程中,您经常需要一种只能有两个值的数据类型,例如:是/否开/关真/假为此,C语言有一个bool数据类型,称为布尔值。布尔变量在C语言中,bool类型不是内置数据类型,例如int或char它是在C99中引入的,您必须导入以下头文件才能使用
小万哥 小万哥
1年前
C 语言教程:条件和 if...else 语句
C语言中的条件和if...else语句您已经学习过C语言支持数学中的常见逻辑条件:小于:a<b小于或等于:ab大于或等于:ab等于:ab不等于:a!b您可以使用这些条件来根据不同的决策执行不同的操作。C语言具有以下条件语句:使用if来指定要执行的代码块,如