0

If I wanted to plot a Treasury yield curve, for a given date, my x, y variables would look something like this:

x = ['1month', '2month', '3month', '6month', '1year', '2year', '3year', '5year', '7year', '10year', '20year', '30year']
y = [0.09, 0.09, 0.10, 0.11, 0.12, 0.11, 0.13, 0.22, 0.40, 0.56, 1.01, 1.23]

I could then plot the curve.

However, what I would also like to do is generate different y-axis lists in order to be able to plot sample curves which would act as a comparison to the real curve. Examples of such curves can be found here: What do different yield curve shapes mean?

So, basically, I would end up with 4 additional lists, of 12 values, for: y_normal, y_inverted, y_steep and y_flat.

I have tried manually working out what these values should be, but I am finding it tricky, and strongly suspect there is a Pythonic way of generating these lists - hopefully basing the '1month' value, of each list, on the '1month' value of the real data.

From there, I don't want to just plot these, on the same chart, but would like to programmatically compare them with the real curve in order ascertain the real curve's type (normal, inverted, steep, flat.) I believe I can use a combination of scipy.optimize curve_fit and sklearn.metrics r2_score to achieve that part, but would also appreciate any additional guidance on how to do it.

If anyone can help me to solve the value generation problem, I would be extremely grateful, as that is the part I am most struggling with, but guidance on the comparison piece would elevate you to superstar level!

HapiDaze
  • 271
  • 6
  • 13

1 Answers1

0

Check out the section "Fake stock market data" from this StackOverflow post: https://stackoverflow.com/a/30424537/13608599. You could modify this to generate synthetic yield curves (e.g., create upward-sloping trend line plus noise that passes through the 1-month point).

You can get decades worth of real yield curve data here: https://fred.stlouisfed.org

jsmart
  • 2,798
  • 1
  • 4
  • 12