[R-sig-finance] 3-d visualisation of yield curves?
Ajay Shah
ajayshah at mayin.org
Sun Aug 29 15:45:28 CEST 2004
A yield curve is a picture z(t) running (say) from maturity t=0 to
t=10 years. And, the yield curve fluctuates; there's a new one every
day. So you have a time-series of curves.
I have seen very pretty 3d color visualisation of the changing yield
curve. The x axis is time, the y axis is maturity, the z axis is the
interest rate. Low interest rates are coloured blue and high interest
rates are coloured red. I have also seen a user-interface where the
person can change the point of perspective and interactively look at
the scene from different vantage points.
q: How would one do this in R? :-) Have you deployed ggobi or lattice
graphics for the task?
q: Does it add insight to look at yield curves in this way? Wise men
on the r-help mailing list generally say that you don't gain too
much insight from pretty 3d plots, so I have de-emphasised
them. But one can't help feeling tempted by those pretty pictures!
There's a code fragment at the end of this email, which sets up a
matrix syc.points which has 118 rows (for 118 days) and 100 columns,
where each column is the spot interest rate at one maturity. The
maturities run from 0.1 to 10 years. This may help you play with the
question. In case it's of interest, this is the spot yield curve in
India from 1 April this year onwards, which has been an exciting time.
Thanks,
-ans.
--
Ajay Shah Consultant
ajayshah at mayin.org Department of Economic Affairs
http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
nsz <- function(b0, b1, b2, tau, t) {
tmp = t/tau
tmp2 = exp(-tmp)
return(b0 + ((b1+b2)*(1-tmp2)/(tmp)) - (b2*tmp2))
}
ns <- structure(list(date = as.integer(c(20040401, 20040402, 20040405,
20040406, 20040407, 20040408, 20040410, 20040412, 20040413, 20040415,
20040416, 20040417, 20040419, 20040420, 20040421, 20040422, 20040423,
20040424, 20040427, 20040428, 20040429, 20040430, 20040503, 20040505,
20040506, 20040507, 20040508, 20040510, 20040511, 20040512, 20040513,
20040514, 20040515, 20040517, 20040518, 20040519, 20040520, 20040521,
20040522, 20040524, 20040525, 20040526, 20040527, 20040528, 20040529,
20040531, 20040601, 20040602, 20040603, 20040604, 20040605, 20040607,
20040608, 20040609, 20040610, 20040611, 20040612, 20040614, 20040615,
20040616, 20040617, 20040618, 20040619, 20040621, 20040622, 20040623,
20040624, 20040625, 20040626, 20040628, 20040629, 20040630, 20040701,
20040702, 20040703, 20040705, 20040706, 20040707, 20040708, 20040710,
20040712, 20040713, 20040714, 20040715, 20040716, 20040717, 20040719,
20040720, 20040721, 20040722, 20040723, 20040724, 20040726, 20040727,
20040728, 20040729, 20040730, 20040731, 20040802, 20040803, 20040804,
20040805, 20040806, 20040807, 20040809, 20040810, 20040811, 20040812,
20040813, 20040814, 20040816, 20040817, 20040818, 20040819, 20040821,
20040823, 20040824, 20040825)), b0 = c(7.3422, 7.8217, 11.3901,
7.4635, 7.2226, 7.2234, 7.3554, 7.3074, 8.468, 7.4894, 7.333,
7.4633, 7.6058, 7.1231, 7.686, 7.0339, 7.4681, 7.3624, 7.3174,
7.4157, 7.7941, 7.4091, 7.8367, 7.4442, 7.7108, 7.4472, 7.4185,
7.1485, 7.0891, 7.3275, 6.9325, 7.0438, 7.0387, 7.2428, 7.7536,
7.1781, 7.1971, 7.1158, 9.4099, 14.0915, 6.9736, 7.3604, 8.2228,
8.9938, 9.2337, 14.5484, 11.5983, 14.1417, 11.5225, 7.516, 7.7201,
7.5108, 7.575, 7.5737, 8.1235, 7.6523, 7.8026, 7.4334, 14.4161,
7.4442, 8.3493, 7.7468, 7.8207, 7.8434, 8.3185, 8.0729, 7.9769,
8.1296, 7.4141, 7.7981, 8.023, 8.2652, 7.9386, 7.8601, 7.9348,
10.3803, 9.4022, 7.7753, 9.1768, 8.8638, 7.8373, 7.421, 7.4414,
7.3915, 7.2947, 7.1905, 7.379, 6.9234, 7.5494, 6.9746, 7.9473,
14.7695, 7.3734, 7.3422, 4.6736, 7.4979, 7.4727, 7.3631, 7.8308,
7.6939, 7.0801, 7.7495, 5.0205, 7.6851, 15.2449, 11.4073, 10.5747,
7.47, 8.06, 15.7304, 10.26, 7.3, 9.51, 9.32, 8.838, 13.3616,
15.5854, 11.0715), b1 = c(-2.721, -3.2183, -6.5193, -3.2818,
-2.4963, -2.6716, -2.4753, -2.8383, -3.9206, -2.8313, -2.7894,
-2.8732, -3.2573, -2.5732, -2.9594, -2.3505, -2.6395, -2.6339,
-2.3883, -2.7691, -3.286, -2.5451, -3.0089, -2.9315, -3.2165,
-2.7868, -1.4352, -2.4697, -2.0563, -2.7682, -2.5612, -2.5146,
-2.5144, -2.4144, -2.9954, -2.3257, -2.1419, -2.1677, -4.3465,
-9.4, -2.1492, -2.6646, -3.4617, -4.1504, -3.1783, -9.4, -6.6092,
-9.4, -6.5229, -2.3708, -2.3271, -2.9659, -2.7749, -2.5683, -3.1604,
-2.3273, -2.543, -2.7601, -9.4, -2.2319, -3.1117, -3.3958, -2.7336,
-2.7717, -3.1778, -2.8743, -2.9824, -3.3025, -2.3269, -2.8134,
-3.2428, -3.4767, -3.2345, -3.019, -2.7832, -5.2133, -4.2563,
-2.9053, -4.092, -3.7751, -2.9284, -2.381, -2.6428, -2.7985,
-2.5906, -2.1872, -2.7013, -2.4288, -2.5056, -2.529, -2.9546,
-9.4, -2.2121, -2.419, -0.0105, -2.6278, -2.7345, -2.278, -2.6838,
-2.2775, -3.0019, -2.4925, -0.0191, -2.4934, -9.2345, -5.4497,
-4.4509, -2.88, -2.31, -9.1449, -3.96, -3.95, -3.4, -3.28, -3.1081,
-6.9305, -9.4, -4.9702), b2 = c(-2.9455, -3.1714, -7.1921, -1.2954,
-2.9376, -2.3315, -3.3516, -1.9412, -2.8231, -2.9133, -2.3912,
-2.7164, -0.6094, -2.4146, -3.0891, -2.8961, -3.452, -2.9017,
-3.525, -2.7331, -2.2974, -3.4923, -3.5535, -2.2847, -2.0384,
-2.8926, -5.7102, -2.7131, -3.5915, -2.4034, -1.4584, -2.2325,
-1.6771, -3.3011, -3.3382, -3.2615, -3.7138, -3.5048, -6.5431,
-8.59, -3.4933, -2.9518, -3.7216, -4.8041, -8.4707, -11.6242,
-7.4944, -8.6059, -7.4215, -4.0466, -4.4962, -2.4366, -2.9504,
-3.7409, -3.5684, -4.1972, -4.0236, -1.9687, -9.1264, -3.5223,
-4.1759, -1.2065, -3.4071, -3.2178, -3.4439, -3.0968, -2.1767,
-0.6931, -2.2134, -2.3287, -0.1982, -3.198, 0.0274, -1.4918,
-2.1835, -2.5059, -1.2104, -3e-04, -0.5105, -0.1034, -0.0324,
-0.0108, -0.0785, -0.0014, 0, -0.4718, -0.0261, 0.036, -0.0113,
0.0142, -0.0189, -7.7247, -0.0115, -0.0146, 7.0134, 0.0116, 0.0098,
-0.0168, -0.0012, -0.0064, 0.0239, 0.0032, 6.0088, 0.01, -7.839,
-2.518, -1.4064, 0.02, -0.01, -9.5194, -2.12, 1.62, -1.98, -0.04,
-0.0255, -6.7197, -9.2038, -2.9766), tau = c(4.5266, 5.7445,
10.7687, 6.682, 4.496, 5.1623, 4.8581, 6.3769, 9.7641, 5.8551,
5.9208, 6.1627, 11.1458, 4.9739, 6.5382, 4.228, 5.2418, 5.5392,
4.6344, 5.4967, 7.0604, 4.7554, 6.2326, 5.9656, 7.7035, 5.3672,
3.8092, 4.2389, 3.5272, 4.9461, 4.8159, 4.0655, 4.972, 3.5159,
4.9192, 3.8536, 3.6992, 3.4197, 5.8819, 14.656, 2.8546, 4.2702,
5.9606, 6.1133, 4.7138, 10.0547, 9.0578, 12.9292, 8.6205, 3.6096,
4.4072, 4.5344, 4.5593, 3.6541, 5.1583, 3.7913, 4.2516, 4.5431,
13.0367, 3.5478, 5.3657, 5.4139, 3.8999, 3.9799, 4.6267, 4.1281,
4.5191, 6.8646, 2.9362, 3.6888, 7.3445, 3.835, 7.5136, 4.6792,
5.2262, 15.5, 15.5, 8.7869, 15.5, 15.4999, 8.3618, 7.8984, 5.6957,
4.3874, 4.4051, 4.7968, 5.0399, 3.0356, 7.3138, 2.8834, 7.6381,
15.5, 6.3357, 4.7519, 10.974, 3.925, 3.7252, 4.5168, 6.2362,
6.282, 1.6898, 5.8815, 9.6076, 4.6436, 15.5, 15.5, 15.5, 1.39,
4.49, 15.5, 12.39, 1.16, 8.23, 13.41, 7.886, 15.5, 13.5623, 15.5
)), .Names = c("date", "b0", "b1", "b2", "tau"), row.names = c("2105",
"2106", "2107", "2108", "2109", "2110", "2111", "2112", "2113",
"2114", "2115", "2116", "2117", "2118", "2119", "2120", "2121",
"2122", "2123", "2124", "2125", "2126", "2127", "2128", "2129",
"2130", "2131", "2132", "2133", "2134", "2135", "2136", "2137",
"2138", "2139", "2140", "2141", "2142", "2143", "2144", "2145",
"2146", "2147", "2148", "2149", "2150", "2151", "2152", "2153",
"2154", "2155", "2156", "2157", "2158", "2159", "2160", "2161",
"2162", "2163", "2164", "2165", "2166", "2167", "2168", "2169",
"2170", "2171", "2172", "2173", "2174", "2175", "2176", "2177",
"2178", "2179", "2180", "2181", "2182", "2183", "2184", "2185",
"2186", "2187", "2188", "2189", "2190", "2191", "2192", "2193",
"2194", "2195", "2196", "2197", "2198", "2199", "2200", "2201",
"2202", "2203", "2204", "2205", "2206", "2207", "2208", "2209",
"2210", "2211", "2212", "2213", "2214", "2215", "2216", "2217",
"2218", "2219", "2220", "2221", "2222"), class = "data.frame")
ns$date = as.Date(as.character(ns$date), format="%Y%m%d")
syc.points <- matrix(nrow=nrow(ns), ncol=100)
# How to do this in a clever vectorised way?
for (i in 1:nrow(ns)) {
j <- 1
for (t in seq(from=0.1,length=100,by=0.1)) {
syc.points[i,j] <- nsz(ns$b0[i], ns$b1[i], ns$b2[i], ns$tau[i], t)
j <- j + 1
}
}
More information about the R-sig-finance
mailing list