Thanks Don,
I read some parts of the paper again, it turns out, on page 7 they mention that 'Observables' are very similar to "Fran's behaviours" from "Functional reactive animation" by Elliot and Hudak (1997). That paper didn't have what I wanted, but another paper, "Functional Reactive programming from First Principles" by Wan and Hudak (2000) has the following definition on page four:

1
type Behavior a = [Time] -> ['a'] //without quotes, this forum inserts a smiley if i don't put in the quotes

so I wrote the following in F# (after consulting #ocaml on irc):

1
type 'a obs = date list -> 'a list  //definition of Observable

Following is my code now (there are several syntax errors, and I haven't touched the function to actually value these instruments, but here goes):
----------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// How to write a financial contract (Peyton Jones and Eber)
// Shahbaz Chaudhary
// Mon Aug 07, 2006

//Mon Aug 9, 2006: changed the observable definition after advice from Don Syme
// and "Functional Reactive programming from First Principles" by Wan and Hudak (2000)
//blah blah
    
type date = System.DateTime
type currency = string //This should perhaps be an enumertion of all currency codes rather than a raw string


//Contract definition
type Contract = 
  | Zero
  | One of currency
  | Give of Contract
  | And of Contract * Contract
  | Or of Contract * Contract
  | Cond of bool obs * Contract * Contract
  | Scale of float obs * Contract
  | When of bool obs * Contract
  | Anytime of bool obs * Contract
  | Until of bool obs * Contract


//Observable definition
type 'a obs = date list -> 'a list

//Observable primitives
let konst a = a obs 
let lift f o = f o //how does it know that 'o' is an observable? type it explicitly?
let lift2 f o1 o2 = f o1 o2 //same type problem
let date = date obs 


//Random Variable
type 'a rv = 'a list //is this right? random variable is a list of possible value of type 'a?
//Value Process
type 'a pr = date-> 'a rv

//Process primitives
let k x = fun dt -> x  //for any date value, 'x' is returned...how does it know that 'fun dt ...' is actually a value process ('a pr)?
let processdate = date pr //?
let cond b pr1 pr2 = if b then pr1 else pr2 //type
//for lift and lift2 of process, use the same functions as observable primitives?
let (+) pr1 pr2 = pr1 + pr2 //*, /, >, <, =, ...

  
let x = "USD" //how does it know that "USD" is actually of type currency? type it explicitly?
let c1 = Give Zero
By on 8/9/2006 7:48 PM ()

If any one is still following this thread, I cleaned up the existing code a little. I've been trying to get through some Fran and FRP papers to understand this stuff better. The book "Haskell School of Expression" has a good chapter on reactive animation, it was fairly helpful. The lift functions for 'process' are not getting typed correctly. The code follows:
------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
type date =Date of System.DateTime
type currency = string //This should perhaps be an enumertion of all currency codes rather than a raw string

//Observable definition
type 'a obs = Obs of (date -> 'a)

//Contract definition
type Contract = 
  | Zero
  | One of currency
  | Give of Contract
  | And of Contract * Contract
  | Or of Contract * Contract
  | Cond of bool obs * Contract * Contract
  | Scale of float obs * Contract
  | When of bool obs * Contract
  | Anytime of bool obs * Contract
  | Until of bool obs * Contract


//Observable primitives
let konst a = Obs (fun t -> a)
let lift1 f (Obs o) = Obs (fun t-> f (o t))
let lift2 f (Obs o1) (Obs o2) =Obs (fun t-> f (o1 t) (o2 t)) 
let date =Obs (fun t-> t) 


//Random Variable
type 'a rv =RV of 'a list //is this right? random variable is a list of possible value of type 'a?
//Value Process
type 'a pr =PR of (date-> 'a rv)

//Process primitives
let k x = PR (fun t -> RV [x]) 
let procdate = PR (fun t ->RV [t])
let proclift1 (f:'a->'b) (PR a) = PR (fun t-> f (a t)) ///from this point on the code doesn't seem to work, 'f' wants to operate on 'a rv instead of primitives values
let proclift2 f (PR a) (PR b) = PR (fun t-> f (a t) (b t))
let proclift3 f (PR a) (PR b) (PR c) = PR (fun t-> f (a t) (b t) (c t))
let procif b a c = if(b) then a else c
//let procif2 = proclift3 procif
let a =k 1
let b = a 1
let c = List.map (fun x -> x+1) a

let x = "USD" //how does it know that "USD" is actually of type currency? type it explicitly?
let c1 = Give Zero
By on 8/18/2006 3:14 PM ()

Hi,

For what it's worth, an (extended?) OCaml port of the said paper was developped, see [link:www.lexifi.com]

[link:www.lexifi.com] is the most comprehensive file from the resources of the site ( [link:www.lexifi.com] ) although there are some other presentations.

Hopefully it can give you some ideas.

By on 8/10/2006 3:34 AM ()

Julien,
Thanks for the link. The material on their site is mostly marketing, that is to say nothing technical (and since I am using the "Contracts" paper as a learning excercise, I need a lot more detail :) ).

By the way, if it isn't already obvious, lexifi doesn't need to worry about competition from the few lines of code I post here :)

By on 8/18/2006 3:40 PM ()

Hi guys,

Normally, I would have hopped in on this one, but things have been a bit crazy. Let me take another look at this thread. I agree with Don, i.e. there is a good topic here and F# works based on this topic would provide a great example for others.

I can't promise time to contribute code right at the moment, but I will at least offer a few ideas.

