Wednesday, January 30, 2013

VBA: Numerical Methods

The best way that I can learn VB is by practice and just get on with it. I never realized that starting something and ending something is harder than the in between process. Good thing it is object-oriented and somehow close to Java. They are almost alike. But not all. Sobrang nangapa ako sa codes and shortcuts. It's been a very long time since I did some programming.

VBA Shorcuts
  • Alt-F11 - Activate VBE
  • F5 - Execute Module
The formulas are assumed to be correct. If I put on another char like $y$ in the formula, there will be an error. I can't seem to fix this. Here are the codes:

Colorit.bas


Something Fun. Coloring the highlighted Red. Just learning on the buttons.

Sub Red()
'
' Red Macro
'
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

Sub NoFill()
'
' NoFill Macro
'
'
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

Bisection.bas

This contain some of the functions found in the other methods. i didn't include it anymore in the other methods.


Sub Bisectiontest()
    
    'ASSUME THAT THE FORMULA IS CORRECT
    '0nly after 1000 iterations of f(x) = 0
    
    form = Range("D4")
    a = Range("E6").Value
    b = Range("E7").Value
     
    'checks the inputs
    If Inputx(form, 0, a, b) = False Then
        MsgBox "Please check your inputs"
        Exit Sub
    'wrong property of bisection
    ElseIf Px(form, a) * Px(form, b) > 0 Then
        MsgBox "Please change your interval"
        Exit Sub
    Else
        'applying the method
        Call Bisection(form, a, b)
    End If
    
End Sub

Public Function Inputx(form, form2, a, b) As Boolean
    'Missing input
    If form = "" Or form2 = "" Or a = "" Or b = "" Then
        Inputx = False
    'wrong input on interval
    ElseIf a Like "[A-Z,a-z]" Or b Like "[A-Z,a-z]" Then
        Inputx = False
    Else
        Inputx = True
    End If
    
End Function

Public Function Px(f, x)
    'This is to evaluate the x variable only
    
    'replacing x with the value
    f1 = Application.WorksheetFunction.Substitute(f, "x", x)
    
    'evaluate the string
    Px = Evaluate(f1)
    
End Function

Private Sub Bisection(form, a, b)
    
    Static counter As Integer
    xn = (a + b) / 2
    fa = Px(form, a)
    fxn = Px(form, xn)
    fb = Px(form, b)
    
    'stop with 1000 iteration
    counter = counter + 1
    If counter = 1000 Then
       Range("F9").Value = xn
        Exit Sub
    Else
    
    'checker for next iteration
    'assigning new a or b
    
        If fa = 0 Then
            Range("F9").Value = a
        ElseIf fxn = 0 Then
            Range("F9").Value = xn
        ElseIf fb = 0 Then
            Range("F9").Value = b
        ElseIf fa * fxn < 0 Then
            b1 = xn
            Call Bisection(form, a, b1)
        Else
            a1 = xn
            Call Bisection(form, a1, b)
        End If
    End If
End Sub

Newton.bas


Sub Newtontest()
    'ASSUME THAT THE FORMULAS inputed are CORRECT
    '0nly after 1000 iterations of f(x) = 0
    
    form = Range("E5")
    form2 = Range("E6")
    a = Range("F8").Value
     
    'checks the inputs
    If Inputx(form, form2, a, 0) = False Then
        MsgBox "Please check your inputs"
        Exit Sub
    'property of newton
    ElseIf Px(form2, a) = 0 Then
        MsgBox "Please change your x. The first derivative is 0."
        Exit Sub
    Else
        'applying the method
        Call Newton(form, form2, a)
    End If
End Sub

Private Sub Newton(form, form2, a)
    
    Static counter As Integer
    
    f = Px(form, a)
    fprime = Px(form2, a)
    
    'stop with 1000 iteration
    counter = counter + 1
    If Not counter = 1000 Then
        'checker for next iteration
        'assigning new xn
        If f = 0 Then
            Range("G10").Value = a
        Else
            xn = a - (f / fprime)
            Call Newton(form, form2, xn)
        End If
    Else
        Range("G10").Value = a
        Exit Sub
    End If
End Sub

