Having a Little Fun with 2D Grids and Interpolation
Today I needed to tackle the 2-D grid and interpolation in as efficient and re-usable way as possible. In previous attempts, I've used special data structures to hold the data points because the interpolation was handled differently. This time, I wanted to take advantage of templates for the z-value so I could re-use the class for doubles as well as entire packages of greeks. In order to do this, I decided that it'd be better to use boost::unordered_map and have the key be the coordinate value, and the value be the function value. When I needed to handle two dimensions, the outer map values were really just axis/function values, and with a simple spinlock to protect the grid, we were done.
All this coded up pretty easily. We are going to stick with a four-point interpolation scheme so the code had to "locate" the four nearest grid points to any given point, and then allow the caller to ask for the interpolated value anywhere on the grid. It's just a few detail functions, and it worked out pretty nicely. The final point was having a templated function that took the (x,y) points and the z values, and then returned the interpolated value. This would allow me to make one of these for double values, and another for the greeks package.
Because it was completely isolated from the rest of the grid, I could change the interpolation from a simple linear to a Taylor Series at any time, and that's exactly what I have planned for the greek payload. After all, I know what the x- and y-axes are in this space, and I know that within the payload I have the derivatives, several powers worth, and that will make it a lot easier to space these points further apart and get reliable interpolated values.
All in all, I have to say I've enjoyed coding this up. Not everything today has been this fun, but at least this was a good note to end the week on.