The Fun of Programming Chapter 6 How to Write a Financial Contract by Simon Peyton Jones Roger L. Costello July 2011 1.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Mathematical Preliminaries
Using Matrices in Real Life
Advanced Piloting Cruise Plot.
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Conversion Problems 3.3.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Intel VTune Yukai Hong Department of Mathematics National Taiwan University July 24, 2008.
Evaluating Limits Analytically
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011.
ABC Technology Project
EU market situation for eggs and poultry Management Committee 20 October 2011.
EU Market Situation for Eggs and Poultry Management Committee 21 June 2012.
15-1 Copyright © 2013 Pearson Education, Inc. publishing as Prentice Hall Chapter 15 Money and Banking.
2 |SharePoint Saturday New York City
Green Eggs and Ham.
VOORBLAD.
R.USHA TGT ( MATHEMATICS) KV,GILL NAGAR CHENNAI.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Note to the teacher: Was 28. A. to B. you C. said D. on Note to the teacher: Make this slide correct answer be C and sound to be “said”. to said you on.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Slippery Slope
Chapter 5: Time Value of Money: The Basic Concepts
H to shape fully developed personality to shape fully developed personality for successful application in life for successful.
Januar MDMDFSSMDMDFSSS
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
A SMALL TRUTH TO MAKE LIFE 100%
PSSA Preparation.
Essential Cell Biology
1 Chapter 13 Nuclear Magnetic Resonance Spectroscopy.
Energy Generation in Mitochondria and Chlorplasts
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Presentation transcript:

The Fun of Programming Chapter 6 How to Write a Financial Contract by Simon Peyton Jones Roger L. Costello July

I am Reading this Book 2

Chapter 6 The author of Chapter 6 is Simon Peyton Jones. Chapter 6 shows how to create financial contracts by combining functions. Don't worry if you are not a financial person (I'm not). The important thing are the lessons learned. This chapter is exciting to me. It goes to the heart of how to write functional programs. It does this by showing a beautiful example. 3

Many kinds of contracts There are many different contracts: – zero-coupon discount bond: a person that acquires a zcb contract will receive x USD (or GPB, or some other currency) on a certain date. Example: receive 100 USD on July 20, 2011 – Swaps – Futures/Forwards – Options 4

Terminology: combinator A combinator is a function. 5

Lessons Learned Identify data types (e.g., Contract ). Identify a primitive set of combinators, each embodying a distinct piece of functionality. Identifying the right primitive combinators is crucial. Identifying the right primitive combinators is hard and requires a careful analysis of the domain. Postpone implementing the data types and the primitive combinators. Work at the abstract level. Create new combinators using the primitive combinators. And do so without depending on how the data types or primitive combinators are implemented. "Describe" contracts using the data types and the combinators. 6

Lessons Learned (cont.) Each primitive combinator should be embarrassingly simple and its implementation so simple that it is obviously correct. In mathematics one starts with a small set of axioms that are embarrassingly simple and everyone can readily agree that they are obviously true. They are then assembled in various ways to create new, indisputable truths (i.e., theorems). Similarly, in functional programming start with a small set of embarrassingly simple combinators which everyone can readily agree that each implementation is obviously correct. They are then composed in various ways to create new, indisputably correct combinators. 7

For me, a beautiful program is one that is so simple and elegant that it obviously has no mistakes, rather than merely having no obvious mistakes. 8 - Simon Peyton Jones

Data Types Create contracts using these data types: – Contract – Observable (i.e., Obs ): this is an observed, objective value Example: 91 is the temperature in Fahrenheit observed in Boston on July 20, 2011 at 4pm EST. This is an example of a numeric observable whose value is 91. Example: today's date is greater than January 1, This is an example of a Boolean observable whose value is True. – Currency – Date 9

Primitive Combinators zero :: Contract A contract with no value one :: Currency -> Contract A contract with a value of 1 USD or 1 GBP or … and :: Contract -> Contract -> Contract The result is a contract with two sub-contracts. If you acquire ( and c1 c2 ) then you get the value in both sub-contracts or :: Contract -> Contract -> Contract The result is a contract with two sub-contracts. If you acquire ( or c1 c2 ) then you must choose one of the sub-contracts 10

Primitive Combinators (cont.) cond :: Obs Bool -> Contract -> Contract -> Contract If you acquire ( cond b c1 c2 ) then you acquire c1 if b is True, and c2 otherwise. scale :: Obs Double -> Contract -> Contract If you acquire ( scale o c ) then you acquire a contract that has the value of c multiplied (scaled up) by the value of o. when :: Obs Bool -> Contract -> Contract If you acquire ( when o c ) then you acquire a contract whose value is zero until o is True. 11

Combinators on other data types The previous two slides showed the primitive combinators on Contract. Notice that only the function signatures are shown (plus a description). Postpone thinking about their implementation. The next slide shows the primitive combinators on observables ( Obs ). 12

Primitive combinators on Obs konst :: a -> Obs a ( konst x ) is an observable with value x date :: Obs Date This function returns a date as the value of an observable data type lift2 :: (a -> b -> c) -> Obs a -> Obs b -> Obs c ( lift2 f o1 o2 ) is the observable resulting from applying the function f to the observables o1 and o2 – Example: lift2 (==) date (konst "July 21, 2011") returns ( Obs True) if the value of date equals July 21, 2011 and ( Obs False) otherwise 13

Example: time-dependent contract Let's create a contract that attains its value only when the date is t. We will use the "when" combinator for this. Recall its signature: when :: Obs Bool -> Contract -> Contract We will need a function that takes as its argument a date t and returns (Obs True) if the current date equals t. Let's call this function "at": at :: Date -> Obs Bool 14

at The "at" function takes as its argument a date t and returns (Obs True) if t equals the current date, (Obs False) otherwise. We use konst to convert t to an observable. The date function returns the current date as an observable. We need to see if date equals ( konst t ). The result of the comparison must be a Boolean observable. We'll use lift2 for this. 15

at 16 at :: Date -> Obs Bool at t = lift2 (==) date (konst t)

Time-dependent contract (cont.) If you acquire ( when (at t) c ) then the contract you acquire has a value only at date t. 17 c1 = when (at t) c) c1 is a contract that attains its value only at date t.

How to Create Contracts The contract on the previous slide was created using the "at" combinator and the primitive "when" combinator. The "at" combinator was created using the primitive lift2, date, and konst combinators. Each contract was created without worrying about an implementation of the Contract data type, the Obs data type, the when combinator, the konst combinator, etc. Wow! 18

Zero-Coupon Discount Bond A zcb contract attains a value of x units of currency k at date t. Example: ( zcb "July 22, 2011" 100 USD ) is a contract that has a value of 100 USD on July 22, Here is its type signature: zcb :: Date -> Double -> Currency -> Contract 19

zcb 20 zcb :: Date -> Double -> Currency -> Contract zcb t x k = when (at t) (scale (konst x) (one k)) When the date is at t then the contract has a value of one unit of currency k, scaled up (multiplied) by x.

Remain Abstract Once again we see on the previous slide how contracts are created by composing primitive combinators and higher-level combinators (e.g., the "at" combinator). And notice that still we haven't concerned ourselves with an implementation of the Contract data type or any of the primitive combinators. 21

Many other contracts We can continue and create many other contracts by composing the combinators. 22

Last step After we are satisfied that the primitive combinators allow us to describe all the different types of contracts, then we implement the data types and the primitive combinators. The following slides show one possible implementation. 23

Contract 24 data Contract = Contract { currency :: Currency, payments :: Double } | SubContract { contracts :: [Contract] } deriving (Show) A Contract is either: a.One Contract that will pay out a value (payments) in a currency. b.Multiple sub-Contracts.

25 Obs data Obs a = Obs { value :: a } An observable has a value. The value may be of any type. Thus, there can be a Boolean observable ( Obs Bool), or a numeric observable ( Obs Double), etc.

26 Currency data Currency = GBP | USD deriving (Show) Enumerate the different currencies (I only show two; obviously there would be many more).

27 Date data Date = Date String deriving (Show, Eq) Construct a date from a string. Example: ( Date "July 22, 2011" )

28 one one :: Currency -> Contract one c = Contract { currency = c, payments = 1 } This function takes as its argument a currency c and returns a Contract that pays 1 unit in the currency.

29 scale scale :: Obs Double -> Contract -> Contract scale o c = Contract { currency = currency c, payments = payments c * value o } This function takes as its argument a numeric observable o, a Contract c, and returns a Contract that pays c 's payment multiplied by the value of o. In other words, it scales up c 's payment by the value of o.

30 zero zero :: Contract zero = SubContract { contracts = [] } This function returns a Contract that has no value.

31 when when :: Obs Bool -> Contract -> Contract when (Obs True) c1 = c1 when (Obs False) c1 = zero This function takes as arguments a Boolean observable and a Contract c1. If the value of the observable is True, it returns c1. If the value of the observable is False, it returns a zero Contract.

Other combinators I won't show the implementation of the other combinators: and, or, cond, anytime, until, etc. I leave that up to the reader. 32

Summary I am excited about the way Simon Peyton Jones writes programs. Here's what I learned: – Analyze the domain to identify a set of data types and a set of embarrassingly simple primitive combinators. – Compose the primitive combinators to create higher-level combinators. – Postpone implementing the data types and the primitive combinators. Work at the abstract level. 33

More Info Simon Peyton Jones has slides as well: us/um/people/simonpj/papers/financial- contracts/Options-ICFP.ppt us/um/people/simonpj/papers/financial- contracts/Options-ICFP.ppt 34