1/27
Prequel Excel VBA Algorithm Application What’s next?
Excel - Modeling and Algorithm
MFE Preparatory Course 2016
Dr. Yang Ye
Nanyang Business School
June 2016
leafyoung@gmail.com
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
2/27
Prequel Excel VBA Algorithm Application What’s next?
Outline
Prequel
Excel
VBA
Algorithm
Application
What’s next?
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
3/27
Prequel Excel VBA Algorithm Application What’s next?
1. Download GDP and benchmark interest rate for G7
2. Download stock prices from Yahoo finance.
G7
Canada
France
Germany
Italy
Japan
United Kingdom
United States
?
ECB
BOJ
FED
BOE
BOC
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
4/27
Prequel Excel VBA Algorithm Application What’s next?
Some Adv. Function
INDIRECT
INDIRECT(“C3”)
INDIRECT(“C”& “3”)
OFFSET
&
$ and VLOOKUP
RANK and LARGE
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
5/27
Prequel Excel VBA Algorithm Application What’s next?
Statistics Functions
AVERAGE()
VAR()
VAR.P()
STDEV.P()
STDEV.S()
SKEW()
KURT()
Volatility = Stdev.p(return)
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
6/27
Prequel Excel VBA Algorithm Application What’s next?
Solver
Figure:
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
7/27
Prequel Excel VBA Algorithm Application What’s next?
Bond YTM
BondPrice = Cashflow1
(1+yield)1 + Cashflow2
(1+yield)2 + ... + LastCashflow
(1+yield)n
BondPrice = Cashflow ∗
1−( 1
(1+yield)n )
yield + MaturityValue ∗ 1
(1+yield)n
How to calculate bond price from YTM?
How to calculate YTM from bond price?
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
8/27
Prequel Excel VBA Algorithm Application What’s next?
Range and Matrix Operation
Define a range for single or rectangular area
=matNew1
CTRL+SHIFT+ENTER: Apply array operations
=IF(D1:F3>0,SQRT(D1:F3),0)
Matrix functions
MMULT
TRANSPOSE
Use cholesky decomposition
M =
1 ρ
ρ 1
Mc =
1 0
ρ sqrt(1 − ρ2
)
Test Mc Mc
t
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
9/27
Prequel Excel VBA Algorithm Application What’s next?
Generate Random Numbers
Generate random number with normal distribution
=RAND()
=NORMSINV(RAND())
Generate correlated random numbers
r1 r2
c1 c2
c3 c4
= q1 q2
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
10/27
Prequel Excel VBA Algorithm Application What’s next?
Syntax
VBA Syntax
Declaration
Condition
Loop
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
11/27
Prequel Excel VBA Algorithm Application What’s next?
Declare
' This is a comment
Option Explicit ' make it required
Option Base 0 ' Array index starts from 0
Dim a As Integer
a = 1
a = "1.1"
Debug.Print a
Dim b As Double
b = 1.1
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
12/27
Prequel Excel VBA Algorithm Application What’s next?
Declare Array
Dim c1(10) As Double
c1(0) = 1
Dim c2(10, 10) As Double
c2(4, 6) = 1
Dim mat2() As Double
ReDim mat2(2)
mat2(0) = 1
mat2(1) = 2
mat2(2) = 3
ReDim Preserve mat2(3) 'Keep the data
Function assignment()Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
13/27
Prequel Excel VBA Algorithm Application What’s next?
Condition
Dim balance As Integer
balance = 100
If balance > 100 Then Debug.Print ">100"
If balance > 100 Then
Debug.Print ">100"
ElseIf balance > 50 Then
Debug.Print ">50"
Else
Debug.Print "<50"
End If
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
14/27
Prequel Excel VBA Algorithm Application What’s next?
For Loop
Dim i As Integer, j As Integer
For i = 1 To 10 'Step 1
Debug.Print i
Next
For i = 1 To 10 Step 2
Debug.Print i
Next
For i = 1 To 3
For j = 5 To 1 Step -1
Debug.Print i & " " & j
Next
Next
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
15/27
Prequel Excel VBA Algorithm Application What’s next?
For Loop - 2
Dim mat(2, 2) As Double
For i = 0 To 2
For j = 0 To 2
mat(i, j) = i + j
Next
Next
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
16/27
Prequel Excel VBA Algorithm Application What’s next?
While Loop
Dim i As Integer, sum As Integer
i = 1
sum = 0
While i <= 10
sum = sum + i
i = i + 1
Wend
i = 1
sum = 0
Do While i <= 10
sum = sum + i
i = i + 1
Loop
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
17/27
Prequel Excel VBA Algorithm Application What’s next?
Putting things together
What does this do?
Rnd -1
Randomize 5
Dim pf(1 To 10) As Double, i As Integer, sum As Double
For i = 1 To 10
pf(i) = Rnd
Next
i = 1
sum = 0
Do While i <= 10
sum = sum + pf(i)
If sum > 2 Then
Exit Do
End If
i = i + 1
Loop
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
18/27
Prequel Excel VBA Algorithm Application What’s next?
Sub/Function
Sub and Function differ in that Sub doesn’t return a value.
Function Afunction(Input1 As String, Input2 as Integer) as Integer
...
Afunction = 3
End Function
Sub Afunction(Input1 As String, Input2 as Integer)
...
End Sub
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
19/27
Prequel Excel VBA Algorithm Application What’s next?
Algorithm - Problem and Solution
Problem: is to convert between column name to column
number.
A: 1
B: 2
...
Z: 26
AA: ?
...
AZ: ?
?: ?
Solution: ?
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
20/27
Prequel Excel VBA Algorithm Application What’s next?
Column Number Conversion - ToColumnNumber
Function ToColumnNumber(ColumnName As String) As Integer
Dim A_Code As Integer, length As Integer, i As Integer
If Len(ColumnName) = 0 Then
ToColumnNumber = 0
Exit Function
End If
A_Code = Asc("A") - 1
length = Len(ColumnName)
ToColumnNumber = 0
For i = 1 To length
Dim last_char As String
Dim char_value As Integer
last_char = Mid(ColumnName, length - i + 1, 1)
char_value = Asc(last_char) - A_Code
ToColumnNumber = ToColumnNumber + _
(char_value) * (26 ^ (i - 1))
Next
End Function
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
21/27
Prequel Excel VBA Algorithm Application What’s next?
Column Number Conversion - ToColumnName
Function ToColumnName(ByVal ColumnNumber As Integer) As String
Dim t As Integer
Dim A_Code As Integer
A_Code = Asc("A") - 1
ToColumnName = ""
If ColumnNumber > 26 Then
Do While ColumnNumber / 26 > 0 And ColumnNumber > 0
Dim first_value As Integer
Dim first_char As String
first_value = A_Code + ColumnNumber Mod 26
first_char = Chr(first_value)
ToColumnName = first_char & ToColumnName
ColumnNumber = ColumnNumber / 26
Loop
Else
ToColumnName = Chr(A_Code + ColumnNumber)
End If
End Function
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
22/27
Prequel Excel VBA Algorithm Application What’s next?
GBM: Geometric Brownian Motion
Definitions
S: Stock price
K: Strike price of option
r: Risk-free rate of return
T: Time to Expiration in years
σ: Volatility
Geometric Brownian motion
dSt = rStdt + σStdWt
Call Option payoff: max(ST − K, 0)
Put Option payoff: max(0, K − ST )
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
23/27
Prequel Excel VBA Algorithm Application What’s next?
Solving the GBM - Black-Scholes Model
Black-Scholes Model - Making assumptions so we can solve
the equation
Call option: c = N(d1) S − N(d2) Ke−rT
Put Option: p = −N(−d2) Ke−rT
− N(−d1) S
d1 =
ln(S/K)+(r+ σ2
2 )T
σ
√
T
, d2 = d1 − σ
√
T
N(x): Cumulative normal distribution function
Excel:
NORMSDIST(z): = NORM.S.DIST(z,TRUE)
NORMSINV: opposite to NORMSDIST
d 1
=(LN(Stock/Strike)+(drift+sigmaˆ2/2)*Time)/(sigma*SQRT(Time))
d 2 =d 1-B7*SQRT(Time)
Call =spot*NORMSDIST(d 1)-strike*EXP(-
drift*maturity)*NORMSDIST(d 2)
Put =strike*EXP(-drift*maturity)*NORMSDIST(-d 2)-
spot*NORMSDIST(-d 1)
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
24/27
Prequel Excel VBA Algorithm Application What’s next?
Monte Carlo
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
25/27
Prequel Excel VBA Algorithm Application What’s next?
GBM MC
From GBM, dSt = rStdt + σStdWt, St = S0e
(r−σ2
2
)t+σWt
Apply numerical method,
St = S0exp (r − σ2
2 )∆t + σ
√
∆tz , z is the random number
We calculate St for every time step ∆t to reach maturity T.
Apply the payoff function to get the value of the option,
discounted to current t = 0.
For call, V = e−rT
max(ST − strike, 0)
For put, V = e−rT
max(0, strike − ST )
Average of all V1, V2...Vn is the value of the option.
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
26/27
Prequel Excel VBA Algorithm Application What’s next?
VBA
Define Payoff function
=IF(type="call",MAX(F286-strike,0)*EXP(-drift*maturity)
,MAX(0,strike-F286)*EXP(-drift*maturity))
Define loop
Option Explicit
Sub MonteCarloMAcro()
Application.ScreenUpdating = False
Dim i As Long, iterations As Long
Dim CallTotal As Double, CallAvg As Double, PutTotal As Double, PutAvg As Double
CallTotal = 0
CallAvg = 0
PutTotal = 0
PutAvg = 0
iterations = Range("MCIteration").Value
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
27/27
Prequel Excel VBA Algorithm Application What’s next?
VBA - Loop Con’t
For i = 1 To iterations
Calculate
CallTotal = CallTotal + Range("CallValue").Value
PutTotal = PutTotal + Range("PutValue").Value
Next i
CallAvg = CallTotal / i
PutAvg = PutTotal / i
Debug.Print "Call: " & CallAvg
Debug.Print "Put: " & PutAvg
Application.ScreenUpdating = True
End Sub
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm
27/27
Prequel Excel VBA Algorithm Application What’s next?
What’s next?
Try R: http://tryr.codeschool.com/
Must try
Dr. Yang Ye Nanyang Business School
Excel - Modeling and Algorithm

