【发布时间】:2014-12-31 04:56:23
【问题描述】:
我刚从 Daniel Duffy 的《金融工程师 C++ 简介:面向对象的方法》一书中开始学习金融领域的 C++。经过一些工作,我完成了前两章,但关于课程的第三章让我有点困惑。我知道一个类由成员数据(在本例中为头文件)和成员函数(在本例中为代码文件)组成。本书继续讨论了构造函数、修饰符、选择器和析构函数。下面的头文件和代码文件中粘贴的内容可能是以显示这些功能的方式编码的。 我不想让这段代码更有效率,我想了解发生了什么。这个程序应该给出欧式期权的看跌/看涨价格及其各自的增量。我在概念上有点理解事物,但语法和结构才是真正让我困惑的地方。当我查找诸如 void 之类的定义时,我看不出它们如何与代码相匹配。
我会评论我认为正在发生的事情。我真的很感激一些帮助,因为此时我完全处于黑暗之中。最后带问号的 cmets 是我最需要帮助的地方。
还有一个资源可以帮助我对 C++ 中的事物和语法进行基本的外行解释。我不想掩盖本书或学习 C++ 中的任何内容。我想完全理解每一行代码。
如果需要,我可以上传或通过电子邮件发送 xcode 项目/文件。
提前感谢您,对于任何愚蠢的错误,我深表歉意。
//////////////类头文件/////////////////
#ifndef EuropeanOption_hpp //Regular header looking stuff. Not sure exactly what it means.
#define EuropeanOption_hpp
#include <string> //Include this if you are using strings.
using namespace std; //Adding in the standard namespace library. Be careful with conflits between libraries in the future.
class EuropeanOption //Looks like we are defining a class here.
{
public: //Public members of the EuropeanOption class can be accessed from anywhere.
void init(); // What is this?
void copy(const EuropeanOption& o2); //What's going on here?
//Defining some constants here that we will do calculations later to get values for. These do not change hence the "const."
double CallPrice() const;
double PutPrice() const;
double CallDelta() const;
double PutDelta() const;
double CallGamma() const;
double PutGamma() const;
double CallVega() const;
double PutVega() const;
//Stuff for the overly used and abused normal distribution that we will calculate later.
double n(double x) const;
double N(double x) const;
//Defining some parameters. These can change, no const at the end.
double r;
double sig;
double K;
double T;
double U;
double b;
string optType; // Option name (call, put)
string unam; // Name of underlying asset
public: //Public members of the EuropeanOption class can be accessed from anywhere.
EuropeanOption(); // Default call option
EuropeanOption(const EuropeanOption& option2); //What?
EuropeanOption (const string& optionType); //Who?
virtual ~EuropeanOption(); //Where?
EuropeanOption& operator = (const EuropeanOption& option2); //No idea what this is.
// Functions that calculate option price and sensitivities
double Price() const;
double Delta() const;
void toggle(); //What?
};
#endif
/////////////类代码文件///////////////
#ifndef EuropeanOption_cpp //Again, not really sure what this is but I know its important.
#define EuropeanOption_cpp
#include "EuropeanOption.hpp"
#include <math.h> //Include a math library.
#include <iostream> //Include IO stuff.
//////////// Gaussian functions /////////////////////////////////
double EuropeanOption::n(double x) const //With :: we made little n's member function part of the EuropeanOption class?
{
double A = 1.0/sqrt(2.0 * 3.1415);
return A * exp(-x*x*0.5);
}
double EuropeanOption::N(double x) const //Here we used little n to make big N's member function part of the European option class?
{ // The approximation to the cumulative normal distribution
double a1 = 0.4361836;
double a2 = -0.1201676;
double a3 = 0.9372980;
double k = 1.0/(1.0 + (0.33267 * x));
if (x >= 0.0)
{
return 1.0 - n(x)* (a1*k + (a2*k*k) + (a3*k*k*k));
}
else
{
return 1.0 - N(-x);
}
}
// Black and Scholes stock option model (1973)
double EuropeanOption::CallPrice() const //Defined callprices's function as part of the EuropeanOption class?
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
double d2 = d1 - tmp;
return (U * exp((b-r)*T) * N(d1)) - (K * exp(-r * T)* N(d2));
}
double EuropeanOption::PutPrice() const //Defined putprices's function as part of the EuropeanOption class?
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
double d2 = d1 - tmp;
return (K * exp(-r * T)* N(-d2)) - (U * exp((b-r)*T) * N(-d1));
}
double EuropeanOption::CallDelta() const
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
return exp((b-r)*T) * N(d1);
}
double EuropeanOption::PutDelta() const
{
double tmp = sig * sqrt(T);
double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
return exp((b-r)*T) * (N(d1) - 1.0);
}
/////////////////////////////////////////////////////////////////////////////////////
void EuropeanOption::init() //What is going on here?
{
r = 0.08;
sig= 0.30;
K = 65.0;
T = 0.25;
U = 60.0;
b = r;
optType = "C"; //What?
}
void EuropeanOption::copy(const EuropeanOption& o2) //What's going on here?
{
r = o2.r;
sig = o2.sig;
K = o2.K;
T = o2.T;
U = o2.U;
b = o2.b;
optType = o2.optType;
}
EuropeanOption::EuropeanOption() //Completely lost.
{
init();
}
EuropeanOption::EuropeanOption(const EuropeanOption& o2) //??
{
copy(o2);
}
EuropeanOption::EuropeanOption (const string& optionType) //??
{
init();
optType = optionType;
if (optType == "c")
optType = "C";
}
EuropeanOption::~EuropeanOption() //I've never seen the swiggly before.
{
}
EuropeanOption& EuropeanOption::operator = (const EuropeanOption& option2) //Still lost.
{
if (this == &option2) return *this;
copy (option2);
return *this;
}
double EuropeanOption::Price() const //Looks like we are returning either a call or put price based on what was entered before??
{
if (optType == "C")
{
return CallPrice();
}
else
return PutPrice();
}
double EuropeanOption::Delta() const
{
if (optType == "C")
return CallDelta();
else
return PutDelta();
}
void EuropeanOption::toggle()
{ // Change option type (C/P, P/C)??
if (optType == "C")
optType = "P";
else
optType = "C";
}
#endif
//////////主代码//////////////
#include "EuropeanOption.hpp"
#include <iostream>
//I did not comment on this section because I don't understand most of it and I think once I understand the class cpp & hpp files this will start to click. I do understand basic things here like int main(), endl, cout, cin. I also left the original author's comments in from the book.
int main()
{
EuropeanOption callOption;
cout << "Call option on a stock: " << callOption.Price() << endl;
// Put option on a stock index
EuropeanOption indexOption;
indexOption.optType = "P";
indexOption.U = 100.0;
indexOption.K = 95.0;
indexOption.T = 0.5;
indexOption.r = 0.10;
indexOption.sig = 0.20;
double q = 0.05; // Dividend yield
indexOption.b = indexOption.r - q;
cout << "Put option on an index: " << indexOption.Price() << endl;
// Call and put options on a future
EuropeanOption futureOption;
futureOption.optType = "P";
futureOption.U = 19.0;
futureOption.K = 19.0;
futureOption.T = 0.75;
futureOption.r = 0.10;
futureOption.sig = 0.28;
futureOption.b = 0.0;
cout << "Put option on a future: " << futureOption.Price() << endl;
// Now change over to a call on the option
futureOption.toggle();
cout << "Call option on a future: " << futureOption.Price() << endl;
// Call option on currency
EuropeanOption currencyOption;
currencyOption.optType = "C";
currencyOption.U = 1.56;
currencyOption.K = 1.60;
currencyOption.T = 0.5;
currencyOption.r = 0.06;
currencyOption.sig = 0.12;
double rf = 0.08; // risk-free rate of foreign currency
currencyOption.b = currencyOption.r - rf;
cout << endl << "** Other pricing examples **" << endl << endl;
cout << "Call option on a currency: " << currencyOption.Price() << endl;
//////// NOW CALCULATIONS OF SENSITIVITIES //////////////////////////////////
// Call and put options on a future: Delta and Elasticity
EuropeanOption futureOption2;
futureOption2.optType = "P";
futureOption2.U = 105.0;
futureOption2.K = 100.0;
futureOption2.T = 0.5;
futureOption2.r = 0.10;
futureOption2.sig = 0.36;
futureOption2.b = 0.0;
cout << "Delta on a put future: " << futureOption2.Delta() << endl;
// Now change over to a call on the option
futureOption2.toggle();
cout << "Delta on a call future: " << futureOption2.Delta() << endl;
// Stock Option: Gamma
EuropeanOption stockOption;
stockOption.optType = "C";
stockOption.U = 55.0;
stockOption.K = 60.0;
stockOption.T = 0.75;
stockOption.r = 0.10;
stockOption.sig = 0.30;
stockOption.b = stockOption.r;
stockOption.toggle();
// Calculating theta of a European stock index
EuropeanOption indexOption2;
indexOption2.optType = "P";
indexOption2.U = 430.0;
indexOption2.K = 405.0;
indexOption2.T = 0.0833; // One month expiration
indexOption2.r = 0.07;
indexOption2.sig = 0.20;
double divYield = 0.05; // Dividend yield, 5% per annum
indexOption2.b = indexOption2.r - divYield;
// Stock Option: Rho
EuropeanOption stockOption2;
stockOption2.optType = "C";
stockOption2.U = 72.0;
stockOption2.K = 75.0;
stockOption2.T = 1.0;
stockOption2.r = 0.09;
stockOption2.sig = 0.19;
stockOption2.b = stockOption2.r;
// Calculating Cost of Carry of a European stock index
EuropeanOption indexOption3;
indexOption3.optType = "P";
indexOption3.U = 500.0;
indexOption3.K = 490.0;
indexOption3.T = 0.222225;
indexOption3.r = 0.08;
indexOption3.sig = 0.15;
double divYield3 = 0.05; // Dividend yield, 5% per annum
indexOption3.b = indexOption3.r - divYield3 ;
return 0;
}
【问题讨论】:
-
在为金融工程师深入研究 C++ 之前,您可能会一目了然地花时间研究 C++ 本身。如果您更了解核心语言及其工作原理(怪癖和所有内容),那么您在这些文件中标记的大多数内容可能对您来说会更清楚。一些潜在的书源can be found here。一个优秀的语言和标准库参考站点can be found here。
-
我还建议您获取代码,编译它,然后在调试器下一次一步运行它。这样,您可以看到程序的流程、观察变量、每一行的作用等等。
-
如果你想很好地理解C++,你必须长期学习和使用它;我们无法在几页文字中传达这种专业知识。至于您的具体问题,我什至无法在简要阅读您的帖子中找到它们(这是我愿意在没有看到任何光明的情况下投资的全部内容)。尝试使用simplest code that can illustrate it 将其简化为一个简单的问题。而且不仅仅是为了我们,或者帮助吸引有用的答案——总的来说,这是一个非常有用的习惯。