在Python中使用二叉树计算股票期权价格

时间:2018-07-11 12:35:28

标签: python pandas numpy

我正在尝试计算期权的价格,下面的代码基于我在其中一个线程中找到的文本。我现在想可视化二叉树,以便在每个节点上显示以下内容:

1)股票价格
2)我们从头开始追溯的期权价格,即欧洲期权的收益
3)提早进行锻炼(即美式期权

)的回报

代码正确计算了值,但是在视觉上显示相同值时遇到了挑战。我曾想将numpy数组转换为df,但结果却很奇怪。

有人可以帮忙吗?

这设置了计算价格的关键参数

""" Store common attributes of a stock option """
import math
class StockOption(object):
    def __init__(self, S0, K, r, T, N, params):
    self.S0 = S0
    self.K = K
    self.r = r
    self.T = T
    self.N = max(1, N) # Ensure N have at least 1 time step
    self.STs = None # Declare the stock prices tree

    """ Optional parameters used by derived classes """
    self.pu = params.get("pu", 0) # Probability of up state
    self.pd = params.get("pd", 0) # Probability of down state
    self.div = params.get("div", 0) # Dividend yield
    self.sigma = params.get("sigma", 0) # Volatility
    self.is_call = params.get("is_call", True) # Call or put
    self.is_european = params.get("is_eu", True) # Eu or Am

    """ Computed values """
    self.dt = T/float(N) # Single time step, in years
    self.df = math.exp(-(r-self.div) * self.dt) # Discount factor

这是演练的核心,在此过程中,将计算所有期货股票价格,然后通过回溯计算收益

""" Price a European or American option by the binomial tree """
from StockOption1 import StockOption
import math
import numpy as np
import pandas as pd


class BinomialTreeOption(StockOption):
    def _setup_parameters_(self):
        self.u = 1 + self.pu # Expected value in the up state
        self.d = 1 - self.pd # Expected value in the down state
        self.qu = (math.exp((self.r-self.div)*self.dt) -
               self.d)/(self.u-self.d)
        self.qd = 1-self.qu



    def payoff(self):
        self._setup_parameters_()

        self.STs = [np.array([self.S0])]
        for i in range(self.N):
            prev_branches = self.STs[-1]

            st = np.concatenate((prev_branches*self.u,
                             [prev_branches[-1]*self.d])) 
            self.STs.append(st)




## Compute payoff at the last node
        payoffs = np.maximum(0, (self.STs[self.N]-self.K) if self.is_call
                          else (self.K-self.STs[self.N]))





        for i in reversed(range(self.N)):
            # The payoffs from NOT exercising the option
            payoffs = (payoffs[:-1] * self.qu +
                   payoffs[1:] * self.qd) * self.df



            # Payoffs from exercising, for American options
            if not self.is_european:
                early_exer_payoff = \
                        (self.STs[i] - self.K) if self.is_call \
                        else (self.K - self.STs[i])

                payoffs = np.maximum(payoffs, early_exer_payoff)


        return payoffs[0]


""" Price an option by the binomial CRR model """
from BinomialAmericanOption1 import BinomialTreeOption
import math

class BinomialCRROption(BinomialTreeOption):
    def _setup_parameters_(self):
        self.u = math.exp(self.sigma * math.sqrt(self.dt))
        self.d = 1./self.u
        self.qu = (math.exp((self.r-self.div)*self.dt)-
                   self.d)/(self.u-self.d)
        self.qd = 1-self.qu



#### Option price
from StockOption1 import StockOption
from BinomialAmericanOption1 import BinomialTreeOption
from BinomialCRROption1 import BinomialCRROption


crr_amoption= BinomialCRROption(50, 60, 0.05, 4, 4,{"sigma": 0.3,
                                               "is_call": True,
                                               "is_eu": False})


print(crr_amoption.payoff())

0 个答案:

没有答案