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.

About these ads

~ by mdavey on March 11, 2010.

One Response to “F#: PSeq.sumBy, What’s Gone Wrong?”

  1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 666 other followers

%d bloggers like this: