2013年5月27日 星期一

Microsoft DreamSpark

這篇並不是教大家關於編程知識,而是讓未畢業的各位盡快到你所屬的學校申請 Microsoft DreamSpark 的帳號,裏面有大量的程式下載,包括:

Microsoft Visual Studio (用於編寫C++, Visual Basic, 貌似還有Data Mining 相關的 tool pack )
SQL Server (資料庫管理)

比如香港中文大學,網址是:
http://www.cuhk.edu.hk/itsc/compenv/license/dreamspark.html


只要按照指示一步一步註冊就可以申請帳號並下載軟體。

此外,Microsoft 也有提供一些免費的課程讓學生自行學習:


強烈建議大家在未畢業前趕快安裝,不然就錯過機會了。

2013年5月26日 星期日

實分析 (Real Analysis) II

今天的課題是實分析 (Real Analysis)

********************************************************

讀者從上次的遊戲中應該可以看出,一開始所列出的一些規則,其實就是加法與乘法的規則。而我為了避免讀者受到既有思維的影響,將常用的符號完全取代成新的符號,才能夠更幫助理解這個課題。

接下來,我們繼續探討自然數N 所存在的一些特性。與上次一樣,我們不會使用常見的符號:

1. Order Structure

我們假定存在一些符號在╳中:
╳:{α, β, γ, δ, ε, ζ, η, θ, ☻, ☼}

這些符號是有排序的,比如β 在α 的後面;η在θ 的前面。

如果一個符號在另一個之前,我們稱之為 →:η→θ
如果一個符號在另一個之後,我們稱之為 ←:β←α
如果一個符號跟另一個在一樣位置,我們稱之為 =:α = α

O1:
對於任何兩個在X內的符號,下列其中之一必然正確:
1. α = β
2. β←α
3. β→α

意思:兩個符號要不然等於自己,否則必然有先後次序之分。

O2:
對於三個在X內的符號,如果β←α,γ←β,則 γ←α。

意思:符號的次序是有傳遞性的。

O3:
對於任何兩個在X內的符號,如果β←α,則β★γ←α★γ

意思:將★的運算子與→ / ← 連結,意思是將兩邊同時做以★處理並不影響符號的次序。

O4:
對於任何兩個在X內的符號,如果β←α,則β♥γ←α♥γ,如果γ←☻

意思:將♥的運算子與→ / ← 連結,意思是將兩邊同時做以♥處理並不影響符號的次序,如果♥←☻成立的話

O4 似乎並不像我們平時直覺般如此明顯。我們嘗試證明O4。

反證法

假設☻←γ ,且β←α 則 β♥γ←α♥γ ...(*)

因此
☻←γ
☻♥☻←☻♥γ ................(*)
☻←☻............................(2)

因此若☻←γ,則「β←α 則 β♥γ←α♥γ」無法成立。

2. Bounds

Upper Bounds

設¢為╳之中的一個子集。如果於¢中的任意符號α 全部符合
α→ζ,或
α = ζ
那麼符號ζ 就是¢的Upper Bound 。

Lower Bounds

設£於╳之中的一個子集。如果於£中的任意符號β 全部符合
β→ε,或
β = ε
那麼符號ε 就是£的Lower Bound 。

3. Maximum and Minimum

Maximum

設¢為╳之中的一個子集。如果ζ 在¢之中且
α→ζ,或
α = ζ
那麼符號ζ 就是¢的Maximum。

Minimum

設£於╳之中的一個子集。如果ε 在£之中且
β→ε,或
β = ε
那麼符號ε 就是£的Minimum。

到底 (2) 跟 (3) 的分別在哪呢?
在於 Max/Min 的符號需要自己本身都在指定的集中。

4. Supremum and Infimum

Supremum

Least Upper Bound.

Infimum

Greatest Lower Bound.

暫別我們的符號遊戲,我們使用常用的實數線來表達 Supremum, Upper Bound 與Maximum 的分別。如下圖:

留意Maximum 並不存在,因為數線本身並不包含最大的一點 (是一個Open Interval )。

2013年5月18日 星期六

實分析 (Real Analysis) I


今天的課題是實分析 (Real Analysis)

********************************************************

在做實分析以前,我們先玩一個遊戲

我們先定義一些符號:
{α, β, γ, δ, ε, ζ, η, θ, ☻, ☼}

這些符號之間擁有一些奇怪的特性,我們可以使用一個符號★ 表示它們之間的關係

★這個符號,有著以下的特徵:

1. α★β = β★α .....................................................................Commutative
2. (α★β)★γ = α★(β★γ).......................................................Associative
3. 存在著一個符號叫 ☻ 會使得 α★☻ = α
4. 對於每一個 α 來說,我們定義一個符號叫,使得如果 α★β =☻,則 β = α

此外,還存在著另外一個符號叫 ♥

♥這個符號,有著以下的特徵:

5. α♥β = β♥α........................................................................Commutative
6. (α♥β)♥γ = α♥(β♥γ)...........................................................Associative
7.  存在著一個符號叫 ☼ 會使得 α♥☼ = α
8. 對於每一個 α 來說,我們定義一個符號叫♭,使得如果 α♥β =☼,則 β = ♭α

最後,★與♥ 存在著一種關係:

9. (α★β)♥γ = (α♥γ)★(β♥γ)..................................................Distributive

********************************************************

然後,我們就開始玩遊戲嚕:


(a) 證明:☻★☻= ☻

答案:
☻★☻= ☻..........................................................................................(3)

(b) 證明:☻♥ α = ☻

答案:


= (☻♥ α ) ★ ♯(☻♥ α )
= [ (☻★☻)♥ α ] ★ ♯(☻♥ α ) .............................................................(a)
= [ (☻♥ α ) ★ (☻♥ α ) ] ★ ♯(☻♥ α )..................................................(9)
= (☻♥ α ) ★ [ (☻♥ α )  ★ ♯(☻♥ α ) ] ................................................(2)
= (☻♥ α ) ★ ☻....................................................................................(4)
= (☻♥ α )..............................................................................................(3)

(c) 證明:(β)♥β = ♯(β♥β)

答案:

(♯β)♥β
= (♯β)♥β ★ ☻......................................................................................(3)
= (♯β)♥β ★ [ (β♥β) ★ ♯(β♥β) ]............................................................(4)
= [ (♯β)♥β ★ (β♥β) ] ★ ♯(β♥β)............................................................(2)
= { [(♯β)★β] ♥β } ★ ♯(β♥β).................................................................(9)
= (☻♥ β )  ★ ♯(β♥β)............................................................................(4)
= ☻★ ♯(β♥β).......................................................................................(b)
= ♯(β♥β) ★ ☻......................................................................................(1)
= ♯(β♥β)................................................................................................(3)


(d) 證明:(α★β)♥(α★(β)) = (α♥α)★(♯(β♥β))

答案:

(α★β)♥(α★(♯β))
= {α ♥  [α★(♯β)] }★ { β ♥ [α★(♯β)] } ................................................(9)
= { [α★(♯β)] ♥ α }★ { [α★(♯β)] ♥ β }.................................................(5)
= { [α♥α] ★ [α♥(♯β)] } ★ { [α♥β] ★ [(♯β)♥β] }..................................(9)
= [α♥α] ★ { [α♥(♯β)] ★ { [α♥β] ★ [(♯β)♥β)] } }.................................(2)
= [α♥α] ★ { { [α♥(♯β)] ★ [α♥β] } ★ [(♯β)♥β] }..................................(2)
= [α♥α] ★ { { [(♯β)♥α] ★ [β♥α] } ★ [(♯β)♥β] }..................................(5)
= [α♥α] ★ { { [(♯β)★β] ♥ α } ★ [(♯β)♥β] }.........................................(9)
= [α♥α] ★ { { [β★(♯β)] ♥ α } ★ [(♯β)♥β] }.........................................(5)
= [α♥α] ★ { { ☻♥ α } ★ [(♯β)♥β] }....................................................(4)
= [α♥α] ★ { ☻★ [(♯β)♥β] }................................................................(b)
= [α♥α] ★ { [(♯β)♥β] ★☻ }................................................................(1)
= [α♥α] ★ [(♯β)♥β]..............................................................................(3)
= (α♥α)★(♯(β♥β)).................................................................................(c)