Secant.bas


Sub Secanttest()
    'ASSUME THAT THE FORMULAS inputed are CORRECT
    '0nly after 1000 iterations of f(x) = 0
    
    form = Range("E5")
    x1 = Range("F7").Value
    x2 = Range("F8").Value
    
    'checks the inputs
    If Inputx(form, 0, x1, x2) = False Then
        MsgBox "Please check your inputs"
        Exit Sub
    'property of secant
    ElseIf Px(form, x1) = Px(form, x2) Then
        MsgBox "f(x1) = f(x2). Please change your points"
        Exit Sub
    Else
        'applying the method
        Call Secant(form, x1, x2)
    End If
End Sub

Private Sub Secant(form, x1, x2)
    
    Static counter As Integer
    
    f1 = Px(form, x1)
    f2 = Px(form, x2)
    
    'stop with 1000 iteration
    counter = counter + 1
    If Not counter = 1000 Then
        'checker for next iteration
        'assigning new xn
        If f1 = 0 Then
            Range("G10").Value = x1
        ElseIf f2 = 0 Then
            Range("G10").Value = x2
        Else
            xn = x2 - f2 * ((x2 - x1) / (f2 - f1))
            Call Secant(form, x2, xn)
        End If
    Else
        Range("G10").Value = x2
        Exit Sub
    End If
End Sub

Fixedpoint.bas


Sub Fixedpointtest()
    'ASSUME THAT THE FORMULAS inputed are CORRECT
    '0nly after 1000 iterations of f(x) = 0
    
    form = Range("E5")
    form2 = Range("E6")
    a = Range("F8").Value
     
    'checks the inputs
    If Inputx(form, form2, a, 0) = False Then
        MsgBox "Please check your inputs"
        Exit Sub
    Else
        'applying the method
        Call Fixedpoint(form, form2, a)
    End If
End Sub

Private Sub Fixedpoint(form, form2, a)
    
    Static counter As Integer
    
    f = Px(form, a)
    g = Px(form2, a)
    
    'stop with 1000 iteration
    counter = counter + 1
    If Not counter = 1000 Then
        'checker for next iteration
        'assigning new xn
        If f = g Then
            Range("G10").Value = a
        Else
            xn = g
            Call Fixedpoint(form, form2, xn)
        End If
    Else
        Range("G10").Value = a
        Exit Sub
    End If
End Sub

I realized how i missed programming. Its so much different that recording it into a macro. Although that helps but you can't define a function or apply my own kinda style in programming. Maybe a mixture of the two can help me this time.

Tuesday, January 29, 2013

Matrices in OLS II

The Technicals

In the book Quantitative Methods in Finance by Watsham and Parramore and in class, it tells us how in empirical data, the series of $x$'s or the matrix with $x$ variables are usually not square, thus not invertible. We just have to work on what was given which are  $\hat{x}$ and  $\hat{y}$.
That's why we have to transform \[ \hat{b} = \hat{x}^{-1}\hat{y} \] into
\[ \hat{b} = (\hat{x}^T \hat{x})^{-1} \hat{x}^T \hat{y}\]
To transform it, identity is used and so is $\hat{x}^T$ as can be seen below.  This is to satisfy my curiosity about the identity.
\[ \begin{array}{rl} b = & x^{-1}y & \\
= & x^{-1}I_n y \\
= & x^{-1}(x x^{-1})^T y \\
= & x^{-1}(x^{-1})^T x^T y \\
= & (x^T x)^{-1} (x^T y)
 \end{array} \]
I looked for other ways to simplify the equation but so far, this is the easiest with the given data.

Application Found

  • Regression Analysis (of course) - The measurement of change in one variable (y)  that is the result of changes in other variables (x) . Regression analysis is used frequently in identifying the variables that affect a certain stock's price.
  • Hedging or Hedge Ratio (211-212 book)


Monday, January 28, 2013

Asset Allocation Issues

My group researched and made this PowerPoint. It is for the class and serves as a review on Asset Allocation module.



Modern Portfolio Theory Slides

This is to review for the Modern Portfolio Theory. It is made by our group in TSP class.


