Technology

European Vanilla Call-Put Option Pricing ด้วย Python

2025-05-13 09:11:14


ทำไมต้องใช้ Python?

แม้ว่า Python จะเป็นที่รู้จักในฐานะภาษา scripting ที่ใช้เชื่อมต่อส่วนต่างๆ ของระบบซอฟต์แวร์ แต่ด้วยเครื่องมืออย่าง NumPy และ SciPy มันมีความสามารถเพียงพอในการใช้กำหนดราคาทางเลือก (Option Pricing) อย่างเต็มรูปแบบ

เหตุผลที่ควรเลือกใช้ Python มีดังนี้:

  • เขียนโค้ดน้อยกว่าภาษาอย่าง Java หรือ C++ อย่างมาก
  • มีไลบรารีที่ดีพร้อมใช้งานตามหลัก “batteries included”
  • เข้าใจง่าย ราวกับเป็น pseudo-code
  • สามารถเพิ่มประสิทธิภาพได้มากขึ้นเรื่อยๆ ด้วย NumPy, Numba, และ Cython


บทความชุดนี้จะเน้นที่ “ความเรียบง่ายก่อนการเพิ่มประสิทธิภาพ” ตามแนวคิดของ Daniel Duffy:

“ทำให้มันทำงานได้ก่อน จากนั้นทำให้มันถูกต้อง แล้วจึงค่อยปรับแต่งให้เร็ว”



สูตร Black-Scholes

สูตร Black-Scholes สำหรับคำนวณราคาของ European Vanilla Call Option มีดังนี้:

C=SN(d1​)−Ke−rTN(d2​)


โดยที่:

d1​=vT​ln(S/K)+(r+21​v2)T​,d2​=d1​−vT

  • S: ราคาสินทรัพย์อ้างอิง
  • K: ราคาใช้สิทธิ (Strike Price)
  • r: อัตราดอกเบี้ยไร้ความเสี่ยง
  • T: เวลาถึงวันหมดอายุ
  • v: ความผันผวนของราคาสินทรัพย์
  • N(x): ฟังก์ชัน CDF ของการแจกแจงแบบปกติมาตรฐาน


จาก Put-Call Parity เราสามารถคำนวณราคาของ Put Option ได้โดย:

P=Ke−rTN(−d2​)−SN(−d1​)



การเขียนฟังก์ชันทางสถิติด้วย Python

สร้างไฟล์ statistics.py แล้วใส่โค้ดดังนี้:

python

CopyEdit

from math import exp, log, pi
def norm_pdf(x):
    """
    Standard normal probability density function
    """
    return (1.0/((2*pi)**0.5)) * exp(-0.5 * x * x)
def norm_cdf(x):
    """
    Approximation to the cumulative distribution function for standard normal
    """
    k = 1.0 / (1.0 + 0.2316419 * abs(x))
    k_sum = k * (0.319381530 + k * (-0.356563782 + 
             k * (1.781477937 + k * (-1.821255978 + 1.330274429 * k))))
    result = 1.0 - (1.0 / ((2 * pi)**0.5)) * exp(-0.5 * x * x) * k_sum
    return result if x >= 0 else 1.0 - result



การคำนวณราคาด้วยสูตรปิด (Closed-Form)

สร้างไฟล์ใหม่ชื่อ closed_form.py แล้วใส่โค้ดนี้:

python

CopyEdit

from math import exp, log, sqrt
from statistics import norm_pdf, norm_cdf
def d_j(j, S, K, r, v, T):
    """
    Computes d1 (j=1) or d2 (j=2) used in Black-Scholes
    """
    return (log(S/K) + (r + ((-1)**(j-1))*0.5*v*v)*T) / (v * sqrt(T))
def vanilla_call_price(S, K, r, v, T):
    """
    Price of a European call option
    """
    return S * norm_cdf(d_j(1, S, K, r, v, T)) - \
           K * exp(-r*T) * norm_cdf(d_j(2, S, K, r, v, T))
def vanilla_put_price(S, K, r, v, T):
    """
    Price of a European put option
    """
    return -S * norm_cdf(-d_j(1, S, K, r, v, T)) + \
            K * exp(-r*T) * norm_cdf(-d_j(2, S, K, r, v, T))



ขั้นต่อไป

ขั้นตอนต่อไปคือการ ตรวจสอบผลลัพธ์ ว่าสูตรเหล่านี้ให้ราคาที่ถูกต้องและเป็นไปตามเงื่อนไข เช่น Put-Call Parity



อ้างอิง : European Vanilla Call-Put Option Pricing with Python

จาก https://www.quantstart.com/articles/European-Vanilla-Call-Put-Option-Pricing-with-Python/

ร่วมเเสดงความคิดเห็น :