Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# applying function to two lists at once

I'm just jumping into F# on my own, doing a translation of a simple financial tool as an exercise. And I'm still waiting for reference books in the mail. Here's something I imagine F# can do in probably more than one clever clever way, but I can't get past syntax errors.

I have a function called Delta for options valuation, and I want to apply it--over two (parallel) lists of input values:

let Delta f k v t r = exp(-r * t) * CDF(D1 f k v t)

let priceList = [100.0; 98.0; 102.0; 100.0]  //f
let strike = 100.0                           //k
let vol = 0.30                               //v
let timeList = [1.0; 0.9; 0.8; 0.7]          //t
let rt = 0.005                               //r

So this function is part of a recursive process (using Newton's Method). I would be happy producing a list of deltas of the same length [d1; d2; d3; d4] [edited: I'm sticking to the list of deltas as desired result]. I'm looking for the clever F# way--in one fell swoop--with list functions. I've tried to shoehorn this Delta function into this basic pattern:

  let sumList = List.map2 (fun x y -> x + y) list1 list2

(It seems like many of the list functions could get me a Delta list or a sum of Delta list.) I'm not coming close to guessing legal syntax. I've also tried the "Match head :: tail" pattern too.

EDIT: I waffled here on what I am asking for because I'm swimming a bit in all the List. functions that are new to me. Let me focus myself: Can I apply (Delta f k v t r) as (Delta priceList strike vol timeList rt) and produce deltaList, with prices, times and deltas all being the same length?

like image 522
RomnieEE Avatar asked Feb 22 '26 03:02

RomnieEE


1 Answers

As others pointed out in the comment, your question is not entirely clear - if you gave a concrete example of input and output you want to get, it would be easier to answer.

That said, if you want to call Delta with the various values as parameters, then there are two things you need to do. Most parameters are just plain values, so those you can pass directly; priceList and timeList are collections, so I assume you want to calculate Delta for all its elements. To do that, you can use Seq.zip and Seq.map:

Seq.zip priceList timeList 
|> Seq.map (fun (f, t) -> Delta f strike vol t rt)
like image 166
Tomas Petricek Avatar answered Feb 24 '26 14:02

Tomas Petricek