SHOW ME THE CODE !

Bastian Gross

German Mathquant
Hey Andy,

Matlab GUI-application-code get very out of hand ;).

Nevertheless I'll code some implied vola and other calibration GUI-applications.
 

kean

Mathematics Student
Andy, I would like to get your view on C++ and C# apparently related to quant modeling in US.
I hope that you can share. You're right, C# is just too easy to convert to VB.net.
 

Andy Nguyen

Member
My opinion is that I'll learn whatever the language is required to get/keep the job. C++ has been the meal ticket for this industry for years. I learned C++ as undergrad, Baruch teaches C++ in its MFE program so that's what I've been dealing with until now.
I got to play with C# just because I choose to do so at work. I'm curious and want to see what the fuss is all about.
I don't have any personal preference over C++ or C#. If tomorrow people higher up tell me to forget about C++, C# and do everything in X%$12+ language, then that's I'll do. I have no love hate whatsoever about a language. It's just a tool I need to learn to do my job, the more the better.
When you go to all the big boards and hear all the big guys fighting over every little details of C++ vs C# vs Java vs OCaml vs X%12+, my guess is that those guys have been using one language for so long that they develop a love/hate/passion for a certain language. They are in a position to know every little detail about a language that they can fight till the end for its supremacy.

I'm not there yet so until then I'll just use whatever available/ I feel like at the moment. The training at Baruch is heavy in C++ but it prepared me to take on other languages without much difficulty.
 

Andy Nguyen

Member
Updated the first post with new code.

I added a pricer for option on a futures contract, a VAR calculator to the code base over the weekend. If someone wants to take the code base to add more function to it, be my guest. These are standard, basic stuff that you will code in an MFE program.
I'm planning to add a pricer for bond with embedded option, CDS pricer to it but only when i have nothing else to do.

If you don't have Visual Studio, just download the zip file, unzip it, and run the .exe file under the /bin folder.
 

Andy Nguyen

Member
This is the code to benchmark your computer
Mathematica
-----------------------------------------------------------------
M = Table[Random[], {i, 1, 300}, {j, 1, 300}];
t = 0.25; T = 2;
Timing[Inverse[M].(MatrixExp[-M t] - MatrixExp[-M T]);]
{0.188, Null}
--------------------------------------------------------------------

In Matlab, here is the same code:


Matlab

------------------------------------------------------------
M = rand(300,300);
t=0.25; T=2;
tic; inv(M) * (expm(-t*M) - expm(-T*M)); toc;
--------------------------------------------------------------

If you have a fast computer, you should beat 0.188 second it took for the Mathematica. Matlab is usually faster.
My T60 laptop took 0.6 second to run in Matlab 7.6
 

Andy Nguyen

Member
Blackscholes pricer in OCaml

Download OCaml and play with it a bit today. I feel very left handed using this functional language coming from C++/C# so it's kinda interesting. Here is the European option pricer using BS. I kind of like how the variable type is handled automatically and the small footprint of the code. I'm not too crazy about tracking all the . in the operator. It takes only 20 minutes to download/install and get something to run so if anyone has time, play with it a bit.
Code:
(* blackscholes.ml - An European option pricer using Black Scholes formula
     *
     * Written by Andy Nguyen
     * Tested on OCaml 3.10.0
     *)
open Printf

    let cnd x =
        let pi = 4.0 *. atan 1.0 in
        let a1 = 0.31938153 in
        let a2 = -0.356563782 in
        let a3 = 1.781477937 in
        let a4 = -1.821255978 in
        let a5 = 1.330274429 in
        let m = abs_float x in     
        let k = 1.0 /. (1.0 +. 0.2316419 *. m) in
        let dCND = 1.0 -. 1.0 /. sqrt(2.0 *. pi) *. exp(-1.0 *. m *. m /. 2.0) *. 
        (a1 *. k +. a2 *. k ** 2.0 +. a3 *. k ** 3.0 +. a4 *. k ** 4.0 +. a5 *. k ** 5.0) in
        if x < 0.0 then 1.0 -. dCND else dCND;;
           
     let bls_call price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
           let d2 = d1 -. vol *. sqrt(time) in
           let call = price *. exp(-1.0 *. yield *. time) *. cnd(d1) -. strike *. exp(-1.0 *. rate *. time) *. cnd(d2) in
      call;;
          
     let bls_put price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
           let d2 = d1 -. vol *. sqrt(time) in
           let put = strike *. exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d2) -. price *. exp(-1.0 *. yield *. time) *. cnd(-1.0 *. d1) in
      put;; 
      
    printf "%f\n" (bls_call 12.0 16.0 0.13 1.0 0.3 0.05 );;
    printf "%f\n" (bls_put 12.0 16.0 0.13 1.0 0.3 0.05);;
