library for lattice data structure

Hi, I have a general question regarding data structures, maybe functional data structures, in Haskell and I’m hoping for some advice. I would like to implement the binomial option pricing model in Haskell. https://en.wikipedia.org/wiki/Binomial_options_pricing_model Given its lattice structure, I originally thought of trying a recursive implementation, but because it’s recombining, the interior nodes would be calculated twice. Although the implementation might be elegant, it doesn’t seem inefficient. Therefore I’ve been searching through Hackage for a data structure library that might lend itself to this problem. There are a ton of libraries and as I don’t have any experience with them, I was hoping someone could provide some advice to narrow the field. Some of the packages I've found so far are: lattices package http://hackage.haskell.org/package/lattices From the name I thought this might be perfect, but the documentation is very terse and there doesn’t appear to be any other documentation so I’m not even sure how to get started with constructing a lattice with this package. matrix package https://hackage.haskell.org/package/matrix This package seems fairly straightforward, and a matrix would work (it’s probably what I would use in R), but it doesn’t seem ideal. There are other more sophisticated packages with matrices like hmatrix and repa but they seem like overkill unless this package is just slow. vector package https://hackage.haskell.org/package/vector Vectors might also work for a data structure, but a matrix just seems like it would be easier. I thought this example implementation in Haskell was intriguing but it doesn’t sound as though it’s very quick. http://www.thulasidas.com/2009-03/a-new-kind-of-binomial-tree.htm Any advice on an appropriate data structure in Haskell for the problem? The solution doesn’t necessarily have to include a package. Thanks James

Hi James, On Tue, Mar 11, 2014 at 07:51:23PM -0500, James Toll wrote:
Hi,
I have a general question regarding data structures, maybe functional data structures, in Haskell and I’m hoping for some advice. I would like to implement the binomial option pricing model in Haskell.
https://en.wikipedia.org/wiki/Binomial_options_pricing_model
Given its lattice structure, I originally thought of trying a recursive implementation, but because it’s recombining, the interior nodes would be calculated twice.
It's even worse than that---a naive implementation will end up doing exponentially too much work. The interior nodes are recalculated much more than just twice. I once had a student who implemented exactly this, and did it in a clever way---they constructed an actual binary tree data structure, but using some "knot-tying" techniques (http://www.haskell.org/haskellwiki/Tying_the_Knot) to result in all the internal nodes actually being shared in memory. So it really was a binary tree data structure but in memory it looked like a lattice.
lattices package http://hackage.haskell.org/package/lattices From the name I thought this might be perfect, but the documentation is very terse and there doesn’t appear to be any other documentation so I’m not even sure how to get started with constructing a lattice with this package.
This package is actually about the mathematical abstraction of lattices; it has nothing to do with data structures. Matrices and vectors are definitely too low-level for this problem.
I thought this example implementation in Haskell was intriguing but it doesn’t sound as though it’s very quick. http://www.thulasidas.com/2009-03/a-new-kind-of-binomial-tree.htm
You're right; this implementation has the exponential blowup problem. However, it's actually possible to take this code and make some simple modifications so that it runs quickly, by memoizing the function f. One simple way to do that is to replace the function f with lookups into an array---the trick is that you can construct the array recursively, and thanks to laziness the entries in the array will automatically be computed in the right order, so you don't have to worry about that like you would in, say, R. For an example, see the very last section (titled "Dynamic Programming") on this page: http://www.cis.upenn.edu/~cis194/lectures/06-laziness.html -Brent