Also read Paul Willmotts FAQ's on Modern Portfolio Theory. He's simply amazing at explaining materials. The article is on the the pages area for reference sake. Oh, I still have to review on the Matrices on MPT. Add it to the list of things to do.

OLS Macro I


Code lies on matrixOLS.xlsm

Sub olsmaker()
' this solves for regression calling on fixed number of dataset
' Also matrix form is defined by the user.

    'calling for x and y
    x = Range("A105:B114").Value
    y = Range("C105:C114").Value
    
    'solving for b=(xT x)^-1 (xT y)
    xT = Application.WorksheetFunction.Transpose(x)
    xTx = Application.WorksheetFunction.MMult(xT, x)
    xTxinv = Application.WorksheetFunction.MInverse(xTx)
    xTxinvxT = Application.WorksheetFunction.MMult(xTxinv, xT)
    sol = Application.WorksheetFunction.MMult(xTxinvxT, y)

    'returning it back to the sheet
    Range("F106") = "b="
    Range("F107:F108").Value = sol

End Sub

Notes:
1. How do I make it dynamic? Update this program.
2. Review on Matrix and OLS. Work on how did this happened. And is there a way to shorten the form?
 \[ \hat{b} = \hat{x}^{-1}\hat{y} \to \hat{b} = (\hat{x}^T \hat{x})^{-1} \hat{x}^T \hat{y} \]
3. Very interested in programming numerical methods. Add it to the List to program.
4. Read the book: VB for Dummies.

Matrices in OLS

Matrix Multiplication

If $A$ and $B$ is $3$ x $3$ matrix, then $AB$ is also a $3$ x $3$ matrix. However $AB \neq BA$ because matrix multiplication is not commutative.

As an illustration: matrixOLS.xlsm

\[ A =  \begin{bmatrix}
1 & 2 &3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
\;\;\;\;\;
B =  \begin{bmatrix}
11 & 12 &13 \\
14 & 15 & 16 \\
17 & 18 & 19
\end{bmatrix}  \]
Then,
\[ \begin{split} AB &= { \begin{bmatrix}
1*11+2*14+3*17 & 1*12+2*15+3*18 & 1*13+2*16+3*19 \\
4*11+5*14+6*17 & 4*12+5*15+6*18 & 4*13+5*16+6*19 \\
7*11+8*14+9*17 & 7*12+8*15+9*15 & 7*13+8*16+9*19
\end{bmatrix} }\\
&=
\begin{bmatrix}
90 & 96 &102 \\
216 & 231 & 246 \\
342 & 366 & 390
\end{bmatrix}
\end{split} \]
and
\[ \begin{split} BA &= { \begin{bmatrix}
11*1+12*4+13*7 & 11*2+12*5+13*8 & 11*3+12*6+13*9 \\
14*1+15*4+16*7 & 14*2+15*5+16*8 & 14*3+15*6+16*9 \\
17*1+18*4+19*7 & 17*2+18*5+19*8 & 17*3+18*6+19*9
\end{bmatrix} }\\
&=
\begin{bmatrix}
150 & 186 &222 \\
186 & 231 & 276 \\
222 & 276 & 330
\end{bmatrix}
\end{split} \]

In Excel, matrix multiplication is easier.
1. Select a $n$ x $n$ where you will put your answer.
2. Use the function =MMULT(1st array, 2nd array) and
3. Type ctrl+shift+enter.

Matrices in OLS: 

 Sample1: on matrixOLS.xlsm

Given: \[ \begin{split} 12&=7a-5b \\
2&=7+9b\end{split}\]
Find: $b$

There are many ways to solve for b. Though the solution we made involves the theoretical concepts on matrices.


Given:
\[ \begin{split} y_1&=a+bx_1 \\
y_2&=a+bx_2\end{split}\]
then in matrix form:
\[ \hat{b} = \begin{bmatrix} a\\b \end{bmatrix} \;\;\;\;\;\;
\hat{y} = \begin{bmatrix} y_1\\y_2 \end{bmatrix} \;\;\;\;\;
\hat{x} = \begin{bmatrix} 1 & x_1\\ 1 & x_2 \end{bmatrix}
\]
and
\[ \hat{y} = \hat{x}\hat{b} \to \hat{b} = \hat{x}^{-1}\hat{y} \]