Save the code above to a file called blackscholes.ml. To run it from the interactive mode, type
Code:
#use "C:/blackscholes.ml";;
Here is the compiling and output
Code:
#use "C:/blackscholes.ml";;
val cnd : float -> float = <fun>
val bls_call : float -> float -> float -> float -> float -> float -> float =
  <fun>
val bls_put : float -> float -> float -> float -> float -> float -> float =
  <fun>
0.546486
- : unit = ()
3.181260
- : unit = ()
 

alain

Older and Wiser
Andy, I'm happy that you are coming to join us on the dark side. Take a look at Okasaki's book about functional data structures. That will change your mind completely
 

Andy Nguyen

Member
Just to make things complete. Here is the BS pricer with all the greeks in OCaml.
Now somebody needs to show us how to do this in F#
Code:
(* blackscholes.ml - An European option pricer using Black Scholes formula
     *
     * Written by Andy Nguyen
     * Tested on OCaml 3.10.0
     *)

    let pi = 4.0 *. atan 1.0;;
    
    
    let cnd x =
        
        let a1 = 0.31938153 in
        let a2 = -0.356563782 in
        let a3 = 1.781477937 in
        let a4 = -1.821255978 in
        let a5 = 1.330274429 in
        let m = abs_float x in     
        let k = 1.0 /. (1.0 +. 0.2316419 *. m) in
        let dCND = 1.0 -. 1.0 /. sqrt(2.0 *. pi) *. exp(-1.0 *. m *. m /. 2.0) *. 
        (a1 *. k +. a2 *. k ** 2.0 +. a3 *. k ** 3.0 +. a4 *. k ** 4.0 +. a5 *. k ** 5.0) in
        if x < 0.0 then 1.0 -. dCND else dCND;;
           
           
     let phi x =
             exp(-1.0 *. x *. x /. 2.0) /.  sqrt(2.0 *. pi);;
             
     let bls_call price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
           let d2 = d1 -. vol *. sqrt(time) in
           price *. exp(-1.0 *. yield *. time) *. cnd(d1) -. strike *. exp(-1.0 *. rate *. time) *. cnd(d2);;
      
     
     let bls_put price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
           let d2 = d1 -. vol *. sqrt(time) in
           strike *. exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d2) -. price *. exp(-1.0 *. yield *. time) *. cnd(-1.0 *. d1);; 
           
           
     let blsdeltaCall price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in   
           exp(-1.0 *. yield *. time) *. cnd(d1);;
        
              
     let blsdeltaPut price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in   
           exp(-1.0 *. yield *. time) *. cnd(d1) -. 1.0;;
              
     let blsgamma price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in   
           exp(-1.0 *. yield *. time) *. phi(d1) /. (price *. vol *. sqrt(time));;
           
      
     let blsvega price strike rate time vol yield =
           let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in  
           price *. exp(-1.0 *. yield *. time) *. phi(d1) *. sqrt(time);;
              

     let blsthetaCall price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          -1.0 *. exp(-1.0 *. yield *. time) *. (price *. phi(d1) *. vol /. (2.0 *. sqrt(time))) -. rate *. strike *. exp(-1.0 *. rate *. time) *. cnd(d2) +. yield *. price *. exp(-1.0 *. yield *. time) *. cnd(d1);;
              
      
     let blsthetaPut price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          -1.0 *. exp(-1.0 *. yield *. time) *. (price *. phi(d1) *. vol /. (2.0 *. sqrt(time))) +. rate *. strike *. exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d2) -. yield *. price *. exp(-1.0 *. yield *. time) *. cnd(-1.0 *. d1);;
              
     let blsrhoCall price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in 
          strike *. time *. exp(-1.0 *. rate *. time) *. cnd(d2);;
              

     let blsrhoPut price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
           -1.0 *. strike *. time *. exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d2);;
              
      
     let blsvolga price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          price *. exp(-1.0 *. yield *. time) *. phi(d1) *. sqrt(time) *. d1 *. d2 /. vol;;
      
              
     let blsvanna price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          -1.0 *. exp(-1.0 *. yield *. time) *. phi(d1) *. d2 /. vol;;
          
          
     let blscharmCall price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          -1.0 *. yield *. exp(-1.0 *. yield *. time) *. cnd(d1) +. exp(-1.0 *. yield *. time) *. phi(d1) *. (2.0 *. (rate -. yield) *. time -. d2 *. vol *. sqrt(time)) /. (2.0 *. time *. vol *. sqrt(time));;
         
         
     let blscharmPut price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          yield *. exp(-1.0 *. yield *. time) *. cnd(-1.0 *. d1) -. exp(-1.0 *. yield *. time) *. phi(d1) *. (2.0 *. (rate -. yield) *. time -. d2 *. vol *. sqrt(time)) /. (2.0 *. time *. vol *. sqrt(time));;
     
     
     let blscolor price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          -1.0 *. exp(-1.0 *. yield *. time) *. (phi(d1) /. (2.0 *. price *. time *. vol *. sqrt(time))) *. (2.0 *. yield *. time +. 1.0 +. (2.0 *. (rate -. yield) *. time -. d2 *. vol *. sqrt(time)) *. d1 /. (2.0 *. time *. vol *. sqrt(time)));;
          
          
                 
     let blsdualdeltaCall price strike rate time vol yield =
      let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
      let d2 = d1 -. vol *. sqrt(time) in
       -1.0 *. exp(-1.0 *. rate *. time) *. cnd(d2);;


     let blsdualdeltaPut price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d2);;
     
     
     let blsdualgamma price strike rate time vol yield =
          let d1 = (log(price /. strike) +. (rate -. yield +. vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          exp(-1.0 *. rate *. time) *. phi(d2) /. (strike *. vol *. sqrt(time));;
     
    
     let blsFutureCall price strike rate time vol =
          let d1 = (log(price /. strike) +. (vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          price *. exp(-1.0 *. rate *. time) *. cnd(d1) -. strike *. exp(-1.0 *. rate *. time) *. cnd(d2);;
      
                   
     let blsFuturePut price strike rate time vol  =
          let d1 = (log(price /. strike) +. (vol *. vol /. 2.0) *. time) /. (vol *. sqrt(time)) in
          let d2 = d1 -. vol *. sqrt(time) in
          strike *. exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d2) -. price *. exp(-1.0 *. rate *. time) *. cnd(-1.0 *. d1);;
 

Andy Nguyen

Member
I played with F# a little bit and except for a few small wrinkles, the code i wrote in OCaml compiled and worked fine in F#.
My impression with F# is that it feels like an updated/modern version of OCaml. It's like M$ took OCaml, fixed the annoying things in it and put out a bunch of development tools for it. Granted I don't have years of experience on these two languages but as someone who used it briefly, F# seems easier for me to use.

In OCaml, it was OK to name your variable yield
In F#, it's reserved.
In OCaml, I have to write -1. *. x to indicate negative variable
In F#, I can just write -x which is what I expect coming from other languages.

I guess this concludes my journey with functional languages.
 

ExSan

Active Member
Hi Quants
Attached ExSan, there may be some of you who would find it useful, hopefully:)
The BlackSholes eq. can be access through the following menu sequence
greetings to all of you


Main Menu: 1 ExSan
EXSAN MENU: 6 Quant Finance Room
QUANT MENU: b BlackSholes

Price of the Stock St -> 12
exercise price K (strike) -> 16
Vol (percent) -> 0.3
risk-free interest rate r -> 0.13
time to expiration (weeks) -> 52
N(d1) = 0.353604 N(d2) = 0.249645
d1 = -0.375607 d2 = -0.675607

CALL----> 0.73586 PUT ----> 2.78539
 

Attachments

alain

Older and Wiser
ExSan, Could you post source code instead of executables? You might have planted a virus in our site with that program so it is better to be safe.
 

ExSan

Active Member
My two cents contribution
RANDOM NUMBERS generator.
Extract attached ExSan to your desktop and execute it

MAIN MENU: 1 Exsan
rows = 10 cols = 10
EXSAN MENU: 1 Generate Random Numbers
RANDOM NUMBERS MENU: 5 Gamma
alpha ---> 2 beta ---> 3
 

Attachments

alain

Older and Wiser
there is no help if you don't post the source code. We would learn more if you post the source code.
 
Top