On Mar 11, 2014, at 8:40 PM, Brent Yorgey wrote:
I once had a student who implemented exactly this, and did it in a clever way---they constructed an actual binary tree data structure, but using some "knot-tying" techniques (http://www.haskell.org/haskellwiki/Tying_the_Knot) to result in all the internal nodes actually being shared in memory. So it really was a binary tree data structure but in memory it looked like a lattice.
I thought this example implementation in Haskell was intriguing but it doesn’t sound as though it’s very quick. http://www.thulasidas.com/2009-03/a-new-kind-of-binomial-tree.htm
You're right; this implementation has the exponential blowup problem. However, it's actually possible to take this code and make some simple modifications so that it runs quickly, by memoizing the function f. One simple way to do that is to replace the function f with lookups into an array---the trick is that you can construct the array recursively, and thanks to laziness the entries in the array will automatically be computed in the right order, so you don't have to worry about that like you would in, say, R. For an example, see the very last section (titled "Dynamic Programming") on this page:
http://www.cis.upenn.edu/~cis194/lectures/06-laziness.html
-Brent
Brent, Wow, thank you. I really appreciate your taking the time to provide so much guidance on the subject. I’m vaguely familiar with memoization, but had not even heard of “knot-tying" techniques. So I’ll really have to look into both topics. Again, thanks for the guidance. As they say, you don’t know what you don’t know, so it’s just so helpful to be pointed in the right direction. Thanks, James

On Wed, Mar 12, 2014 at 7:51 AM, James Toll
https://en.wikipedia.org/wiki/Binomial_options_pricing_model
Given its lattice structure, I originally thought of trying a recursive implementation, but because it's recombining, the interior nodes would be calculated twice.
Looks like a case of Finite Language Syndrome. 'Lattice' has a pretty specific technical meaning in math, which isn't what's referred to here. If something on hackage mentions lattice, it's invariably the math meaning. This 'lattice' of high finance is a tree data structure, in fact, a special tree you'll probably recognize as Pascal's Triangle. You might want to look up efficient FP representations of the latter. -- Kim-Ee

On Mar 11, 2014, at 9:57 PM, Kim-Ee Yeoh wrote:
Looks like a case of Finite Language Syndrome. 'Lattice' has a pretty specific technical meaning in math, which isn't what's referred to here. If something on hackage mentions lattice, it's invariably the math meaning.
Yes, I was concerned there might be some conflict between the finance, math and/or physics terminology. I gave a fair amount of thought to how to refer to the structure, but in the end just picked lattice because that’s how it’s referred to in the wikipedia link I included and because I had already read a lot of quibbling over the use of “tree”, mostly in the context of binary trees and binary heaps. Coming from a finance background, differences in terminology between finance, physics, math, and statistics is par for the course. I’ll have to be better about understanding the terminology from a math perspective and will try to keep that in mind when posting to this list. Thanks, James

On Wed, Mar 12, 2014 at 11:35 AM, James Toll
Coming from a finance background, differences in terminology between finance, physics, math, and statistics is par for the course. I'll have to be better about understanding the terminology from a math perspective and will try to keep that in mind when posting to this list.
On deeper reflection, I realize it's more complicated than that. Because there are at least two meanings in math. The lattices on hackage are all about posets and meets and joins, tracing back to the work of Birkhoff in that subspecialization of algebra known as order theory. Then there are the lattices in sphere packings and geometric number theory and 'lattice'-based cryptography, cf ntru. One could say that the geometric lattices are closer to binomial lattices, but not really. I wonder why 'binomial tree' isn't perfectly cromulent. -- Kim-Ee

On 3/12/14 1:19 AM, Kim-Ee Yeoh wrote:
On Wed, Mar 12, 2014 at 11:35 AM, James Toll
mailto:james@jtoll.com> wrote: Coming from a finance background, differences in terminology between finance, physics, math, and statistics is par for the course. I’ll have to be better about understanding the terminology from a math perspective and will try to keep that in mind when posting to this list.
On deeper reflection, I realize it's more complicated than that. Because there are at least two meanings in math.
The lattices on hackage are all about posets and meets and joins, tracing back to the work of Birkhoff in that subspecialization of algebra known as order theory.
Then there are the lattices in sphere packings and geometric number theory and 'lattice'-based cryptography, cf ntru.
One could say that the geometric lattices are closer to binomial lattices, but not really. I wonder why 'binomial tree' isn't perfectly cromulent.
Thanks. Your comments have embiggened our understanding. -- Dudley

On Wed, Mar 12, 2014 at 11:09 PM, Dudley Brooks
Thanks. Your comments have embiggened our understanding.
No Springfield and no bicentennial, but I just realized that I don't know how to represent Pascal's Triangle properly in Haskell -- omg! Google would be cheating. Also boring. So I've self-assigned myself this essay. -- Kim-Ee
participants (4)
-
Brent Yorgey
-
Dudley Brooks
-
James Toll
-
Kim-Ee Yeoh