********************************************************

好了,聰明的讀者您或許會發現,在做了一堆東西以後,我們所證明的,只不過是:
(x+y)*(x-y) = x^2 - y^2

2013年5月17日 星期五

Use Excel to calculate Hull-White Trinomial Tree

Hull-White Model (赫爾懷特模型)



In financial mathematics, the Hull–White model is a model of future interest rates. In its most generic formulation, it belongs to the class of no-arbitrage models that are able to fit today's term structure of interest rates. It is relatively straightforward to translate the mathematical description of the evolution of future interest rates onto a tree or lattice and so interest rate derivatives such as bermudan swaptions can be valued in the model.

The first Hull–White model was described by John C. Hull and Alan White in 1990. The model is still popular in the market today.

The Hull-White Model satisfy the following stochastic differential equation:



This article use Excel as a tool to generate a Hull-White trinomial tree. Hull-White Trinomial Tree is a lattice model where, at each node, the interest rate can go upward, middle, or downward.

Since Hull-White Model is a mean-reverting model, the interest rate cannot go infinitely large or small. Therefore, if the node goes too far away (more precisely, j > 0.184 / (alpha * delta t) ), the interest rate branches will change shape, as illustrated below:



The generated tree will be looked as follows:




In the Hull-White Model, the following parameters are required before we do our calculations:

1. Term
It is the number of years that we want to generate for our tree
Allowed values: 1~80

2. Step
The step we want to generate for our tree
Allowed values: 1 (yearly), 2 (half-yearly), 4 (quarterly), 12 (monthly)

3. Alpha
A factor that determinate the rate how fast the interest rate reverts to mean
Allowed values: Any

4. Sigma
The volatility of the interest rate
Allowed values: Any

5. Spot Rates
The Zero Coupon Rate as observed at t = 0
The number of Spot Rates required is (Term*Step)

Then, copy the following code in VBA as an user-defined function:


To call the function, type in
=HwTree(Term, Step, Spot_Rates, Alpha, Sigma, Return_Type)
(Ctrl-Shift-Enter)
The amount of rows must be 2*jmax + 1
where jmax is the smallest integer larger than ( Alpha * Step ) / 0.184



Where return type allowed are 1, 2, 3 or 4
1 is the short rate of the Hull-White Model
2 is the Andrew-Debreu Price
3 is the discount price to time zero
4 is the probability of reaching the node

You can download the excel directly at:
https://docs.google.com/file/d/0B5esrphcTfIca3BGWFJDR0JORnM/edit?usp=sharing

References:
Option, Future and other Derivatives (8th Edition) - John C. Hull
http://en.wikipedia.org/wiki/Hull%E2%80%93White_model
http://lombok.demon.co.uk/financialTap/interestrates/hwtrinomialtree
http://janroman.dhis.org/finance/Hull%20&%20White/HWImp.pdf

2013年5月11日 星期六

線性代數 (Linear Algebra) I

今天的課題是線性代數 (Linear Algebra)

********************************************************

向量 (Vector)

向量 (Vector),與矢量 (Scalar) 不同,是有方向性的一條線。

矢量 (Scalar) 描述的就像大小、強度 (Magnitude)。
向量是有方向性的,也有大小的。

基本的向量,比如說:OA = (2,3),描述的就是 2 維空間上的一點。更正確來說,是在笛卡兒座標 (Cartesian Coordinate) 上的「向右走兩步,向上走三步」。

當中的「右、上」說的是方向,「兩、三」是大小。



一般來說,我喜歡將向量寫成



我將其稱呼為「n維的1點」。數學一點的說法是,

相對來說的,



則被理解成「1維的n點」。

********************************************************

矩陣 (Matrices)

矩陣的寫法如下:



矩陣,我將其理解為「n維的m點」。
其意思是,將矩陣上的每一列 (Column) 獨立拆開來看,都是一條「n維的1點」的Vector。因此有 m 列即是「n維的m點」。

********************************************************

線性組合 (Linear Combinations)

