**PirateGrunt**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The other day, I picked up the latest copy of the CAS’ journal, Variance and skipped to the back where Leigh Halliwell had an article. I hope that I’m well on record as being one of his biggest fans, but if not, let me remedy that now. Leigh Halliwell has done really tremendous stuff. He’s mathematically sophisticated, but addresses practical problems. I often struggle to keep up with his flurry of ideas and deft handling of the math, but once I (possibly) sort it out, it becomes embedded in my thinking.

This most recent piece is fairly short and only addresses the basic mechanics of the discrete Fourier transform. I’ve used the Fourier transform before (even helped write a paper which uses it), but I’ve not touched it in ages. My understanding is rusty. Further, I’ve done nothing at all with complex numbers in R, so this will be interesting.

My first goal is to visualize the second section of Halliwell’s paper. This is nothing more than describing the idea of “roots of unity”. A couple moments with Google informs me how to construct a complex number in R. With that, I can knock together a short function to generate roots. Let’s have a look:

There’s no real reason to create two functions. By Euler’s formula they’re identical. I was curious if there were any machine precision issues that might favor one approach. According to `R`

, the results are identical.

Note that after n=1, the sums don’t quite agree with Halliwell’s math. Of course the smart money is on Halliwell (and I certainly can’t fault his algebra). This may be an issue of precision.

Sets of numbers are fine, but all those plusses and minuses and letter “i”s are freaking me out. Let’s look at these numbers visually. To reinforce the intuition, we’ll draw a unit circle and confirm that the points do indeed lie there. I’ll construct a list of data frames to hold the values as `ggplot2`

tends to be happier with them. I also did a few experiments (not shown) plotting the modulus and argument values with polar coordinates.

So, here we have a visual representation of Halliwell’s main point: the Fourier transform is periodic for fixed `n`

. If you need more points, we’ll just keep going around the unit circle to get them. This is something we can grasp intuitively by referring to Euler’s formula, which is the sum of sine and cosine functions, which are periodic. Let’s draw one last plot where we show 8 points and also 4096.

By fiddling with the transparency, we can see the smaller set overlaid on the larger one. If we’re only using 8 points, we’ll go round that circle pretty quickly. With 4096, we won’t.

OK, so circles are fun, but the real important bit is how it can help us price insurance. It’s been ages since I translated Heckman and Meyer’s paper from Fortran to VBA and my brain is a bit foggy. For a warmup, we’ll reproduce Halliwell’s example of convolving a binomial process. We’ll use the same construction he applies; namely a vector with four values and we’ll push it farther than it ought to go so that we can observe the cyclicality.

0.25 | 0.125 | 0.125 | 0.188 |

0.5 | 0.375 | 0.25 | 0.188 |

0.25 | 0.375 | 0.375 | 0.312 |

0 | 0.125 | 0.25 | 0.312 |

So, we got exactly what Halliwell got. It’s wrong, of course, so let’s pad things a bit and get proper probabilities. In this case, we know that we’ll tap out at 6 elements, but let’s go ahead and pad to 8.

0.25 | 0.125 | 0.0625 | 0.0312 |

0.5 | 0.375 | 0.25 | 0.156 |

0.25 | 0.375 | 0.375 | 0.312 |

3.2e-17 | 0.125 | 0.25 | 0.312 |

0 | 0 | 0.0625 | 0.156 |

-3.2e-17 | -2.78e-17 | -1.85e-17 | 0.0312 |

I’m warmed up and ready to hit his last example: an aggregate set of losses where the number of claims is Poisson distributed, lambda = 3 and the severity is a very simple set of four values (the first of which corresponds to a claim value of zero). The `fft`

function

Here we see a very clear graphic presentation of Halliwell’s point on cyclical overflow. The difference is greatest at the leftmost point of the graph. Points 7 and 8 *almost* agree. We’ll also note that 4096 is (for this range of values) probably overkill. The probability drops off substantially after x=30. Just for fun, let’s compare n=32 to n=4096

Let’s compare the first 15 rows.

x | 32 | 4096 |
---|---|---|

1 | 0.0498 | 0.0498 |

2 | 0.0747 | 0.0747 |

3 | 0.116 | 0.116 |

4 | 0.133 | 0.133 |

5 | 0.136 | 0.136 |

6 | 0.125 | 0.125 |

7 | 0.106 | 0.106 |

8 | 0.0831 | 0.0831 |

9 | 0.0613 | 0.0613 |

10 | 0.0429 | 0.0429 |

11 | 0.0286 | 0.0286 |

12 | 0.0183 | 0.0183 |

13 | 0.0112 | 0.0112 |

14 | 0.00666 | 0.00666 |

15 | 0.00381 | 0.00381 |

One final thing. At one point, Halliwell asks us to imagine that we’re dealing with an infinite sum. This is impossible as there can’t be infinite roots of unity, so we have to cut things off somewhere. But his point is significant. No matter how many points we’re assuming, it is really an infinite set; a circle has no end.

Let’s have another look at our set of 8 points.

Little | Big |
---|---|

0.112 | 0.112 |

0.118 | 0.118 |

0.145 | 0.145 |

0.151 | 0.151 |

0.147 | 0.147 |

0.132 | 0.132 |

0.109 | 0.109 |

0.0852 | 0.0852 |

This was loads of fun! I’ve gotten reacquainted with a technique I haven’t used in ages and I can’t wait to turn it loose on some actual data. `R`

is flexible enough that switching between severity and frequency distributions is a breeze and the visualization makes it very easy to get a handle on how to tune results. So much fun!

## References

- Halliwell’s excellent article
- John Myle’s White talks complex numbers
- Yes, I needed StackOverflow to draw a unit circle. I thought there might be an easy method using something like
`abline`

. - Obligatory Wikipedia reference
- Tukey’s original paper!
- Heckman and Meyers’ paper with direct inversion
- Loss Models textbook I can’t imagine how you can be an actuary and not own this book.

#### Session info:

**leave a comment**for the author, please follow the link and comment on their blog:

**PirateGrunt**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.