I've seen a few opinions on these ideas and the corresponding research and haven't had time to become informed.

---O

By on 8/18/2006 4:15 PM ()

Hi all,

here's my 2 pence contribution . I let aside the random variables & Stochastic process for the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 

open System

type date =Date of System.DateTime
type currency = USD
|EUR
|JPY 
|GBP //currency is maybe better with be an enumeration of all currency codes rather than a raw string
//Observable definition
type 'a obs = Obs of (date -> 'a)

//Contract definition (I just added some of the other "combinators" from the contract-icfp.ps working paper)

type Contract = 
| Zero
| One of currency
| Give of Contract
| And of Contract * Contract
| Or of Contract * Contract
| Truncate of date * Contract 
| Then of Contract * Contract 
| Scale of float obs * Contract 
| Get of Contract
| Anytime of Contract 
| Cond of bool obs * Contract * Contract 
| When of bool obs * Contract
| Until of bool obs * Contract

//Observable primitives

let konst a = Obs (fun t -> a)
let lift1 f (Obs o) = Obs (fun t-> f (o t))
let lift2 f (Obs o1) (Obs o2) =Obs (fun t-> f (o1 t) (o2 t)) 
let add_obs = lift2 (+.) ;; //Addition on float Obs
let minus_obs = lift2 (-.)
let mult_obs = lift2 ( *.) 
let div_obs = lift2 ( /.) 
let date = Obs (fun t-> t) 
let TempInParis = Obs(fun t -> 9.7);; // first order approx :-)
let TempInParisinKelvin = add_obs TempInParis (konst 273.);;
let scaleK x c = Scale ((konst x), c)
let zcb t x k = scaleK x (Get(Truncate(t,(One k)))) //Zero Coupon Bond
let perhaps t u = Truncate(t , (Or(u,Zero)))
let european t u = Get(perhaps t u) //European option
let american (t1,t2) u = 
 let opt = Anytime(perhaps t2 u) in 
 Then(Get(Truncate(t1,opt)),opt)

let oneBuck = One USD;;
let hundredBucks = Scale((konst 100.),oneBuck);;
let GiltStrip = zcb (Date(new System.DateTime(2007,5,10))) 100. GBP;;

Regards

Julien

By on 8/21/2006 2:52 PM ()

Thanks Julien,
I'm glad to see I wasn't wildly off the mark. Following are a few more lines of code starting at the "from this point on the code doesn't seem to work..." comment:

1
2
3
4
5
6
7
8
9
let proclift1 f (PR a) = PR (fun t-> List.map f (a t)) 
let proclift2 f (PR a) (PR b) = PR (fun t->List.map2 f (a t) (b t))
let proclift3 f (PR a) (PR b) (PR c) = PR (fun t->map3 f (a t) (b t) (c t))
let procif b a c = if (b) then a else c  //won't this evaluate both 'a' AND 'c' due to eager evaluation???
let procif2 b a c = proclift3 procif b a c 

//Additional process primitives (dependent on valuation model)
let exch currencyTo currencyFrom = currencyTo * currencyFrom //garbage for now
By on 8/21/2006 6:33 PM ()

A few thoughts : shouldn't an observable be more general : a constant observable doesn't depend on time :

1
let (f : Observable<DateTime -> int>) = fun time -> time.DayOfWeek 

seems a bit more logical than having to define your constant as

1
let (foo : Observable<int>) = (fun _ : DateTime)-> 12345

An idea from the lexifi presentation I mentioned is the use of the Either combinator :

1
2
3
type Contract = 
 | ...
 | Either of list<Contract> (* of which Or is an abbreviation *)

The American option definition (from the icfp paper) is odd. Aside from the date of acquisition, there are no two dates in an american option definition. Unless they were thinking of the acquisition date, but then : why not in the European case? Not sure to get their idea here... An American option is an option that you can exercise anytime between the acquisition date and the expiration date. So the first definition of the paper : Anytime (perhaps expDate payoff) ought to be right....

In his paper, Eber mentions Acquire : bool observable -> Contract -> Contract as a function that returns a contract as soon as the bool observable is true. It seems like a good idea. Rather than using get(truncate t1 c), you'd have acquire (t=t1) c, or something in this vein... More straightforward, and more human-readable.

For the american option (I suppose) you would get :

1
2
let americanOption acqDate expirationDate payoffFunction = 
  acquire {acqDate} (Anytime (perhaps expDate payoff))

As for exchange rates, couldn't we have something like ?

1
let exchange_rate cur1 cur2 = And (one c1, give (one c2))

julien.

By on 8/22/2006 10:21 AM ()

Hi falcon,

I think it would be excellent to work through a high-quality implementation of this work in F#.

I took a brief look at the paper today and the datatype approach seems the right one for the "Contract" type. I'm not so sure if that's the case for "Observable" - that feels more like it would be best to prototype with that being a concrete type.

In general, wherever Haskell type classes are used you will probably need to pass around dictionaries of operations explicitly (where dictionaries of operations are represented by records of function values), or else make the types concrete. For example, either make "Observable" a concrete type (some representation of the time-varying data whichb allows you to implement Lift, Lift2, Date etc., e.g. time series data). Later you can pass around a dictionary of operations that manipulates abstract Observable values, though in any particular application your representation of observables will probably be fixed.

I'd be glad to take a longer look with you tomorrow - and perhaps Julien and optionsScalper would like to join in ? :-)

don

By on 8/9/2006 5:12 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper