Financial Algorithms Cookbook

Error: Failed to load processor TOC
No macro named [[TOC]] found

Bonds

Interest - Discount Calculations

Accrued Interest

  • todo short description

Accrued Interest = P * R/M * A/D


Where:

A - Number of accrued days counted according to the day count basis.
D - Number of days in the period
M - number of periods per year
P - Par Value
R - annnula interest rate

Algorithm

def getAccrued(P, R, M, A, D):
        return P*(1.0 *R/M)*(1.0*A/D)

Example

What is the accrued interest of the following bond?
Par Value = 5 000 000
Rate = 13.6% (semi-annual)
Day count basis = 30/360
Settlement Date = 11/09/2001
First Interest Date = 09/23/2001

getAccrued(5000000, 13.6/100.0, 2, 46, 360)

>>>> 43444.4444444

Short Coupon

  • todo description

Accrued Interest = P * R/M*A/NL

Where:[[BR]]

A - Accrued days
M - Number of coupon periods per year
NL - Number of days representing the quasi-coupon period
P - Par Value
R - Annual coupon rate in decimal format

Algoirthm

def getAccruedShortCoupon(P, R, M, A, NL):
        return P*(1.0 *R/M)*(1.0*A/NL)

Example

A city issued $10 000 000 bonds on August 15. The bonds are dated June 1 and were setteled on September 15. The interest rate on the bonds is 7% paid semmi-annual. Assume 30/360 basis.

getAccruedShortCoupon(10000000, .07, 2, 104, 180)

>>>> 202222.222222

