Evaluasi Pilihan Monte Carlo Clojure

Halo! Nama saya Roma, saya bekerja sebagai pengembang iOS di Exness. Dan selain itu, saya menulis di Clojure dan berinvestasi.


Hari ini saya akan berbicara tentang cara mengevaluasi opsi. Ini adalah artikel pengantar dan tidak mungkin Anda dapat menghasilkan jutaan menggunakan metode yang diusulkan. Namun, ini adalah dasar yang baik untuk memahami metode penilaian yang lebih kompleks.



Clojure


Clojure?


, , .


, Clojure JVM, , , Java.


, REPL. Clojure, Jupyter Notebook . IDE .



, , – . , - :


(defn call-option-value [security-price strike-price]
    (Math/max (- security-price strike-price) 0))

;; examples
(call-option-value 360.0 280.0) 
=> 80.0
(call-option-value 10.0 280.0) 
=> 0.0

, . , - .


: , , , .


Clojure :


(-> (get-possible-outcomes) mean present-value))

-, :


(-> (repeatedly n simulate-outcome) mean present-value)))

-


, . .


, :


(defn gbm-step [price dt rate volatility]
  (let [drift (* price rate dt)
        shock (* price volatility (Math/sqrt dt) (gaussian))
        change (+ drift shock)]
    (+ price change)))

;; next day price    
(gbm-step 1200 1/365 0.01 0.15)
=> 1207.554940519062

, , . .


, iterate .


Apple ($257) 100 :


(take 100 (iterate #(gbm-step % 1/365 0.01 0.15) 257))
=>
(257
 258.6727911540819
 256.91541924148663
 252.98034966342195
 251.1008036685261
 ...

10 , :



, , :


(defn simulate-outcome [price strike rate volatility expiration]
  (let [steps 100
        dt (/ expiration steps)
        prices (iterate #(gbm-step % dt rate volatility) price)
        price-at-expiration (last (take steps prices))]
    (call-option-value price-at-expiration strike)))

;; simulate 5 outcomes for one option
(repeatedly 5 #(simulate-outcome 1924 1925 0.01 0.45 0.5))
=> (0.0 730.6715047778875 329.1915857113486 0.0 0.0)

. , , , 1.


, :


(defn evaluate-call-option [& {:keys [security-price strike-price risk-free-rate volatility expiration]}]
  (let [expiration (year-fraction-until expiration)
        simulate-fn (partial simulate-outcome security-price strike-price risk-free-rate volatility expiration)
        n 1000]
    (-> (repeatedly n simulate-fn) mean (present-value risk-free-rate expiration))))

;; example    
(evaluate-call-option
  :security-price 1924
  :strike-price 1925
  :risk-free-rate 0.01
  :volatility 0.45
  :expiration (LocalDate/of 2020 4 17))
=> 74.66533445636996

Amazon 2100 2140 17 :


(for [strike (range 2100 2150 10)]
  (evaluate-call-option
     :security-price 1987
     :strike-price strike
     :risk-free-rate 0.01
     :volatility 0.35
     :expiration (LocalDate/of 2020 4 17)))
=> (23.9 21.1 16.4 15.5 15.3)

:


=> (22.9 20.6 18.35 16.4 14.6 13.6)

, .



. . , - cljfx .




All Articles