These are the steps applied to the example:

1. The linear equation was converted into its matrix form.
2. We got $\hat{x}^{-1}$ by using Excel's function =MINVERSE(array)
3. The formula $\hat{b} = \hat{x}^{-1}\hat{y}$ is then applied using the Excel's function =MMULT(array).



Sample2:  matrixOLS.xlsm

Given the following dataset, find $b$.



Solution 1: Matrix

Sample 2 involves a series of $x$'s and $y$'s. If that is the case, solving for $b$ would involve some transformation. 

\[ \hat{b} = \hat{x}^{-1}\hat{y} \to \hat{b} = (\hat{x}^T \hat{x})^{-1} \hat{x}^T \hat{y} \]

The image below shows the solution for Sample2.



Solution 2: Graphing

1. Select the data to regress. $x$ must be on the first column followed by the $y$.
2. Click on "Insert" Ribbon -> "Charts" -> "Scatter" -> "Scatter with only Markers".
3. Right click on one of the data points of the Graph.
4. Select "Add Trendline" or "Format Trendline"
5. Check on the "Display Equation on Graph." The slope is $b$ and the y-intercept is $a$.




Bivariate Case (OLS)

Given
\[ x = x_1,x_2,\cdots, x_n   \,\,\ \text{and}  \,\,\,  y = y_1,y_2,\cdots, y_n\]

We define the optimal minimum or the optimized mininum as:
\[ \text{Opt Min F} = \sum{[y_\text{actual}-f(x)]^2}\]
where
\[ \text{line  } y = f(x)= a +bx\]
Substituting the line $f(x)$ to the optimized minimum $F$, we have
\[ F = \sum{[y-a -bx)]^2}\]
Taking the first derivative with respect to $a$ and $b$,
\[ \begin{split} \frac{\partial F}{\partial a} &= 2 \sum{(y-a-bx)(-1)} = 0\\
\frac{\partial F}{\partial b} &= 2 \sum{(y-a-bx)(-x)} = 0 \end{split}  \]
We can now derive the normal equation for the OLS as:

\[ \begin{split} \sum{y} &= an + b \sum{x}\\
\sum{xy} &= a \sum{x} + b \sum{x^2} \end{split} \]
Solve for $a$ and $b$ to get critical points and find out if they really produced the optimum.

Intro to Visual Basic

Enabling Macros in Excel.
1.  Choose File -> Excel Options -> Trust Center -> Trust Center Settings -> Macro Settings
2. Click "Enable all Macros"
3. Click "Ok" button.

Showing Developer Tab in Excel.
1.  Choose File -> Excel Options -> Popular
2. Check the "Show Developer Tab in the Ribbon"
3. Click "Ok" button.

First Code - VB Intro.xlsm


Sub dummy()
 '
 ' dummy Macro
 ' just messing around

 ' assigning variables
    x = Range("a5").Value
    fx = (x ^ 2 - 10 * x + 9)
    fprime = x * 2 - 10

 'formula for newton method
    nextiterate = x - fx / fprime

 'assigning b1 for next iterate
    Range("b5").Value = nextiterate

End Sub

Notes:
1. If we click on the "Macros" Button on the ribbon, we can create, edit, delete and run the macro.
2. If we click on the "Record" button in the ribbon and do some stuff in Excel, it records what you were actually doing for future reference. Make sure to click "Stop" once we're done.
2. If we click on the "Visual Basic" button on the ribbon. The entire Visual basic program will run.

Visual Basic Program
1. On the upper right corner shows the "Project - VBA Project" which shows the workbook's sheets and macro.
2. On the lower right corner shows properties  the active sheets.

Secant Method Exercise

This exercise is also on numericalmethods.xlsx

Given:  $ f(x) = x^2-10x+9$, find the roots using the secant method.

Computation:

1. We choose two points for $x_{-1}$ and $x_{0}$.
2. Solve for the next points using the formula: \[  x_n = x_{n-1} - f(x_{n-1}) \frac{x_{n-1}-x_{n-2}}{f(x_{n-1})-f(x_{n-1})}   \]

