Pages

Monday, December 26, 2011

Ordinary Differential Equations Part I


Ordinary equations finding its wide application in engineering, applied mathematics , finance and biological science has always been on the frontier of mathematics. At a higher level ODE can be divided into two classes
  1. First order Differential equations
  2. Higher Order Differential equations
First order ODE are the one where equation consist of first level derivative of equation. To solve these equations we need initial value of the Y to get final value at some defined value of X.Following are the basic methods used to solve first order ODEs -

  1. Euler method
  2. Predictor corrector method
  3. Runge Kutta method
In all above methods we divide the path into n interval and then reach next value of Y in steps.Next value of Y is defined by Taylor series.

Taylor series -


Example -

Let us look at Euler method, in this we divide difference between intial value of X and final value of X into n intervals, calling it h.

Y' = Y+X Initial values Y(1)=2, Find value of Y(2)

Now x_initial = 1 and x_final = 2 , h = x_final-x_initial/n
at n =5 h= .2

Y(1.2) = Y(1)+ (0.2)*(Y(1)+X(1)) = 2.6
Y(1.4) = Y(1.2) + (0.2)*(Y(1.2) + X(1.2)) = 2.6+ .2* (2.6 + 1.2) = 3.36
and continue.

C++ Code for Euler method

I have developed code for the implementation of Euler method in c++, Please find it below (hope commenting is enough for understanding)

#include // for normal_distribution
using boost::math::normal; // typedef provides default type is double.

#include
using std::cout; using std::endl; using std::left; using std::showpoint; using std::noshowpoint;
#include
using std::setw; using std::setprecision;
#include
using std::numeric_limits;
#include
#include