More Related Content

PDF
Online Advance Excel & VBA Training in India
PPTX
Vba Class Level 1
PPTX
Vba part 1
PDF
Excel VBA programming basics
PPTX
Intro to Excel VBA Programming
PPTX
E learning excel vba programming lesson 3
PPTX
E learning excel vba programming lesson 4
PPTX
Introduction to Excel VBA/Macros
Online Advance Excel & VBA Training in India
Vba Class Level 1
Vba part 1
Excel VBA programming basics
Intro to Excel VBA Programming
E learning excel vba programming lesson 3
E learning excel vba programming lesson 4
Introduction to Excel VBA/Macros

What's hot (20)

PPTX
VBA Classes from Chandoo.org - Course Brochure
PPTX
Vba Excel Level 2
PPTX
Vba introduction
PPTX
PPT
AVB201.2 Microsoft Access VBA Module 2
DOCX
Excel vba
PDF
Visual Basics for Application
PPT
AVB201.1 MS Access VBA Module 1
PPTX
Intro To C++ - Class 10 - Control Statements: Part 2
PDF
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
DOCX
Fieldsymbols
PPTX
IOS Swift Language 3rd tutorial
PPTX
IOS Swift language 2nd tutorial
PDF
The swift programming language
PPTX
Java script basic
PPTX
07 java variables
PPT
Creating A User‑Defined Function In Excel Using Vba
PPTX
Understanding excel’s error values
PDF
MA3696 Lecture 9
VBA Classes from Chandoo.org - Course Brochure
Vba Excel Level 2
Vba introduction
AVB201.2 Microsoft Access VBA Module 2
Excel vba
Visual Basics for Application
AVB201.1 MS Access VBA Module 1
Intro To C++ - Class 10 - Control Statements: Part 2
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
Fieldsymbols
IOS Swift Language 3rd tutorial
IOS Swift language 2nd tutorial
The swift programming language
Java script basic
07 java variables
Creating A User‑Defined Function In Excel Using Vba
Understanding excel’s error values
MA3696 Lecture 9
Ad

Viewers also liked (18)

PDF
Sample Employment Verification
PDF
Equifax Verification Services - Auto Lender Infographic
PDF
Sample Income Verification
PDF
Integrating Mortgage Disclosures With Income Verification
PPSX
5 Annoying Excel Pivot Table Problems
PPTX
PPTX
Why Is Excel VBA So Important For Banks ?
PPT
Excel VBA
PDF
Excel vba macro programing
PPTX
Tutorial 6: Multiple Worksheets and Workbooks
PDF
2016 ms excel_bm
PPTX
Tutorial 8: Developing an Excel Application
PPSX
Advanced Excel &Basic Excel Training
PPS
SET THEORY
PPT
Introduction to Digital signatures
PPTX
MS EXCEL PPT PRESENTATION
PDF
Visual Design with Data
PDF
Build Features, Not Apps
Sample Employment Verification
Equifax Verification Services - Auto Lender Infographic
Sample Income Verification
Integrating Mortgage Disclosures With Income Verification
5 Annoying Excel Pivot Table Problems
Why Is Excel VBA So Important For Banks ?
Excel VBA
Excel vba macro programing
Tutorial 6: Multiple Worksheets and Workbooks
2016 ms excel_bm
Tutorial 8: Developing an Excel Application
Advanced Excel &Basic Excel Training
SET THEORY
Introduction to Digital signatures
MS EXCEL PPT PRESENTATION
Visual Design with Data
Build Features, Not Apps
Ad

Similar to 2016 Excel/VBA Notes (20)

PDF
Dsp lab _eec-652__vi_sem_18012013
PDF
Dsp lab _eec-652__vi_sem_18012013
PDF
Ms excel 2016_function
PDF
การบ้านวิชาห้องสมุด
DOCX
B61301007 matlab documentation
PPTX
Use of Excel Spreadsheets in Computing Grades
PPTX
Mechanical Engineering Homework Help
PDF
Programming for Problem Solving
PPT
Matlab introduction
PPTX
an introductory to excel training doc.pptx
PDF
Chap 5 c++
PPTX
CS103-12.pptx on Excel for basic formula and functions
PDF
A MATLAB project on LCR circuits
DOCX
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
PPT
Operators and Expressions in C++
PPTX
1. Ch_1 SL_1_Intro to Matlab.pptx
PDF
Scilab as a calculator
PDF
Matlab for marketing people
PDF
A complete introduction on matlab and matlab's projects
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Ms excel 2016_function
การบ้านวิชาห้องสมุด
B61301007 matlab documentation
Use of Excel Spreadsheets in Computing Grades
Mechanical Engineering Homework Help
Programming for Problem Solving
Matlab introduction
an introductory to excel training doc.pptx
Chap 5 c++
CS103-12.pptx on Excel for basic formula and functions
A MATLAB project on LCR circuits
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
Operators and Expressions in C++
1. Ch_1 SL_1_Intro to Matlab.pptx
Scilab as a calculator
Matlab for marketing people
A complete introduction on matlab and matlab's projects

Recently uploaded (20)

PPTX
Hushh Hackathon for IIT Bombay: Create your very own Agents
PDF
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
PPTX
Business_Capability_Map_Collection__pptx
PPTX
The Data Security Envisioning Workshop provides a summary of an organization...
PPTX
Phase1_final PPTuwhefoegfohwfoiehfoegg.pptx
PPTX
chuitkarjhanbijunsdivndsijvndiucbhsaxnmzsicvjsd
PDF
©️ 02_SKU Automatic SW Robotics for Microsoft PC.pdf
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PPTX
CHAPTER-2-THE-ACCOUNTING-PROCESS-2-4.pptx
PPTX
IMPACT OF LANDSLIDE.....................
PPTX
Hushh.ai: Your Personal Data, Your Business
PPTX
865628565-Pertemuan-2-chapter-03-NUMERICAL-MEASURES.pptx
PDF
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
PDF
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
PPTX
cp-and-safeguarding-training-2018-2019-mmfv2-230818062456-767bc1a7.pptx
PPT
expt-design-lecture-12 hghhgfggjhjd (1).ppt
PPTX
AI AND ML PROPOSAL PRESENTATION MUST.pptx
PPTX
Lesson-01intheselfoflifeofthekennyrogersoftheunderstandoftheunderstanded
PDF
The Role of Pathology AI in Translational Cancer Research and Education
PPTX
Crypto_Trading_Beginners.pptxxxxxxxxxxxxxx
Hushh Hackathon for IIT Bombay: Create your very own Agents
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
Business_Capability_Map_Collection__pptx
The Data Security Envisioning Workshop provides a summary of an organization...
Phase1_final PPTuwhefoegfohwfoiehfoegg.pptx
chuitkarjhanbijunsdivndsijvndiucbhsaxnmzsicvjsd
©️ 02_SKU Automatic SW Robotics for Microsoft PC.pdf
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
CHAPTER-2-THE-ACCOUNTING-PROCESS-2-4.pptx
IMPACT OF LANDSLIDE.....................
Hushh.ai: Your Personal Data, Your Business
865628565-Pertemuan-2-chapter-03-NUMERICAL-MEASURES.pptx
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
cp-and-safeguarding-training-2018-2019-mmfv2-230818062456-767bc1a7.pptx
expt-design-lecture-12 hghhgfggjhjd (1).ppt
AI AND ML PROPOSAL PRESENTATION MUST.pptx
Lesson-01intheselfoflifeofthekennyrogersoftheunderstandoftheunderstanded
The Role of Pathology AI in Translational Cancer Research and Education
Crypto_Trading_Beginners.pptxxxxxxxxxxxxxx

