您的位置:首页 > 理论基础 > 数据结构算法

MFE常用数据结构之Lattice

2013-05-24 23:54 134 查看
今天看了DUFFY的C++ For FE中关于介绍Lattice的相关内容,为了表示对原作者的尊敬,首先我还是引用一下作者关于Lattice Structures的介绍的原话。

"Lattice structures are well known in quantitative finance.“

对,没错,原话就这么短。也许是太出名了吧,不需要过多赘述。不过我发现一个奇怪的现象就是在实现这个数据结构的过程中,不管是百度还是google,关于Lattice Structures的文章是少之又少,这极大地打击了我的积极性,虽然学技术不能这么功利,但是我的确很纳闷,真的很出名,经常被用到吗?

言归正传,Lattice Structures(LS)是一种类似树的结构体,但是它和一般的树还是有所区别,如图:

首先用到了一个自己实现的Array类,其中有一个类型为V的指针数组,每个数组成员代表该LS结构的1层,每一层上面是一个Vector对象,由于Vector对象的强大功能,可以用来存放任何对象,这也为我们后续开发提供了便利。

这里有几个问题需要注意,就是对每一层的vector初始化,由于LS结构第i层节点数是第i-1层节点数+该LS的(N-1)如上图,所以需要在构造Lattice的对象时,对Array中的指针数组按层进行初始化,即初始化每个vector的大小。

具体的过程这里不累述了,有许多bug耗费了本人大量精力,苦不堪言。

下面我简答谈谈这种数据结构的作用,由于第一天接触,所以难免有许多误解,希望看到的朋友能够指出,本人一定虚心接受。

LS(2)可以用来计算Option price,如给1个初始价格在根节点,然后我们预测他有两种(实际也只有两种)价格走势,一种是朝着左子节点的看跌,另一种是朝着右子结点的看涨。假设根节点的值为Value,那么左子节点的值就为Value*down,右子结点为Value*up。

同理,我们如果知道某一层所有的节点值,然后得到一些其他的条件值,我们可以反推得到根植,计算出该option的初始价格。

另外,该结构如此复杂,想必还有很多更高深的功能,非本人现有能力所及。总之还是那句话,见证我的技术成长路线!



PS:下面给出两个重要头文件,具体实现太挫,就不献丑了。

Array类

#ifndef Array_hpp

#define Array_hpp

template<int def_size,class Type>

class Array

{

private:
Type* m_data;//数组用来存储元素
int m_size;//数组大小

public:
int minI;

int maxI;
// Constructors & destructor
Array();
Array(int size);
Array(const Array<def_size,Type>& source);
virtual ~Array();
// Selectors
int Size() const;
// Operators
const Type& operator[](int index) const;// Index operator for const Arrays
Type& operator[](int index);// Index operator for non const Arrays

// int 
Array<def_size,Type>& operator = (const Array<def_size,Type>& source);

};

#endif // Array_hpp

Lattice类

#ifndef Lattice_h

#define Lattice_h

#include <cstdlib>

#include <vector>

#include "Source.cpp"

using std::vector;

template <class V, class I, int NumberNodes> 

class Lattice

{ // Generic lattice class

private:
// Implement as a full nested Vector class
Array<1,vector<V> > tree;//最开始使用Array<1,vector<V>>时会报错,改为Array<1,vector<V> >就OK了 注意这个空格 ORZ

// Redundant data
I nrows;
// Number of rows
int typ;
// What kind of lattice (number of nodes)

public:
// Constructors & destructor
Lattice();
// Default constructor
Lattice(const I& Nrows);
// Number of rows and branch factor
Lattice(const I& Nrows, const V& val); // + value at nodes
Lattice(Lattice<V, I, NumberNodes>& source);// Copy constructor
virtual ~Lattice();// Destructor

// Iterating in a Lattice; we need forward and backward versions
I MinIndex() const;// Return the minimum row index
I MaxIndex() const;// Return the maximum row index
I Depth() const;// The (depth) number of rows in the lattice

// Operatos
Lattice<V, I, NumberNodes>& operator = (const Lattice<V, I, NumberNodes>& source);
vector<V>& operator [] (const I& nLevel );// Subscripting operator
const vector<V, I>& operator [] (const I& nLevel ) const;// Subscripting operator

// We need the form of the lattice at the 'base' of the pyramid. This
// will be needed when we use backward induction 
vector<V, I> BasePyramidVector() const;
I BasePyramidSize() const;// The number of discrete points at end
I numberNodes() const;// Total number of mesh points

};

#endif

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 C++ MFE 技术