(taken from http://www.msrb.org/msrb1/glossary/view_def.asp?param=ACCRUEDINTEREST )

Long Coupon

*todo description

Accrued Interest = P * R/M*(sum{i=1,NC}[Ai/NLi])

Where:

Ai - Number of accrued days for the ith quasi-coupon period within odd period
M - Number of coupon periods per year
NC - Number of quasi-coupon periods that fit in add period. If the number contains a fractional part, raise it the next whole number
NLi - Length of the quasi-coupon periods within odd perid (in days)
P - Par Value
R - Annual interest rate (in decimal format)

Algorithm

def getAccruedLongCoupon(P, R, M, A, NL, NC):
        sum = 0.0
        i = 1
        
        while i <= NC:
                sum+= 1.0*  A[i-1] / NL[i-1]
                i+=1
        
        return P*(1.0 *R/M)*sum

Example

  • todo get new example

Par Value: 10 000
Rate: 7.5 (semi-annual)
issue Date: 07/01/92
First Interest Date: 04/01/93
Settlement Date: 02/01/93

M = 2
A1 = 123 (10/01/92 - 02/01/93)
A1 = 92 (07/01/92 - 10/01/92)
Nc = 2 (10/01/92 - 04/01/93), (04/01/92 - 10/01/92)
NL1 = 182 (10/01/92 - 04/01/93) NL2 = 183 (04/01/92 - 10/01/92)

getAccruedLongCoupon(10000, .075, 2,[123, 92], [182,183], 2)

>>>>441.958656098

Zero Coupon Prices and the Money Multipliers

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Using Zeros to Value Cash Flows

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

No Arbitrage

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Bond Price Dynamics

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Bond Valuation

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Yield to Maturity or Internal Rate of Return

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Bond Rankings and Interest Rates

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Bond Price Movements and Duration

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Duration -> DurationDraft

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Modified Duration

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Bond convexity

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Odd First Coupons

Typical Uses

  • todo

Ingredients

  • todo

Price (given yield) with an odd short first coupon

P = [ RV/(1 + Y/M)(N-1+DSC/E)] +

[(100 * R/M * DFC/E)/(1 + Y/M)DSC/E] +

[E{k=2;N} (100 * R/M) / (1 + Y/M)(K-1+DSC/E)] -

[100*R/M*A/E]

Where:

A - Number of days from begining of coupon period to settlement date (accured days)
DFC - Number of days from the begining of the odd first coupon period (dated date) to the first coupon date
DSC - Number of days from settlement date to first coupon date
E - Numberof days in quasi-coupon period in which the settlement date falls
K - A summation counter
M - Number of coupon periods per year (standard for the particular security involved)
N - Number of coupons payable between settlement date and redemption date (maturity date, call date, put date, etc.) (If this number contains a fractional part, raise it to the next whole number - ie 2.5 = 3)
P - Dollar price per $100 pae value
R - Annual interest rate (as a decimal)
RV - Redemption value of the security per $100 par value (RV = 100, except in those cases where call or put features must be considered)
Y - Annual yield ( as a decimal) on investment with security held to redemption
(Note: This formula does not apply to municipal securities)

Algorithm

def getOddShortFirstCoupon(A, DFC, DSC, E, M, N, R, RV, Y):

    coefficient = 1.0 + 1.0 * Y/M

    term1 = 1.0 * RV/(coefficient ** (N - 1 + (1.0 * DSC/E)))

    term2 = (100.0 * (1.0 * R/M) * (1.0 * DFC/E))/coefficient ** (1.0 * DSC/E)

    term3 = 0
    K = 2

    while K <= N:
        
        term3 += (100.0 * R/M) / coefficient ** (K - 1 + (1.0 * DSC/E))
        K += 1
        
    term4 = 100.0 * (1.0 * R/M) * (1.0 * A/E)

    return term1 + term2 + term3 - term4

Example

  • -> replace example

Calculate a dollar price given a yield for a Treasury Bond with an odd short first coupon


Settlement Date 11/11/92
Maturity Date 03/01/05
Issue / Dated Date 10/15/92
First Coupon Date 03/01/93
Day Count Basis Actual / Actual
A = 27 (10/15/92 - 11/11/92)
DFC = 137 (10/15/92 - 03/01/93)
DSC = 110 (11/11/92 - 03/01/93)
E = 181 (09/01/92 - 03/01/93) (based on the quasi-coupon date of 9/1/92)
M = 2 (semi-annual)
N = 25 (11/11/92 - 03/01/05)
R = 0.0785 (7.85%)
RV = 100.000
Y = 0.0625 (6.25%)
P = 113.597717
DOLLAR PRICE = 113.597717

Price (given yield) with an odd long first coupon

P = [ RV/(1 + Y/M)(N+Nqf+DSC/E)] +

[(100 * R/M * [E{i=1;NCF} (DFCi/NLFi)])/(1 + Y/M)Nqf+DSC/E] +

[E{k=1;N} ((100 * R/M) / (1 + Y/M)(K+Nqf+DSC/E))] -

[100*R/M*[E{i=1;NCF} (Ai/NLFi)]]

Where:

Ai - Number of occured days for the ith quasi-coupon period within the odd period (accured days)
DFCi - Number of days from dated date to the first quasi-coupon (i=1) or number of days in quasi-coupon (i=2,3,...,NCF)
NLFi - Normal length in days of the full ith quasi-coupon period within odd period
DSC - Number of days from settlement date to the next quasi-coupon date or next coupon date
E - Number of days in quasi-coupon period, or regular coupon period, in which the settlement date falls
K - A summation counter
M - Number of coupon periods per year
N - Number of coupons payable between ''the first real coupon date'' and redemption date (maturity date, call date, put date, etc.) (If this number contains a fractional part, raise it to the next whole number - ie 2.5 = 3)
NCF - Number of quasi-coupon periods that fit in odd period. (If this number contains a fractional part, raise it to the next whole number.)
Nqf - Number of ''whole'' quasi-coupon periods between settlement date and first coupon (this may be zero)
P - Dollar price per $100 pae value
R - Annual interest rate (as a decimal)
RV - Redemption value of the security per $100 par value (RV = 100, except in those cases where call or put features must be considered)
Y - Annual yield ( as a decimal) on investment with security held to redemption

(Note: This formula does not apply to municipal securities)

Algorithm

def getOddLongFirstCoupon(A, DFC, DSC, E, M, N, NCF,  NLF, Nqf, R, RV, Y):

    coefficient = 1.0 + 1.0 * Y/M
    K = 1
    term3 = 0.0
    sumAi = 0.0
    sumDFCi = 0.0
    
    while K <= N :
        term3 += 100.0 * R/M  / (coefficient **(Nqf + K + 1.0 * DSC/E))
        K += 1

    i = 1
    while i <= NCF :
        sumAi += 1.0 * A[i-1]/NLF[i-1]
        sumDFCi += 1.0 * DFC[i-1]/NLF[i-1]
        i += 1
        
    term1 = RV/(coefficient **(Nqf + N + 1.0 * DSC/E))
    term2 = 100.0 * (R/M) * sumDFCi / (coefficient **(Nqf + 1.0 * DSC/E))
    term4 = 100.0 * R/M * sumAi
    
    return term1 + term2 + term3 - term4

Example

  • -> replace example

Calculate a dollar price given a yield for a Treasury Bond with an odd short first coupon


Settlement Date 11/11/92
Maturity Date 03/01/05
Issue / Dated Date 10/15/92
First Coupon Date 03/01/93
Day Count Basis Actual / Actual
A1 = 78 (6/15/92 - 9/1/92)(based on the quasi-coupon date of 3/1/92 and 9/1/92)
A2 = 71 (9/1/92 - 11/11/92)(based on the quasi-coupon date of 3/1/92 and 9/1/92)
DFC1 = 78 (6/15/92 - 9/1/92)(based on the quasi-coupon date of 3/1/92 and 9/1/92)
DFC2 = 181 (9/1/92 - 3/1/93)(based on the quasi-coupon date of 3/1/92 and 9/1/92)
DSC = 110 (11/11/92 - 03/01/93)
E = 181 (09/01/92 - 03/01/93) (based on the quasi-coupon date of 3/1/92 and 9/1/92)
M = 2 (semi-annual)
N = 24 (3/1/93 - 03/01/05)
NCF - 2 (6/15/92 - 3/1/93)
NLF1 - 184 (3/1/92 - 9/1/92) (based on the quasi-coupon date of 3/1/92 and 9/1/92)
NLF2 - 181 (9/1/92 - 3/1/93) (based on the quasi-coupon date of 3/1/92 and 9/1/92)
Nqf - 0 (11/11/92 - 3/1/93)
R = 0.0935 (9.35%)
RV = 100.000
Y = 0.0775 (7.75%)

getOddLongFirstCoupon([78,71], [78,181], 110, 181, 2, 24, 2, [184,181], 0, 0.0935, 100.000, 0.0775)
>>>> 112.478106

References

Odd Last Coupons

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Odd First and Last Coupons

Odd Short First and Odd Long Last Coupons

Typical Uses

  • todo

Ingredients

P =
[RV/(1+Y/M)(N-1+DSC/E+Nql+DLQ/LQL)] +
[(100*R/M*DFC/E)/(1+Y/M)DSC/E] +
[sum{K=2,N}[(100*R/M)/(1+Y/M)K-1+DSC/E]] +
[(100*R/M*sum{i=1,NCL}[DLCi/NLLi])/(1+Y/M)N-1+DSC/E+Nql+DLQ/LQL] -
[100*R/M*A/E]

Where:

A - Number of occured days for the ith quasi-coupon period within the odd period (accured days)
DFC - Number of days from the beginning of the odd first coupon (dated date) to the first coupon date
NLFi - Normal length in days of the full ith quasi-coupon period within odd period
DSC - Number of days from settlement date to the next quasi-coupon date or next coupon date
E - Number of days in quasi-coupon period, or regular coupon period, in which the settlement date falls
K - A summation counter
M - Number of coupon periods per year
N - Number of coupons payable between ''the first real coupon date'' and redemption date (maturity date, call date, put date, etc.) (If this number contains a fractional part, raise it to the next whole number - ie 2.5 = 3)
NCF - Number of quasi-coupon periods that fit in odd period. (If this number contains a fractional part, raise it to the next whole number.)
Nqf - Number of ''whole'' quasi-coupon periods between settlement date and first coupon (this may be zero)
P - Dollar price per $100 pae value
R - Annual interest rate (as a decimal)
RV - Redemption value of the security per $100 par value (RV = 100, except in those cases where call or put features must be considered)
Y - Annual yield ( as a decimal) on investment with security held to redemption

(Note: This formula does not apply to municipal securities)

==== Algorithm ===

def getOddShortFirstOddLongLast(A, DFC, DLC, DLQ, DSC, E, LQL, M, N, NCL, NLL, Nql, R, RV, Y):

    coefficient = 1.0 + 1.0 * Y/M
    K = 2

    term1 = RV/(coefficient **(N - 1 + 1.0*DSC/E + Nql + 1.0*DLQ/LQL))
    term2 = (100.0 * R/M * DFC/E) / coefficient ** (1.0*DSC/E)
    while K <= N :
        term3 += 100.0 * R/M  / (coefficient **(K - 1 + 1.0 * DSC/E))
        K += 1

    i = 0
    while i < NCL:
        term4 += 1.0 * DLC[i] / NLL[i]
        i += 1

    term4 = (100.0 * R/M * term4) / coefficient ** (N - 1 + 1.0*DSC/E + Nql + 1.0*DLQ/LQL)
    term5 = 100.0 * R/M * A/E

    return term1 + term2 + term3 + term4 - term5

Example

References

Stepped Coupons

Typical Uses

  • todo

Ingredients

  • todo

Algorithm

Example

References

Zero Coupon

Typical Uses

The zero coupon securities do not pay any periodic interest payment. This type of instruments are sold at discount rate (for example $50 for every $100 of the face value) and increase in value as they aproach the maturity. The securities mature to its face value. For example one may buy a zero coupon bond with par value of $1000 and 10 years to maturity for $503. In ten years the return on the investment will be $1000. This investment yields 7.0% of return paid at the maturity date.

Ingredients

Computations of price and yield of zero coupon securities are based on quasi-cupon periods. A quasi-coupon periods are the coupon periods which would exist if teh security was paying interest rate diffenet from zero.

Price

PV = [ RV / ( 1 + Y / M )Nq - 1 + DSC / E]

Where:

  • PV - Present value per $100 of face value
  • RV - Redemption Value
  • Y - Anual yield (in a decimal form) on investment in security held to redemption
  • M - Number of quasi-coupon periods per year
  • E - Number of days in quasi-coupon period
  • Nq - Number of quasi-coupon periods between setelment date and redemption date. If the number contains a fractional part, it has to be rounded to the next whole number.
  • DSC - Number of days from settlement date to next quasi-coupon date as if the security paid periodic interest.

Algorithm

 int getPrice(double y, double rv, double nq, int e, int dsc, int m) {
  
        _nq = (int)Math.round(nq);
        
        double coefficient = (_nq -1 + dsc*1.0/e*1.0);
        
        return rv/Math.pow(1+y/m, coefficient); 

    }
def zeroCouponPrice(Y, RV, Nq, E, DSC, M):
  Nq = Math.ceil(Nq)
  coefficient = Nq - 1 + DSC / E
  PV = ( RV / ( 1 + Y / M )) ** coefficient
  return PV

Example

A municipal bond was setteled on 02/12/92. The maturity date of the bond is 07/01/05. What was the price of the bond if the yield is 10.55%?

Y = 10.55 (0.1055)
RV = 100
assume semi-annual quasi period:
  M = 2
  E = 180
  DSC = 139
  Nq = 27

getPrice(Y, RV, Nq, E, DSC, M)
>>>25.252446

Yield

Y = [ (RV / P)1/(Nq - 1 + DSC/E) - 1]*M

Where:

  • Y - Anual yield (in a decimal form) on investment in security held to redemption
  • PV - Present value per $100 of face value
  • RV - Redemption Value
  • M - Number of quasi-coupon periods per year
  • E - Number of days in quasi-coupon period
  • Nq - Number of quasi-coupon periods between setelment date and redemption date. If the number contains a fractional part, it has to be rounded to the next whole number.
  • DSC - Number of days from settlement date to next quasi-coupon date as if the security paid periodic interest.

Algorithm

  int getYield(double p, double rv, double nq, int e, int dsc, int m) {
  
        _nq = (int)Math.round(nq);
        
        double coefficient = 1D/(_nq -1 + dsc*1.0/e*1.0);
        
        return (int)(Math.pow(rv/p, coefficient) - 1) * m * 100; 

    }
def zeroCouponYield(PV, RV, Nq, E, DSC, M) {
  Nq = Math.ceil(Nq)
  coefficient = 1/(Nq - 1 + DSC/E)
  Y = M *(RV /PV)** coefficient) -1)
  return Y

Example

A corporation issued a zero coupon bond with redemption value of $100. The bond is sold at $25.125 discount price. What is a yield on investment in such a bond with settlement date 08/26/2002 and maturity date 09/01/2015?

P = 25.125
RV = 100
assume semi-annual quasi period:
  M = 2
  E = 180
  DSC = 5
  Nq = 27

getYield(P, RV, Nq, E, DSC, M)
>>>10.900794

References