This formula is derived from getting the slope of the secant lines through the points $(x_n,f(x_n))$, $(x_{n-1},f(x_{n-1}))$ and  $(x_{n-2},f(x_{n-2}))$.

\[  \frac{f(x_n) - f(x_{n-1})}{x_n - x_{n-1}} = \frac{f(x_{n-1}) - f(x_{n-2})}{x_{n-1} - x_{n-2}} \]

Since $f(x_n)=0$, then
\[ \frac{0 - f(x_{n-1})}{x_n - x_{n-1}} = \frac{f(x_{n-1}) - f(x_{n-2})}{x_{n-1} - x_{n-2}} \]

Solve for $x_n.$

\[ \begin{split}
- f(x_{n-1})(x_{n-1} - x_{n-2})      &= (x_n - x_{n-1})( f(x_{n-1}) - f(x_{n-2})) \\
\frac{- f(x_{n-1})(x_{n-1} - x_{n-2})}{ f(x_{n-1}) - f(x_{n-2})}    &=  x_n - x_{n-1} \\
 x_{n-1} - \frac{ f(x_{n-1})(x_{n-1} - x_{n-2})}{ f(x_{n-1}) - f(x_{n-2})}    &=  x_n \\
 x_n &=  x_{n-1} - \frac{ f(x_{n-1})(x_{n-1} - x_{n-2})}{ f(x_{n-1}) - f(x_{n-2})} \end{split}\]

3. Continue to iterate until $f(x) = 0$ or we've applied the stopping rules.

This is my answer.





I also applied secant method on $ f(x) = cos(x) - x$ and arrived at this result.



So far in excel it works. How about applying these methods in Visual Basic? Will it work? What can be my possible problems?

Newton, Fixed Point and Secant Method

Newton Method

This method is based on the Taylor polynomial series. It uses the tangent line to find the next point ($x$) until its $f(x)$ approaches zero.

