Showing posts with label financial modelling. Show all posts
Showing posts with label financial modelling. Show all posts

Saturday, January 16, 2010

Monte Carlo Simulation

http://www.anthony-vba.kefra.com/vba/vba12.htm

What is a Monte Carlo Simulation? Well, think about it as a computation process that utilized random numbers to derive an outcome(s). So instead of having fixed inputs, probability distributions are assigned to some or all of the inputs. This will generate a probability
distribution for the output after the simulation is ran.

Here is an example. A firm that sells product X under a pure/perfect competition market* wants to know the probability distribution for the profit of this product and the probability that the firm will loss money when marketing it.

The equation for the profit is: TP = TR - TC = (Q*P) - (Q*VC+FC)

Assumption:

* The Quantity Demanded (Q) flucturates between 8,000 and 12,000 units and is uniformly distributed.
* Variable Cost (VC) is normally distributed (with mean = 7, Sd = 2) truncated on both sides (with a minimum of 7 / 2 and a maximum of 10).
* Market Price (P) is normally distributed (with mean = 10, Sd = 3) truncated on the left-hand side (with a minimum of 1).
* Fixed Cost (FC) is $5,000.

Also See Log-Normal Probability Distribution

* Under perfect competition market, the firm does not have the influence to affect the price of this product - the firm takes the market price as a given, dP/dQ = 0.

Outcome:
The average profit for this investment is $29,546 as shown on cells G25 after 50,000 iteration is ran. The probability that the profit of the investment turns out to be negative (loss money) is 22.28% as shown on cell C24. The probability distribution of the profit > X is display on column F and G. For example, there is 65% of chance that the profit will be greater than $12,481. The probability distribution is quite normal as shown on the figure. The mean is also very close to the median. This is due to the probability distribution that we assigned to the variables.




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

************************************************************************************************
Option Explicit
Option Base 1

'**********************************************************************************
'* Run Monte Carlo Simulation *
'**********************************************************************************
Sub MonteCarlo()

Dim Iteration As Long, i As Long
Dim Q As Double, P As Double, TR As Double
Dim VC As Double, FC As Double, TC As Double
Dim SdVC As Double, MeanVC As Double, SdP As Double, MeanP As Double
Dim MinQ As Double, MaxQ As Double, AverageTP As Double, SumTP As Double
Dim ProfitX As Double, CountNo As Double
Iteration = Range("C3").Value
FC = Range("C7").Value
MinQ = Range("C13").Value
MaxQ = Range("C14").Value
MeanVC = Range("C15").Value
SdVC = Range("C16").Value
MeanP = Range("C17").Value
SdP = Range("C18").Value
ProfitX = Range("B24").Value

ReDim TP(Iteration) As Double

SumTP = 0
CountNo = 0
For i = 1 To Iteration: Cells(12, 3) = i
VC = Truncate_Normal_VC(MeanVC, SdVC, MeanVC / 2, MeanP)
P = Truncate_Normal_P(MeanP, SdP, 1)
Q = Int((MaxQ - MinQ + 1) * Rnd + MinQ)
TC = FC + VC * Q
TR = P * Q
TP(i) = TR - TC

'Comment out the following will make the simulation run faster
Cells(5, 3) = Q
Cells(6, 3) = P
Cells(8, 3) = VC
Cells(9, 3) = TC
Cells(10, 3) = TR
Cells(11, 3) = TP(i)


If TP(i) > ProfitX Then CountNo = CountNo + 1
SumTP = SumTP + TP(i)
Next i
AverageTP = SumTP / Iteration
Cells(25, 7) = AverageTP
Cells(24, 3) = 1 - CountNo / Iteration
Call Sort(Iteration, TP)
Call Hist(Iteration, 40, TP(1), TP(Iteration), TP)
For i = 1 To 20
Cells(i + 3, 6) = 1 - (0.05 * i)
Cells(i + 3, 7) = TP(Int(Iteration / 20 * i))
Next i
Cells(3, 6) = "Close to 100%"
Cells(13, 6) = "Median = 50%"
Cells(23, 6) = "Close to 0%"
Cells(3, 7) = TP(1)
End Sub

