0

How do I calculate internal rate of return (IRR) and yield to maturity (YTM) in Sympy? I am trying to calculate the YTM of a bond of $1000 face value that pays $50 in coupons every year. The bond is currently selling for $900, and matures in 3 years. Using the formula for the YTM:

900 = [50 / (1 + r)] + [50 / (1 + r)^2] + [50 / (1 + r)^3] + [1000 / (1 + r)^3]

How do I solve for r, the YTM? Does Sympy have a solver for this kind of problem, or do I have to do it in Python using Newton's method?

Flux
  • 9,805
  • 5
  • 46
  • 92

1 Answers1

0

When following the docs for Solvers

from sympy.solvers import solve
from sympy import Symbol
r = Symbol('r')
solve((50 / (1 + r)) + (50 / pow((1 + r),2)) + (50 / pow((1 + r),3)) + (1000 / pow((1 + r),3)) - 900)

returns:

[-53/54 + (-1/2 - sqrt(3)*I/2)*(sqrt(11594049)/5832 + 11492/19683)**(1/3) + 55/(2916*(-1/2 - sqrt(3)*I/2)*(sqrt(11594049)/5832 + 11492/19683)**(1/3)),
 -53/54 + 55/(2916*(-1/2 + sqrt(3)*I/2)*(sqrt(11594049)/5832 + 11492/19683)**(1/3)) + (-1/2 + sqrt(3)*I/2)*(sqrt(11594049)/5832 + 11492/19683)**(1/3),
 -53/54 + 55/(2916*(sqrt(11594049)/5832 + 11492/19683)**(1/3)) + (sqrt(11594049)/5832 + 11492/19683)**(1/3)]

P.S. I have no clue about what an YTM, IRR is...

Luuk
  • 12,245
  • 5
  • 22
  • 33
  • Is there a way to show the answer in decimals rather than fractions? I only need the result that is between 0 and 1. – Flux Nov 05 '22 at 20:34