F#: PSeq.sumBy, What’s Gone Wrong?
As per my previous blog posting, I have cashflow generation and hence NPV being calculated for my IRS rate. I’ve move from a List to a Seq for storage of the InterestRateStream’s. Primarily this move was so that I could take advantage of PSeq.
If we look at my current code, it’s processes each stream sequentially:
let valueDate = new DateTime(2010,1,1)
let trade = createTrade 101
let cashFlows =
trade.SwapStream |> Seq.sumBy (fun x -> generateCashflow(x,trade, valueDate))
printfn "IRS NPV %A" cashFlows
Since the cashflow generation of each leg is separate, I’d like to parallelize the processing, so I change the code to use PSeq:
let valueDate = new DateTime(2010,1,1)
let trade = createTrade 101
let cashFlows =
trade.SwapStream |> PSeq.sumBy (fun x -> generateCashflow(x,trade, valueDate))
printfn "IRS NPV %A" cashFlows
Unfortunately this doesn’t generate the same NPV for the trade.
I was expecting the function generateCashflow() to be called twice, one for each leg, in parallel, with the result of each function being summed into cashFlows. Maybe I’ve miss-understood what PSeq.sumBy should do? Or maybe I have a parallel execution bug.


This is a bug in F# PowerPack.PSeq module. I’ve already created an issue, you can vote for it here http://fsharppowerpack.codeplex.com/WorkItem/View.aspx?WorkItemId=4139.