'**********************************************************************************
'* Return random numbers from a Truncated Normal Distribution for VC *
'**********************************************************************************
Function Truncate_Normal_VC(MeanX, SdX, leftLimit, RightLimit)
Dim x As Double
Dim fac As Double, r As Double, V1 As Double, V2 As Double
5 x = gauss * SdX + MeanX
If RightLimit < x Or x < leftLimit Then GoTo 5
Truncate_Normal_VC = x
End Function

'**********************************************************************************
'* Return random numbers from a Truncated Normal Distribution for Price *
'**********************************************************************************
Function Truncate_Normal_P(MeanX, SdX, leftLimit)
Dim x As Double
Dim fac As Double, r As Double, V1 As Double, V2 As Double
5 x = gauss * SdX + MeanX
If x < leftLimit Then GoTo 5
Truncate_Normal_P = x
End Function

'**********************************************************************************
'* Return random numbers from Standard Normal Distribution *
'**********************************************************************************
Function gauss()
Dim fac As Double, r As Double, V1 As Double, V2 As Double
10 V1 = 2 * Rnd - 1
V2 = 2 * Rnd - 1
r = V1 ^ 2 + V2 ^ 2
If (r >= 1) Then GoTo 10
fac = Sqr(-2 * Log(r) / r)
gauss = V2 * fac
End Function

'***********************************************************************************
'* Sort the numbers *
'***********************************************************************************
Sub Sort(n As Variant, arr() As Double)
Dim Temp As Double
Dim i As Long
Dim j As Long
For j = 2 To n
Temp = arr(j)
For i = j - 1 To 1 Step -1
If (arr(i) <= Temp) Then GoTo 10
arr(i + 1) = arr(i)
Next i
i = 0
10 arr(i + 1) = Temp
Next j
End Sub

'**********************************************************************************
'* Construct Historgram Distribution *
'**********************************************************************************
Sub Hist(n As Variant, M As Long, Start As Double, Right As Double, arr() As Double)
Dim i As Long, j As Long, Find As Long
Dim Length As Double
ReDim breaks(M) As Single
ReDim freq(M) As Single

For i = 1 To M
freq(i) = 0
Next i

Length = (Right - Start) / M

For i = 1 To M
breaks(i) = Start + Length * i
Next i

For i = 1 To n
If (arr(i) <= breaks(1)) Then freq(1) = freq(1) + 1
If (arr(i) >= breaks(M - 1)) Then freq(M) = freq(M) + 1
For j = 2 To M - 1
If (arr(i) > breaks(j - 1) And arr(i) <= breaks(j)) Then freq(j) = freq(j) + 1
Next j
Next i

For i = 1 To M
Cells(i + 1, 9) = breaks(i)
Cells(i + 1, 10) = freq(i)
Next i
End Sub

Thursday, December 31, 2009

finacial modelling using spreadsheet

introduction to modelling tools:
spreadsheet features and functions.
using excel fuctions such as Array functions, refernce functions, financial fuctions, data validation, analysis toolpak
Business forecasting: Time series analysis
using moving average,regression,anova
building a valuation model (includes constructing financial statemetns)
handling business uncertainity
risk analysis using montecarlo simulation
scenario building
checking model for acuracy and assuring data protection
introduction to visual basic

Financial Modeling Using Excel and VBA
Chandan Sengupta

Softcover, 672 pages + bonus CD-ROM
ISBN 0471267686
978-0471267683
Wiley
February 2004
(click below for the best currently available price for this important resource)



Financial modeling is finance in action. It is both challenging and rewarding. Financial Modeling Using Excel and VBA shows you how you can both enjoy the challenge and reap the rewards.

Financial modeling is an essential skill for all financial professionals - and Excel and its built-in programming language, Visual Basic for Applications (VBA), are the preferred tools for the job. Financial Modeling Using Excel and VBA, designed for self-study, classroom use, and reference, presents a comprehensive approach to developing both simple and sophisticated financial models in all major areas of finance using both Excel and VBA.

The book assumes only basic knowledge of finance and Excel, and no previous knowledge of VBA. From that base, it teaches financial modeling, VBA, and advanced features of Excel using a unique, simple approach that is based on the author’s twenty years of experience in financial modeling in the business world and the classroom. The book reviews all the necessary financial theory and concepts, and walks you through a wide range of real-world financial models–over seventy-five of them–that you can imitate and use for practice as well.

The included companion CD-ROM includes several useful modeling tools and full working versions of all the models discussed in the book, so you can be more productive. You may view an excerpt of this book. This book and CD-ROM set provide the following instructions, models and resources:

Introduction to Financial Modeling.
Excel for Financial Modeling
Excel Basics.
Advanced Excel Features.
Excel’s Built-In Functions and Analysis Tools.
Financial Modeling Using Excel
How to Build Good Excel Models.
Financial Statements Forecasting.
Time Value of Money.
Financial Planning and Investments.
Analyzing Market History.
Bond Pricing and Duration.
Simulating Stock Prices.
Options and Option Portfolios.
Binomial Option Pricing.
VBA for Financial Modeling
Introduction to VBA.
VBA Essentials.
Sub and Function Procedures.
Debugging VBA Models.
Financial Modeling Using VBA
How to Build Good VBA Models.
Time Value of Money.
Financial Planning and Investments.
Analyzing Market History.
Simulating Stock Prices.
Options and Option Portfolios.
Binomial Option Pricing.
Appendix A: Keyboard Shortcuts for Excel.
Appendix B: VBA Quick Reference.
Appendix C: Excel and VBA Built-In Functions.
"Excel and VBA have vast capabilities, yet until this book, users were on their own to figure out just how to use these tools for both single and complex financial problems. Financial Modeling fills the gaps with practical guidance to modeling a wide range of finance problems." –Tim Koller, Partner, McKinsey & Company, Inc.

CHANDAN SENGUPTA teaches at the Fordham University Graduate School of Business Administration, specializing in Financial Modeling, Business Valuation, Corporate Finance, and Investment Management. Formerly, he was vice president at the Chase Manhattan Bank for eight years and senior financial advisor for Mobil Corporation for ten years. He is the author of The Only Proven Road to Investment Success, also published by Wiley.

(information from the publisher)

Day One
Learn how to efficiently use VBA to build, debug and analyse financial models

Overview of core VBA skills

* The Visual Basic Environment
* Common elements of the VBA syntax
* Generating your own code versus recording a macro
* Programming standards
* Application: Navigating a model using VBA
* User Defined Functions ('UDF')

How to test and debug VBA applications

* Break Points
* Immediate Window
* Watch Window
* Stack
* Application : Debug a client s Project Finance VBA code

Coding VBA Building blocks

* Difference between sub/ function/ macro
* Interacting with an Excel spreadsheet
* Different types of Looping Operations
* Explanation of Option Explicit
* Application: Solving a circular reference using a cut & paste macro.

Day Two
Applications in financial models

Application session

* Automated Sensitivity and Scenario analysis using VBA
* Sculpting a Debt Repayment to a Target Ratio
* Debt sizing / Debt sculpting optimisation
* Automated execution of Macros
* Presentation applications
* Disclaimer Coding
* Security discussion on passwords and protected/ hidden sheets
* Tariff break-even to a target IRR

Advanced Applications

* Automation controlling ALL of Windows other components
* Explode your Graphs exceptional presentation functionality
* Error Handling what is it and how should you be using it?
*********************************************************************
Course Content

The programme consists of the following 2 modules:

Part I: Excel VBA Fundamentals

Participants will learn the fundamentals of Excel VBA and prepare themselves to create simple financial models in Part II. Small programs will be used to illustrate the underlying programming concepts.

The VBA programming environment
Variables and constants
Macros
Built-in VBA functions and operators
User-defined functions
Branching and looping
Object-based concept: Objects, methods and properties
The Range object
Debugging
The following additional topics may also be covered if time allows:
Arrays
Procedures
Custom dialog boxes
Workbook and worksheet events
Chart objects
Part II: Financial Modeling

Participants will apply VBA programming concepts and skills to build simple yet real-world financial models. The techniques of writing good models will also be discussed. As the scope of financial modeling is huge, topics have to be selective. The following shows a list of possible topics. Actual coverage depends somewhat on the finance background of the class.

Loan amortization
a. Simple model
b. Iteration: The bisection approach
c. Allowance for changing interest rates or repayment period
Financial planning
a. Retirement planning
b. Portfolio restructuring
Market history analysis
a. Normal and real growth of stocks
b. Growth of investment in different assets
Stock simulation
a. Stock volatility estimation
b. The geometric Brownian motion and the log-normal distribution
c. Stock price simulation
Option pricing
a. Different option strategies
b. The Black-Scholes option pricing model
c. Implied volatility estimation
d. Options portfolio
e. The binomial model