Python中蒙特卡罗期权定价的类实现

时间:2016-12-02 00:13:51

标签: python-2.7 class montecarlo

我是一个试图自学Python的数学家。通过这里的一些指示,我有这个程序工作(没有类实现),但作为一个数学家,我的代码是丑陋的。我希望使用类来帮助它看起来更干净。

但我觉得我对如何让一个班级流入另一个班级感到困惑。或者至少将功能放入其他功能中。

无论如何,我已经阅读了很多材料,并且这部分工作得很好:

#-----------------------------------------------
# Plain vanilla european option pricer 
# by DudeWah
#-----------------------------------------------

#import libraries
import time as walltime
from random import gauss
from math import exp, sqrt

#-----------------------------------------------
#Class for all parameters
#-----------------------------------------------

class parameters():
    """parameters to be used for program"""

    #--------------------------
    def __init__(self):
        """initializa parameters"""

        self.c_or_p = "call"
        self.S = 857.29 #underlying asset price
        self.v = 0.2076 #volatility
        self.r = 0.0014 #rf treasury rate
        self.T = 18/ 365.0 #days till maturity
        self.K = 860.00 #strike rate
        self.simulations = 10000000 #no. of simulations

    #---------------------------
    def print_parameters(self):
        """print parameters"""

        print "---------------------------------------------"
        print "---------------------------------------------"
        print "Pricing a:", self.c_or_p.upper()
        print "---------------------------------------------"
        print "Parameters of Monte Carlo Vanilla Call Pricer"
        print "Underlying Asset Price = ", self.S
        print "Volatility = ", self.v
        print "Risk-Free 10 Year Treasury Rate =", self.r
        print "Days Until Expiration = ", self.T
        print "Number of Simulations = ", self.simulations
        print "---------------------------------------------"
        print "---------------------------------------------"

这部分是个问题。我不知道我在做什么。如果有人关心,我可以提供算法。

#-----------------------------------------------
#Class for Monte Carlo
#-----------------------------------------------    
class vanilla_option_mc():
    def __init__(self):
        self.payoffs = []
        self.discount_factor = exp(-prm.r * prm.T)

    def gen_asset_price(self):
    #def gen_asset_price(S,v,r,T):
        """One of two main functions to be used. This will 
        generate the price of the security. This function
        will be used repetitively inside the for loop 
        later on during the actual Monte Carlo simulation"""

        self.asset_price = prm.S * exp((prm.r - 0.5 * prm.v**2) * prm.T 
                      + prm.v * sqrt(prm.T) * gauss(0,1.0))

        return self.asset_price


    def call_payoff(self):
        """use to price a call"""
        self.cp = max(self.call_S - self.call_K, 0.0)
        return self.cp

    def put_payoff(self):
        """Use to price a put"""
        self.pp = max(prm.K - prm.S, 0.0)
        return self.pp

    def calculate_payoff_vector(self):
        """main iterative loop to run the 
        Monte Carlo simulation. Returns a vector
        of different potential payoffs""" 

        self.count = 0
        for i in xrange(simulations):
            self.S_T = gen_asset_price()
            if self.c_or_p == "call":
                self.payoffs.append(call_payoff(self.S_T,prm.K))
            elif self.c_or_p == "put":
                self.payoffs.append(put_payoff(self.S_T,prm.K))
        self.count += 1

        return self.payoffs    

    def compute_price(self):
        """Uses payoff vector and discount
        factor to compute the price of the
        option"""
        self.V_i = [x*self.discount_factor for x in self.payoffs]
        self.price = sum(self.V_i) / float(prm.simulations)

    def print_price(self):
        """prints the option price to terminal"""
        print str(prm.c_or_p.upper()),"Price: %.4f" % self.price        

    def calc_statistics():
        """uses payoffs and price to calc
        variance, standard deviation, and
        a 95% confidence interval for price"""
        self.V_i_minus_price_squared = [(x - self.price)**2 for x in self.V_i]
        self.variance = sum(self.V_i_minus_price_squared) /float(prm.simulations-1)
        self.sd = sqrt(self.variance)
        self.CI = [self.price - (1.96*sd/sqrt(float(prm.simulations))),
                   self.price + (1.96*sd/sqrt(float(prm.simulations)))]


    def print_statistics():
        print "Variance: %.4f" % self.variance
        print "Standard Deviation: %.4f" % self.sd
        print "95% Confidence Interval:", self.CI


prm = parameters()
prm.print_parameters()

我已经在学习曲线中遇到了问题,我现在可以很好地工作了很多东西。但是,就深度课程而言,以及可读性和干净的代码而言,我缺乏。

我想改进这些事情。

提前致谢。

0 个答案:

没有答案