線性組合(英語:Linear combination)是一個線性代數中的概念,代表一些抽象的向量各自乘上一個純量後再相加。

以數式表示:



那麼其幾何意義為何?我嘗試用將3 條2 維上的向量相加:



就如下圖所示,a+2b+c 所走的路,最後其實只是等同於 x 這個向量。




********************************************************

線性方程組 (System of Linear Equations)


在數學中,線性方程組是方程組的一種,它符合以下的形式:




或是




以線性組合的方法表示,則是:



因此,在幾何上的意義就是:

有m 個n 維的向量以x1, x2, ...,xm 的權數相加後,最後走出來的 (n維上) 的一點。

所以,如果一個線性方程組是相容 (Consistent) 的:

必定存在 (x1,x2,...,xm) 的解,使得其線性組合可以成為向量b 。

考慮下列線性方程組:



即是,當x vector 存在後:


尋得某些值,如:(1,2,1),使得:

















或是存在多於一個解:


2013年5月6日 星期一

概率空間 (Probability Space) 與 概率測度 (Probability Measure)

概率空間 (Probability Space) 與 概率測度 (Probability Measure)

想要了解上述兩個概念,我們需要從更基本的集合論 (Set Theory) 入手

集合 (Set)

集合,簡單地說,就是一堆東西的總體,其中每個東西稱為這個集合的一個元素。

舉例而言, 定義 Ω:
Ω = { 小明, 小美, 小強 }

如此Ω 則是一個包含了三個元素 (element) 的集合。當中小明,小美,小強均為 Ω 的元素。

子集 (Subset)

子集,為大集合中一部分的集合,故亦稱部分集合。
表示子集的符號為
如  F ⊂ Ω 代表 F 是 Ω 的子集。


舉例而言:
Ω = { 小明, 小美, 小強 }
則 ∅ , { 小明 }, { 小美 }, { 小強 }, { 小明, 小美 }, { 小明, 小強 }, { 小美, 小強 }, { 小明,小美,小強 } 均為 Ω 的子集
如 { 小明, 小強 }  Ω

註:∅ 為 空集 (Null set)

冪集 (Power set)

冪集,就是將集合 Ω 中所有子集組合而成的新的集合。
冪集的寫法為 ℙ(Ω) 

舉例而言:
Ω = { 小明, 小美, 小強 }
ℙ(Ω)  = { ∅ , { 小明 }, { 小美 }, { 小強 }, { 小明, 小美 }, { 小明, 小強 }, { 小美, 小強 }, { 小明,小美,小強 } }

聯集 (Union)

聯集,是將多個集合 Ω , Σ 中分別擁有的所有元素合併,從而構成的新的集合。
聯集的符號為∪



舉例而言:
Ω = { 小明, 小美, 小強 }
Σ = { 一心, 家寶, 小強 }

則 Ω ∪ Σ 為 { 小明, 小美, 小強,  一心, 家寶 }

交集 (Intersection)

交集,是將多個集合 Ω , Σ 中共同擁有的所有元素合併,從而構成的新的集合。
交集的符號為∩



舉例而言:
Ω = { 小明, 小美, 小強 }
Σ = { 一心, 家寶, 小強 }

則 Ω ∩ Σ 為 { 小強 }

差集 (Complement)

差集,是一個集合 Ω 中排除掉令一集合 Σ 中共同擁有的元素後,從而留下的新的集合 (子集) 。
差集的符號為 \

舉例而言:
Ω = { 小明, 小美, 小強 }
Σ = { 一心, 家寶, 小強 }

則 Ω \ Σ 為 { 小明, 小美 }

σ 代數 (Sigma-Algebra)

在數學中,某個集合 Ω 上的σ代數又叫σ域,是 Ω 的冪集的一個子集。
這個子集滿足對於可數個集合的聯集運算和交集運算的封閉性(因此對於交集運算也是封閉的)。

定義:

設 Ω 為一非空集合。若然 F  符合下列條件, F 為 Ω 的 σ 代數

1. Ω  F

2. Σ  ⇒ Σ'  F
3. ∀( F ) ⇒ ∪A = F

Ω , )記號稱為一個可測空間。

概率空間 (Probability Space)