2016 Excel/VBA Notes

  • 1. 1/27 Prequel Excel VBA Algorithm Application What’s next? Excel - Modeling and Algorithm MFE Preparatory Course 2016 Dr. Yang Ye Nanyang Business School June 2016 [email protected] Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 2/27 Prequel Excel VBA Algorithm Application What’s next? Outline Prequel Excel VBA Algorithm Application What’s next? Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 3/27 Prequel Excel VBA Algorithm Application What’s next? 1. Download GDP and benchmark interest rate for G7 2. Download stock prices from Yahoo finance. G7 Canada France Germany Italy Japan United Kingdom United States ? ECB BOJ FED BOE BOC Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 4/27 Prequel Excel VBA Algorithm Application What’s next? Some Adv. Function INDIRECT INDIRECT(“C3”) INDIRECT(“C”& “3”) OFFSET & $ and VLOOKUP RANK and LARGE Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm
  • 2. 5/27 Prequel Excel VBA Algorithm Application What’s next? Statistics Functions AVERAGE() VAR() VAR.P() STDEV.P() STDEV.S() SKEW() KURT() Volatility = Stdev.p(return) Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 6/27 Prequel Excel VBA Algorithm Application What’s next? Solver Figure: Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 7/27 Prequel Excel VBA Algorithm Application What’s next? Bond YTM BondPrice = Cashflow1 (1+yield)1 + Cashflow2 (1+yield)2 + ... + LastCashflow (1+yield)n BondPrice = Cashflow ∗ 1−( 1 (1+yield)n ) yield + MaturityValue ∗ 1 (1+yield)n How to calculate bond price from YTM? How to calculate YTM from bond price? Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 8/27 Prequel Excel VBA Algorithm Application What’s next? Range and Matrix Operation Define a range for single or rectangular area =matNew1 CTRL+SHIFT+ENTER: Apply array operations =IF(D1:F3>0,SQRT(D1:F3),0) Matrix functions MMULT TRANSPOSE Use cholesky decomposition M = 1 ρ ρ 1 Mc = 1 0 ρ sqrt(1 − ρ2 ) Test Mc Mc t Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm
  • 3. 9/27 Prequel Excel VBA Algorithm Application What’s next? Generate Random Numbers Generate random number with normal distribution =RAND() =NORMSINV(RAND()) Generate correlated random numbers r1 r2 c1 c2 c3 c4 = q1 q2 Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 10/27 Prequel Excel VBA Algorithm Application What’s next? Syntax VBA Syntax Declaration Condition Loop Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 11/27 Prequel Excel VBA Algorithm Application What’s next? Declare ' This is a comment Option Explicit ' make it required Option Base 0 ' Array index starts from 0 Dim a As Integer a = 1 a = "1.1" Debug.Print a Dim b As Double b = 1.1 Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 12/27 Prequel Excel VBA Algorithm Application What’s next? Declare Array Dim c1(10) As Double c1(0) = 1 Dim c2(10, 10) As Double c2(4, 6) = 1 Dim mat2() As Double ReDim mat2(2) mat2(0) = 1 mat2(1) = 2 mat2(2) = 3 ReDim Preserve mat2(3) 'Keep the data Function assignment()Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm
  • 4. 13/27 Prequel Excel VBA Algorithm Application What’s next? Condition Dim balance As Integer balance = 100 If balance > 100 Then Debug.Print ">100" If balance > 100 Then Debug.Print ">100" ElseIf balance > 50 Then Debug.Print ">50" Else Debug.Print "<50" End If Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 14/27 Prequel Excel VBA Algorithm Application What’s next? For Loop Dim i As Integer, j As Integer For i = 1 To 10 'Step 1 Debug.Print i Next For i = 1 To 10 Step 2 Debug.Print i Next For i = 1 To 3 For j = 5 To 1 Step -1 Debug.Print i & " " & j Next Next Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 15/27 Prequel Excel VBA Algorithm Application What’s next? For Loop - 2 Dim mat(2, 2) As Double For i = 0 To 2 For j = 0 To 2 mat(i, j) = i + j Next Next Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 16/27 Prequel Excel VBA Algorithm Application What’s next? While Loop Dim i As Integer, sum As Integer i = 1 sum = 0 While i <= 10 sum = sum + i i = i + 1 Wend i = 1 sum = 0 Do While i <= 10 sum = sum + i i = i + 1 Loop Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm
  • 5. 17/27 Prequel Excel VBA Algorithm Application What’s next? Putting things together What does this do? Rnd -1 Randomize 5 Dim pf(1 To 10) As Double, i As Integer, sum As Double For i = 1 To 10 pf(i) = Rnd Next i = 1 sum = 0 Do While i <= 10 sum = sum + pf(i) If sum > 2 Then Exit Do End If i = i + 1 Loop Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 18/27 Prequel Excel VBA Algorithm Application What’s next? Sub/Function Sub and Function differ in that Sub doesn’t return a value. Function Afunction(Input1 As String, Input2 as Integer) as Integer ... Afunction = 3 End Function Sub Afunction(Input1 As String, Input2 as Integer) ... End Sub Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 19/27 Prequel Excel VBA Algorithm Application What’s next? Algorithm - Problem and Solution Problem: is to convert between column name to column number. A: 1 B: 2 ... Z: 26 AA: ? ... AZ: ? ?: ? Solution: ? Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 20/27 Prequel Excel VBA Algorithm Application What’s next? Column Number Conversion - ToColumnNumber Function ToColumnNumber(ColumnName As String) As Integer Dim A_Code As Integer, length As Integer, i As Integer If Len(ColumnName) = 0 Then ToColumnNumber = 0 Exit Function End If A_Code = Asc("A") - 1 length = Len(ColumnName) ToColumnNumber = 0 For i = 1 To length Dim last_char As String Dim char_value As Integer last_char = Mid(ColumnName, length - i + 1, 1) char_value = Asc(last_char) - A_Code ToColumnNumber = ToColumnNumber + _ (char_value) * (26 ^ (i - 1)) Next End Function Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm
  • 6. 21/27 Prequel Excel VBA Algorithm Application What’s next? Column Number Conversion - ToColumnName Function ToColumnName(ByVal ColumnNumber As Integer) As String Dim t As Integer Dim A_Code As Integer A_Code = Asc("A") - 1 ToColumnName = "" If ColumnNumber > 26 Then Do While ColumnNumber / 26 > 0 And ColumnNumber > 0 Dim first_value As Integer Dim first_char As String first_value = A_Code + ColumnNumber Mod 26 first_char = Chr(first_value) ToColumnName = first_char & ToColumnName ColumnNumber = ColumnNumber / 26 Loop Else ToColumnName = Chr(A_Code + ColumnNumber) End If End Function Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 22/27 Prequel Excel VBA Algorithm Application What’s next? GBM: Geometric Brownian Motion Definitions S: Stock price K: Strike price of option r: Risk-free rate of return T: Time to Expiration in years σ: Volatility Geometric Brownian motion dSt = rStdt + σStdWt Call Option payoff: max(ST − K, 0) Put Option payoff: max(0, K − ST ) Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 23/27 Prequel Excel VBA Algorithm Application What’s next? Solving the GBM - Black-Scholes Model Black-Scholes Model - Making assumptions so we can solve the equation Call option: c = N(d1) S − N(d2) Ke−rT Put Option: p = −N(−d2) Ke−rT − N(−d1) S d1 = ln(S/K)+(r+ σ2 2 )T σ √ T , d2 = d1 − σ √ T N(x): Cumulative normal distribution function Excel: NORMSDIST(z): = NORM.S.DIST(z,TRUE) NORMSINV: opposite to NORMSDIST d 1 =(LN(Stock/Strike)+(drift+sigmaˆ2/2)*Time)/(sigma*SQRT(Time)) d 2 =d 1-B7*SQRT(Time) Call =spot*NORMSDIST(d 1)-strike*EXP(- drift*maturity)*NORMSDIST(d 2) Put =strike*EXP(-drift*maturity)*NORMSDIST(-d 2)- spot*NORMSDIST(-d 1) Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 24/27 Prequel Excel VBA Algorithm Application What’s next? Monte Carlo Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm
  • 7. 25/27 Prequel Excel VBA Algorithm Application What’s next? GBM MC From GBM, dSt = rStdt + σStdWt, St = S0e (r−σ2 2 )t+σWt Apply numerical method, St = S0exp (r − σ2 2 )∆t + σ √ ∆tz , z is the random number We calculate St for every time step ∆t to reach maturity T. Apply the payoff function to get the value of the option, discounted to current t = 0. For call, V = e−rT max(ST − strike, 0) For put, V = e−rT max(0, strike − ST ) Average of all V1, V2...Vn is the value of the option. Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 26/27 Prequel Excel VBA Algorithm Application What’s next? VBA Define Payoff function =IF(type="call",MAX(F286-strike,0)*EXP(-drift*maturity) ,MAX(0,strike-F286)*EXP(-drift*maturity)) Define loop Option Explicit Sub MonteCarloMAcro() Application.ScreenUpdating = False Dim i As Long, iterations As Long Dim CallTotal As Double, CallAvg As Double, PutTotal As Double, PutAvg As Double CallTotal = 0 CallAvg = 0 PutTotal = 0 PutAvg = 0 iterations = Range("MCIteration").Value Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 27/27 Prequel Excel VBA Algorithm Application What’s next? VBA - Loop Con’t For i = 1 To iterations Calculate CallTotal = CallTotal + Range("CallValue").Value PutTotal = PutTotal + Range("PutValue").Value Next i CallAvg = CallTotal / i PutAvg = PutTotal / i Debug.Print "Call: " & CallAvg Debug.Print "Put: " & PutAvg Application.ScreenUpdating = True End Sub Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm 27/27 Prequel Excel VBA Algorithm Application What’s next? What’s next? Try R: http://tryr.codeschool.com/ Must try Dr. Yang Ye Nanyang Business School Excel - Modeling and Algorithm