The recursive equation enters into un-initialized interval hence index out of exeption. You can avoid this bug by tweaking: initialize not-needed valuations and assign them to zero.
This creates some overhead but simplifies the logic, cannot think about any simpler way to do this.