概率空間 (Ω, F, P) 是一個總測度為1的測度空間(即 P(Ω) =1)


第一項 Ω 是一個非空集合,有時稱作「樣本空間」(Sample Space)。Ω 的集合元素稱作「樣本輸出」(outcome),可寫作ω。
第二項 是樣本空間 Ω 的冪集的一個非空子集。 的集合元素稱為事件 Σ。事件 Σ 是樣本空間 Ω 的集族 (class)。集合 必須是一個σ-代數:


( Ω, ) 合起來稱為可測空間。



第三項 稱為概率,或者概率測度 (Probability Measure)。這是一個從集合 到實數域 R 的函數,: F → R。每個事件都被此函數賦予一個機率值。必須是一個測度,且 P(Ω) = 1。

舉例而言,擲銀幣一次,定義正面為H 且反面為T,樣本輸出 ω 為:

{H}
{T}

F = { ∅, {H} , {T} , { H , T } }

P( ω = {H} ) = 0.5
P( ω = {T} ) = 0.5

****************************************************************

因此,擲銀幣三次,在概率測度 P 下,樣本輸出 ω 為:

{HHH}
{HHT}
{HTH}
{THH}
{HTT}
{THT}
{TTH}
{TTT}

F = { ∅, {HHH} , {HHT} , ... , {TTT} , { {HHH} , {HHT} } , ... , { {TTH} , {TTT} } , ... , { {HHH} , {HHT} , {HTH} } , ... , { {HHH}, {HHT}, {HTH} , {THH} , {HTT} , {THT} , {TTH} , {TTT} }

P( ω = {HHH} ) = 1/8
P( ω = {HHT} ) = 1/8
P( ω = {HTH} ) = 1/8
P( ω = {THH} ) = 1/8
P( ω = {HTT} ) = 1/8
P( ω = {THT} ) = 1/8
P( ω = {TTH} ) = 1/8
P( ω = {TTT} ) = 1/8

****************************************************************

若果覺得銀幣是有偏差的,如:擲得正面的機率為 2/3
在概率測度 Q 下,樣本輸出 ω 為:

Q( ω = {HHH} ) = 8/27
Q( ω = {HHT} ) = 4/27
Q( ω = {HTH} ) = 4/27
Q( ω = {THH} ) = 4/27
Q( ω = {HTT} ) = 2/27
Q( ω = {THT} ) = 2/27
Q( ω = {TTH} ) = 2/27
Q( ω = {TTT} ) = 1/27

可見 PQ  為描述 可測空間 ( Ω, ) 的不同概率測度,使用不同的函數去描述同一個可測空間。

References:
http://www.wikipedia.org/
http://www.math.scu.edu.tw/teacher/Chieping/probability.html

2013年4月30日 星期二

Proof of Ito isometry


The previous article has proved the Ornstein–Uhlenbeck process, however, it is not a formal proof since some steps is not clear enough such as "defining dWt as Wt+dt - Wt"

Therefore, I shall post the proof of "Ito isometry" here, which is a formal proof for handling expectation and variance of stochastic integral.

We want to proof the followings:





*****************************************************************

To begin with all, we have to learn the concept of "partition".

Most of the reader should have basic concept about differentiation and integration, in fact, the differential itself is a partition:



where h->0 can be understand as "the difference of x2 and x1 tends to 0"


For the integration



What we are actually doing is:



Now, we replace the ambiguous "h->0" to a more vigorous one

Define the partition P to be:



where i  is between 0 to n and xn equals b (the upper bound of the integration range)

Define "the largest" partition to be:



where sup is "supremum" (we will come back to this in after article, now just think supremum is something like "maximum")

Hence, here we have the following definition of integration:



*****************************************************************

Then, we go back to the proof of Ito isometry:

By definition,



Note that



and it is independent of the function f(t,Z), hence



Therefore,



Moreover, we have



Since E(X) part becomes zero,



Substituting f(t,Z) into X,



Because ti - ti-1 is exactly the differential which equals dt,

We have the Ito isometry:



*****************************************************************

The proof is still not perfectly completed yet because we assume the the expectation sign and limit can interchange. We shall come back to this later.