Given the \[ P(x_0) = \frac{f(x_0)}{0!} + \frac{f'(x_0)(x-x_0)}{1!} + \cdots\]
Equate it 0 and solve for $x$.
\[ \begin{split} \frac{f(x_0)}{0!} + \frac{f'(x_0)(x-x_0)}{1!} + \cdots &= 0\\
f(x_0) &= -f'(x_0)(x-x_0) \\
x-x_0 &= \frac{f(x_0)}{-f'(x_0)}\\
x&=x_0 - \frac{f(x_0)}{f'(x_0)} \end{split} \]

Example: As solved in the numericalmethods.xlsx

Given $x^2-10x+9$ Find a root of the equation using the starting points: 15,-1000 and 5.

We first solve for the derivative of  $x^2-10x+9$ which is $2x-10$. And then we look for the next $x$'s until its $f(x)$ is zero or we followed our stopping rules.





Problem arises if the first derivative of the function is 0 and we must have a good intuition at the starting point.

Secant Method: Assignment

Compute for the zeroes of $x^2-10x+9$ using the Secant Method. Show how you derive at the iterative process.

This method uses finite differences instead of differential calculus. We estimate for the first derivative as "rise over run" and there is a need for three points this time to secure succeeding estimate.

Fixed Point Method

We plug in $x=g(x)$.

Example:

Given $f(x)=x^2-10x+9$, we first find the $g(x)$ by equating $f(x)=0$. And then isolate $x$.

\[ \begin{split} x^2-10x+9 &=0\\
10x &= x^2+9\\
x&= \frac{x^2}{10}+\frac{9}{10} \end{split} \]

We don't know how many iteration it will end but end it when $x=g(x)$ or when they are approximately the same.

Saturday, January 26, 2013

Basic Numerical Methods: Self Review

I actually found a good book for the Numerical Methods topic. It explains the concepts easily and this is what I summarized. The pictures were taken from the same book Introductory Numerical Analysis by Aquino-Ruivivar and Cureg.

Bisection Method. Basically what the Bisection method does, is to get its midpoint of the $x$'s such that the $f(x)$'s is approaching 0. It continues to iterate until the $f(x)=0$ or you tell it to stop using the valid stopping rules.


Here's what to remember.

1. Given the interval $[a,b]$, $f(a)f(b)<0$. That means there is a sign change in the $f(x)$'s.
2. The next point which is denoted by \[ x_1=\frac{a+b}{2} \]
3. We replace the value of $a$ or $b$ depending on the sign of $f(x_1)$.
4. Continue doing so, until $f(x)$ is close to zero, or then again used the stopping rules.

As excel reference, we can use this:


Regula Falsi. This method on the other hand, chooses the next point to be the one that passes through the x -axis. As shown on the picture below, it continues to iterate until the f(x) is closer to zero. Again it will stop because of the stopping rules.

Here's what to remember.

1. Given the interval $[a,b]$, $f(a)f(b)<0$. That means there is a sign change in the $f(x)$'s. $b
2. The next points which is denoted by \[ x_n=b_{n-1}-\frac{f(b_{n-1})(b_{n-1}-a_{n-1})}{f(b_{n-1}-f(a_{n-1}} \]
3. Continue doing so, until $f(x)$ is close to zero, or then again used the stopping rules.


As excel reference, we can use this:


Fixed Point Method. I got a little confused about this method. All I know is that we choose a $g(x)$ from the original $f(x)$ and then iterate until $g(x)=x$. For reference, the convergence and divergence of fixed point method is as follows:


Newton's Method. In the Newton's Method, it uses the slope of a point or the tangent line to get the next point to iterate. The $f(x)$ converges faster to zero than the other methods. However, there are some restrictions. 

Restrictions:
1. If the point we choice is not a tangent line, it will create an infinite loop.
2. If $f(x)=0$, then the next iterate will fly into infinity.

Here's a good representation of the Newton's method.



As excel reference, we can use the table below. We can get the next point by using this formula: \[x_n = x_{n-1}- \frac{f(x_{n-1})}{f'(x_{n-1})}\]


Secant Method. This method is like the Regula Falsi method. Only the sign change in the $f(x)$ doesn't matter. It is also like the Newton's Method, only that this method uses two points. For representation, look at the figure below. The $x_{-1}$ and $x_0$ will be your given points. It computes for the next point which is $x_1$ . Then we obtain the next point $x_2$ by connecting the points $x_0$ and $x_1$ to make a line. It goes on an on until $f(x)$ converges to 0.



Given $x_{-1}$ and $x_0$, to compute for the next points, the formula is as follows:
\[  x_n = x_{n-1} - f(x_{n-1}) \frac{x_{n-1}-x_{n-2}}{f(x_{n-1})-f(x_{n-1})}   \]


As excel reference, we can use the table below:




Friday, January 25, 2013

Bisection Method


Root Finding

This the reliance of finding the roots/zeroes.

Optimization

Given a polynomial $f(x)$, these are the following steps to determine the minimum or maximum points.

1. Solve for the first derivative of  $f(x)$.
2. Equate $f(x)=0$ and solve for the critical points.
3. Solve for the second derivative of $f(x)$.
4. If $f''(x)=0$, then there is an inflection.
    If $f''(x)>0$, then the critical point is a minimum. (concave up)
    If $f''(x)<0$, then the critical point is a maximum. (concave down)

Finding for the zeroes

Given $f(x)=x^2-10x+9$, we can solve for the zeroes by factoring. Hence,
 \[ \begin{split} x^2-10x+9 &= 0 \\
 (x-1)(x-9) &= 0 \\
x & =1,9 \end{split} \]
There are other ways to find the zero using the quadratic formula, completing the square, graphing, etc. But in real life, nobody knows what the values of $x$. And if we are given polynomials that are non linear or difficult to find the zeroes, we can use the numerical methods such as the bisection methods. Bisection Method

Assumptions:
1. Function is continuous.
2. There is a certain point of time that it passes through the horizontal line.

Method:
1. Choose two points $x_1$ and $x_2$. Solve for $f(x_1)$ and f(x_2)$.
2. If one of $f(x) is 0, we stop. If not, solve for its midpoint $md = \frac{x_1+x_2}{2}$ and its $f(md)$.
3. If $f(md)=0$, we stop. If not, replace one of the two points $x_1$ and $x_2$ with $md$ of the same sign.
4. Continue steps 2 and 3 and stop only when f(x)=0.

Example:  Can be seen on numericalmethods.xlsx

Given $f(x)=x^2-10x+9$, use the bisection method to get one of the points.


Core issues:

If you have bad starting point, when will you stop?
1. if answer keeps on repeating (logic of the rule), or no change in iterate
2. trivial answer: $f(x)=0$

Valid stopping rules:
3. $|x_n-x_n-1|<absolute error$
4. limit number or iterations
5. percentage change of midpoints(or the relative absolute error) is small enough.

Taylor Polynomial

I always forget this basic series. So recall:
\[  P(x)=\frac{f(x_0)(x-x_0)^0}{0!} + \frac{f'(x_0)(x-x_0)^1}{1!} + \frac{f''(x_0)(x-x_0)^2}{2!}+ \cdots \]
Sample Application: Please see numericalmethods.xls
\[ \begin{split} ln(1.10) &= ln(1+0.10) \\ & \Rightarrow ln(1+x), \,\,\  x=0.10 \end{split}\]
\[  \begin{split}
 ln(1+x) &= \int {\frac {1}{1+x}dx} \\
&= \int{1-x+x^2-x^3+x^4-\cdots} \\
&= x - \frac{x^2}{2}+\frac{x^3}{3}-\frac{x^4}{4}+\frac{x^5}{5}-\cdots
\end{split} \]
If $x=0.10$, then
\[  \begin{split}
g(x)&=ln(1+x)\\
&= x - \frac{x^2}{2}+\frac{x^3}{3}-\frac{x^4}{4}+\frac{x^5}{5}-\cdots \\
&= 0.10 - \frac{0.10^2}{2}+\frac{0.10^3}{3}-\frac{0.10^4}{4}+\frac{0.10^5}{5}-\cdots
&= 0.09531
\end{split} \]
And as you involve higher polynomials, you will arrive at higher or exact answers.

Errors in Computation II


As I recall from one of my textbooks, Introductory Numerical Analysis by Aquino-Ruvivar and Cureg, this is how a prof of mine defined the types of error.

Errors that arises during computation:

Input error - error inherent in the source data. usually occur in experimental data or in the encoding stage or formulation of the mathematical computation.

Round-off error - same definition as above. It occurs because of the limited capacity of the computer.

Truncation error - errors that are due to the specific algorithm or formula that is used in a particular numerical method. Example of it is evaluating a specific number using Taylor Series. It is usually truncated at the first few terms of the series.

The prof also discussed that sizes of the error are actually generated when particular methods are used somewhat differ from the expected amount of errors. It is usually expressed in two forms:

Absolute Error: $|x-\tilde{x}|$

Relative Error:  $\frac{|x-\tilde{x}|}{|x|}$

Relative error, though is the better measure than the absolute error.

Errors in Computations

This is the preliminary in Basic Numerical Methods.

Computer and Errors

There are many types of error. In class, we discussed four.

Round-off error - errors made when rounding off numbers or truncating numbers.

 Example:

Given: 2.54674  
Computer rounding it off to the nearest hundreds: 2.55

Propagation - error usually occur in software programming, a normal error
                     - error within an error, what we call the butterfly effect

Example:    Given:  The machine can take in two numbers to the right of the decimal places.
                              $f(x)=x^2+7x, x=0.12$

Manual Solution:
$ f(0.12)=(0.12)^2+7(0.12)=0.0144+0.84=0.8544$
Machine's Solution:
$f(0.12)=(0.12)^2+7(0.12)=0.014+0.84=0.85$

Underflow and Overflow - too much error in the exponents, if the number is too small or to big.

CF Challenges

Since I am supposed to make a "personal diary" detailing insights learned, etc. on a class of mine, I suppose a blog will do. Although, I am supposed to be making this two weeks ago. I have to recall everything..anyway starting of...

Computational Finance Challenges

1. ACCURACY - exactness of the answer

2. EFFICIENCY - the less resources, the more efficient

3. STABILITY - similar results for similar data. usually are not because of constraints

Remember the three at all times. :-)