using namespace std;
int main()
{
float y_initial,y_final;
float x_initial,x_final;
float derv_y,derv_x,c_const;
float count,h;
int i;
//open file to store results
std::ofstream myfile;
myfile.open("plota.data");

// warning to enter 0 if variable has no value
cout<<"\n Enter 0 if variable has no valuee"<
// Get all the X and Y values
cout << "\n Enter intial value of X and Y respectively"<
cin>>x_initial>>y_initial;
//Taking final value of X where we need to calculate Y
cout << "\n Final value of X"<
cin>>x_final;
// Get Count of interval
cout<<"\n Number of Interval Required"<
cin>>count;
// Coeffciient of derivative equations
cout<<" \n Enter the coefficient of Y, X and constant of derivative"<
cin>>derv_y>>derv_x>>c_const;
//Calculating intervals
h= ( x_final - x_initial ) / count;
//Loop to find values
for(i =0;i
{
y_final = y_initial + h * (derv_y*y_initial+derv_x*x_initial+c_const);
y_initial = y_final;
x_initial = x_initial+h;

myfile<
}


return 0;

}

Output of the code-


Enter 0 if variable has no valuee

Enter intial value of X and Y respectively
1
2

Final value of X
2

Number of Interval Required
10
Enter the coefficient of Y, X and constant of derivative
1
1
0

The result is stored in "plota.data" file and can plotted using GNU plot, Run following commands

gnuplot> set terminal postscript
Terminal type set to 'postscript'
Options are 'landscape noenhanced defaultplex \
leveldefault monochrome colortext \
dashed dashlength 1.0 linewidth 1.0 butt noclip \
palfuncparam 2000,0.003 \
"Helvetica" 14 '
gnuplot> set output "ODE.ps"
gnuplot> plot "plota.data"

result looks like below


Tuesday, December 20, 2011

C++ code Hypothesis Testing


#include // for normal_distribution
using boost::math::normal; // typedef provides default type is double.

#include
using std::cout; using std::endl; using std::left; using std::showpoint; using std::noshowpoint;
#include
using std::setw; using std::setprecision;
#include
using std::numeric_limits;
#include

using namespace std;
int main()
{

int mean,std,pvalue;
int testchoice,conf;
int z=0;
cout << "\n\n\n Hypothesis Testing - Using Normal Distribution";

try
{
{
int precision = 17; // traditional tables are only computed to much lower precision.

normal s; // (default mean = zero, and standard deviation = unity)
cout.precision(5);
cout.precision(6); // default
cout <<"\n Enter the mean value of the Sample"<
cin>>mean;
cout << "\n Enter Std Deviation for Sample"<
cin>>std;
z=0;
z = mean/std;

cout<<" \n Please use the following menu to Enter the hypothesis you want to test"<
cout<<"\n 1. to validate if population mean is equal to sample mean"
"\n 2. to test if population mean is less than sample mean"
"\n 3. to find if population mean is more than sample mean"<
cin>>testchoice;
cout<<" Enter the confidence interval you want to test for"<
cout<< "\n 1. for 90% confidence interval"
"\n 2. for 95% confidence interval"
"\n 3. for 99% confidence interval"<
cin>>conf;
if( testchoice == 1 && conf ==1 )
{
if (pvalue < .05 or pvalue > .95)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 1 && conf ==2 )
{
if (pvalue < .10)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 1 && conf ==3 )
{ if (pvalue > .90)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
if( testchoice == 2 && conf ==1 )
{
if (pvalue < .025 or pvalue > .975)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 2 && conf ==2 )
{
if (pvalue < .05 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 2 && conf ==3 )
{
if(pvalue > .95 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
if( testchoice == 3 && conf ==1 )
{
if (pvalue < .005 or pvalue > .995)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 3 && conf ==2 )
{
if (pvalue < .01)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 3 && conf ==3 )
{
if (pvalue > .01 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}

cout<


}
}

catch(const std::exception& e)
{
std::cout <<
"\n""Message from thrown exception was:\n " << e.what() << std::endl;
}
return 0;

}

Sunday, December 11, 2011

Hypothesis Testing advanced

I have written basic article - http://quantfinanceindia.blogspot.com/2011/06/distributions-hypothesis-testing.html

Hypothesis testing is used to test the validity of data at certain confidence interval and then to use that to make decisions. There is step wise procedure to d othe hypothesis testing , below are the main steps-
  1. Establish the decision creteria and choose null hypothesis. (Ho)
  2. Decision criteria is usually taken as alternative hypothesis (H1)
  3. Calculate Z scor using the sample data provided.
  4. Make decision using sample z score and test type (can be one tail or two tail)
  5. Share your finding
Examples
1. Two tail test
At edible sweet packaging unit, If i want to test that mean weight of population is equal to 500g then i will use two tail sample test. First establish the hypothesis
Ho Mean of Population is 500g
H1 Mean is not equal to 500g
At 99% confidence interval we will find out p value for the test by calculating z score which nothing but sample mean/std deviation
In case P value is .07, then we will say there is not enough evidence that mean of population is different from 500g and we cannot reject null hypothesis
In case P value is .001 then we will say at 99% there is enough evidence to reject null hypothesis and accept the alternative hypothesis that mean is different from 500g.
Obvious Fact – Mean need to be significantly different from the population mean value for us to establish that it falls in the rejection region.

2. One tail test
In a manufacturing company if there is 3% error in every product they manufacture, to find out if the mean error of the population is less than 3% we will use one tail test..
Ho Mean is 3%
H1 Mean is Less than 3%
This will be left tail test where we will take sample mean and standard deviation to find z score for the distribution and then calculate p value.
In case P value is .78 then we can say there is not enough evidence to reject null hypothesis and we cannot reject null hypothesis
In case P value is .002 then we can reject the null hypothesis that mean is 3% and we can say the mean is less than 3%.
Obvious fact –  Sample mean need to significantly lower than population mean for it to fall in rejection region and have low Z-score value.

Implementatino in C++ :

In case you need help to compile programs in UNIX take a look at my guide on running C++ codes in UNIX machine in g++.@ http://quantfinanceindia.blogspot.com/2010/01/unix-for-quants.html
Copy of the code

#include // for normal_distribution
using boost::math::normal; // typedef provides default type is double.

#include
using std::cout; using std::endl; using std::left; using std::showpoint; using std::noshowpoint;
#include
using std::setw; using std::setprecision;
#include
using std::numeric_limits;
#include

using namespace std;
int main()
{

int mean,std,pvalue;
int testchoice,conf;
int z=0;
cout << "\n\n\n Hypothesis Testing - Using Normal Distribution";

try
{
{
int precision = 17; // traditional tables are only computed to much lower precision.

normal s; // (default mean = zero, and standard deviation = unity)
cout.precision(5);
cout.precision(6); // default

cout <<"\n Enter the mean value of the Sample"<
cin>>mean;

cout << "\n Enter Std Deviation for Sample"<
cin>>std;
z=0;
z = mean/std;


cout<<" \n Please use the following menu to Enter the hypothesis you want to test"<
cout<<"\n 1. to validate if population mean is equal to sample mean"
"\n 2. to test if population mean is less than sample mean"

"\n 3. to find if population mean is more than sample mean"<
cin>>testchoice;

cout<<" Enter the confidence interval you want to test for"<
cout<< "\n 1. for 90% confidence interval"
"\n 2. for 95% confidence interval"

"\n 3. for 99% confidence interval"<
cin>>conf;
if( testchoice == 1 && conf ==1 )
{
if (pvalue < .05 or pvalue > .95)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 1 && conf ==2 )
{
if (pvalue < .10)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 1 && conf ==3 )
{ if (pvalue > .90)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
if( testchoice == 2 && conf ==1 )
{
if (pvalue < .025 or pvalue > .975)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 2 && conf ==2 )
{
if (pvalue < .05 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 2 && conf ==3 )
{
if(pvalue > .95 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
if( testchoice == 3 && conf ==1 )
{
if (pvalue < .005 or pvalue > .995)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 3 && conf ==2 )
{
if (pvalue < .01)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 3 && conf ==3 )
{
if (pvalue > .01 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}


cout<


}
}

catch(const std::exception& e)
{
std::cout <<
"\n""Message from thrown exception was:\n " << e.what() << std::endl;
}
return 0;

}

Boost
It is c++ library for the implementation of many utilities, we will be utilizing mathematical distribution library for calculation of p value. Installation of boos can be done following below steps -
    2. unzip the file in /usr/include/ directort using cp or tar commands
    3. Go inside the folder and run ./bootstrap.sh
    4. /b2 install
working of the code
1. Code will ask you the mean of the sample you want to use and then the standard deviation of the sample
2. Next ask will be the test you want to conduct, are you looking of one tail or two tail test (equality of the mean or less than or greater than relation)
3. Confidence interval you want to choose
4. Result will shown as if Null Hypothesis is accepted or not and what is the result.
Code is stored [at] -
Output of the c++ code for hypothesis testing is
Hypothesis Testing - Using Normal Distribution
Enter the mean value of the Sample
3

Enter Std Deviation for Sample
3
Please use the following menu to Enter the hypothesis you want to test

1. to validate if population mean is equal to sample mean
2. to test if population mean is less than sample mean
3. to find if population mean is more than sample mean
1
Enter the confidence interval you want to test for

1. for 90% confidence interval
2. for 95% confidence interval
3. for 99% confidence interval
1
Null Hypothesis not rejected as there is not enough statistical evidence

Thursday, December 1, 2011

Credit Metrics - Part II

Credit metrics as discussed in part I is dependent on the transition matrix for the result.

We will take two bond portfolio for the analysis and will use bivaraite normal distribution to combine both the distribution and to get resultant distribution for the portfolio. We will follow below stepwise approach for the calculations-

1. Take down the probability of transition for each of the portfolio

2. Calculate inverse for each of the number using normsinv(number) formula in excel.

Refer to image 1

3. Combine numbers using bivar formula in excel and using correlation. In this case bivar(A_number,B_Number,Rho).

A_Number and B_Number are inverted numbers from the portfolios.

Rho is the correlation between two portfolios - .20 in this case

4. Calculate this number for all the combinations.

5. You will get a chart like shown in the attached image (image 2), this chart represent cumulative probability of transition.

6. Take down the number corresponding to 5%number, it will be our VaR and answer in this case was (-) 6. refer to yellow box in image 3


Credit Metrics - Part I


Credit Metrics was developed by JP Morgan to simplify the credit risk calculation and is widely used across industry for its simplicity.

In credit metrics, credit rating transition matrix is used as representation of credit worthiness of the firm. Credit transition matrix will tell us the probability of transfer of asset from one rating class to other. At each rating class asset value will change depending upon the discount prevalent in the market for those particular ratings.

Probability of credit rating migration and change in value can be used to find the probability for given amount of loss in portfolio or amount of loss for given portfolio at given probability. (2nd part is nothing but definition of VaR)

Advantages of using credit ratings in calculations is that it take care of small fluctuations in the ratings while other models include only default in their calculations.

We will first take one bond portfolio example to find out VaR. For the calculation I have taken following assumptions-

  • Credit rating transition matrix is self made and does not represent any financial firm or company.
  • Valuation at each credit rating is assumed (In real world forward rate curve data should be used to find out the correct pricing)

Step wise calculation is mentioned below. Please refer to the sheet ,It will be easy to follow below steps –

  1. Convert individual probability data to commutative distribution by adding all the probabilities below that – for example commutative probability of loss at BBB will be combining BBB and all the below that. (this will represent that there is X probability that loss will be either value at BBB or more than)
  2. For VaR calculations take down the value of Bond at the VaR point. 5% VaR point will be 95 and VaR will be
VaR point value – Actual Value

Result for 5% VaR will be 95-103 => (-) 8 , similarly result for 1% VaR will be (-)43.
Above result will help to set aside economic capital to bear any future fluctuations in the future.


Saturday, September 3, 2011

Merton model & credit KMV


Credit risk modeling revolves around study to find out probability of default to consider credit risk that a firm is undertaking, Merton model act as foundation for majority of model being used to measure credit risk for any firm, another widely used model is credit KMV. KMV was developed by Moody, however it employs basic techniques of Merton model to find PD.

both models works on the basic assumptions -
  • Brownian movement of stock price
  • Perfect market scenario (Equity reflect true valuation of company assets)
  • Equity volatility can be mapped to asset volatility
In Merton model , equity value of stock can be taken as call option on firm assets where strike price will be total debt of company. Let's go little deep in Merton model for option valuation-


N(d1) is the probability of stock price (S) ending more than (K) while N(d2) is the probability of same thing at risk neutral rate. N(d1)*S-N(d2)*K*exp(-r(T-t)) gives us the call option price and has been demonstrated by Black Sholes and is famous as BS formula.

As N(d1) represent probability of S being higher than K, Merton used same technique to find if company assets ends more than total debt. S was replaced by F (Firm Assets) and K was replaced by D (total debt). r was expected growth rate of assets and firm volatility was taken into account instead of equity volatility. There are number of techniques to find firm volatility , we can use following one

Volatility of Assets = Volatility of Equity * Equity value / Firm Value

Biggest disadvantage of Merton model was to assume default rate will follow normal probability as we putting d2 into N(d2) to default rate.

Credit KMV

Credit KMV came out with improvement on this instead of using normal distribution, they stopped at calculation of d2 and called it distance to default.Next step was to compare this value against database of historical US defaults to find probability of default, Moody has demonstrated (enron default) over time that KMV model is better than credit ratings as it quickly reflects the change in firm's position.

Saturday, August 13, 2011

Credit Risk & R Language

Credit risk is the risk of counter-party/client defaulting on the payment as per the payment schedule.Investor investing in corporate bonds or providing loans need to be aware of credit quality of client to ensure repayment of loans and interest to realize upon the gains. Risk taken by investor in such investments is compensated by the higher returns that he/she will be making on such investments.The excess return which investor makes on such investments is known as credit spread.

Credit Spread = Interest return (corporate bond) - Risk free rate
Risk free rate : rate of return given by Govt. securities, it is called risk free because probability of default by Govt. is almost negligible.

For a retail investor it becomes very difficult to measure/value the credit quality of corporate bonds to reach right credit spread which he/she should ask, for taking that additional risk. Such bonds/loans are rated by national rating agencies who will rate corporate issues using various models (KMV,Credit Metrics or Merton model).Rating agencies give grade to each issue,bonds depending upon the probability of default by issuing authority/institution. Rating of BBB and above are called investment grade bonds while bonds below BBB are called Junk bonds(not good for investment).

Financial institutions investing in bonds/loans needs to set aside capital to bear any shocks arising out of credit default by issuing institutions. Basel II has set guidelines for capital requirements for credit risk , credit risk can be measured using following three techniques (Basel recommend moving to next level of approach to measure credit risk depending upon maturity of central bank of country).

  1. Standardized approach
  2. Foundation internal rating based approach ( Pd measured by financial institutions while LGD provided by central bank)
  3. Advanced IRB : PD and LGD both measured by financial institutions..

Standardized approach is quite easy where we directly use credit ratings by national agencies to find out capital requirements for credit portfolio but this can lead to wrong estimation as this does not take into account correlation and diversification of the portfolio.

On the other hand both internal rating based approach gives independence of measuring PD for the portfolio, while finding out PD for the portfolio we can account for the dependency and correlation of the portfolio.We will discuss following models to measure PD for single bond or group of bonds (portfolios) and will also model this in R language:

  • Altman Z score
  • Merton Model
  • Credit KMV
  • Credit Metrics

Before getting into other models, let me explain Z score model (info from Wikipedia)

T1 = Working Capital / Total Assets

T2 = Retained Earnings / Total Assets

T3 = Earnings before Interest and Taxes / Total Assets

T4 = Market Value of Equity / Total Liabilities

T5 = Sales/ Total Assets

Z score Bankruptcy Model:

Z = 1.2T1 + 1.4T2 + 3.3T3 + 0.6T4 + .999T5

Zones of Discrimination:

Z > 2.99 -“Safe” Zones

1.81 < Z < 2.99 -“Grey” Zones

Z < 1.81 -“Distress” Zones