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.