示例#1
0
def tn_pdf(x, loc, scale, lower=0, upper=1):
    if upper == np.inf:
        Z = 1 - norm._cdf(lower-loc/scale)
    else:
        Z = norm._cdf((upper-loc)/scale) - norm._cdf((lower-loc)/scale)
    pdf_psi = norm._pdf((x - loc) / scale)
    return pdf_psi / (scale * Z)
示例#2
0
    def evaluate(self, t, lamda):
        # if not np.all(self._check_args(lamda)):
        #    raise ValueError('invalid args')

        term = np.log((1. - t) / t) * 0.5 / lamda

        from scipy.stats import norm
        # use special if I want to avoid stats import
        transf = ((1 - t) * norm._cdf(lamda + term) +
                  t * norm._cdf(lamda - term))
        return transf
示例#3
0
def implied_vol_P(S, K, t, T, q, r, pTrue):
    tol = 1e-8
    sigmaHat = initial_guess(S, K, t, T, q, r)
    sigmaA, sigmaB = sigmaHat, sigmaHat
    sigmaDiff = 1.0
    n = 1
    nMax = 1000
    while sigmaDiff >= tol and n < nMax:
        if n==1:
            p = put_black_scholes(S, K, t, T, sigmaHat, q, r)
        else:
            p = put_black_scholes(S, K, t, T, sigmaB, q, r)
        fn = p-pTrue
        fn1 = S*math.exp((0-q)*(T-t))*math.sqrt(T-t)*norm._cdf(dOne(S,K,t,T,sigmaB, q, r))
        if fn1 == 0:
            return 0.0
        increment = fn/fn1 * 0.1
        if fn>0:
            sigmaA = sigmaB
            sigmaB = sigmaB-increment
        else:
            sigmaMid = (sigmaA+sigmaB)/2
            while sigmaA-sigmaB > tol:
                sigmaMid = (sigmaA+sigmaB)/2
                if put_black_scholes(S, K, t, T, sigmaMid, q, r)-pTrue == 0:
                    return sigmaMid
                elif (put_black_scholes(S, K, t, T, sigmaMid, q, r)-pTrue)*(put_black_scholes(S, K, t, T, sigmaB, q, r)-pTrue)<0:
                    sigmaA = sigmaMid
                else:
                    sigmaB = sigmaMid
            return abs(sigmaMid)
        n += 1
        sigmaDiff = abs(increment)
    return sigmaB
示例#4
0
def bias_corrected_ci(true_coeff, samples, conf=95):
    """
    Return the bias-corrected bootstrap confidence interval, using the method from the book.
    :param true_coeff: The estimates in the original sample
    :param samples: The bootstrapped estimates
    :param conf: The level of the desired confidence interval
    :return: The bias-corrected LLCI and ULCI for the bootstrapped estimates.
    """
    ptilde = (samples < true_coeff).mean()
    Z = norm.ppf(ptilde)
    Zci = z_score(conf)
    Zlow, Zhigh = -Zci + 2 * Z, Zci + 2 * Z
    plow, phigh = norm._cdf(Zlow), norm._cdf(Zhigh)
    llci = np.percentile(samples, plow * 100, interpolation='lower')
    ulci = np.percentile(samples, phigh * 100, interpolation='higher')
    return llci, ulci
示例#5
0
def bias_corrected_ci(estimate: np.array, samples: np.array, conf: float = 95) -> (float, float):
    """
    Return the bias-corrected bootstrap confidence interval for an estimate
    :param estimate: Numerical estimate in the original sample
    :param samples: Nx1 array of bootstrapped estimates
    :param conf: Level of the desired confidence interval
    :return: Bias-corrected bootstrapped LLCI and ULCI for the estimate.
    """
    ptilde = ((samples < estimate) * 1).mean()
    Z = norm.ppf(ptilde)
    Zci = z_score(conf)
    Zlow, Zhigh = -Zci + 2 * Z, Zci + 2 * Z
    plow, phigh = norm._cdf(Zlow), norm._cdf(Zhigh)
    llci = np.percentile(samples, plow * 100, interpolation='lower')
    ulci = np.percentile(samples, phigh * 100, interpolation='higher')
    return llci, ulci
示例#6
0
def bias_corrected_ci(estimate, samples, conf=95):
    """
    Return the bias-corrected bootstrap confidence interval for an estimate
    :param estimate: Numerical estimate in the original sample
    :param samples: Nx1 array of bootstrapped estimates
    :param conf: Level of the desired confidence interval
    :return: Bias-corrected bootstrapped LLCI and ULCI for the estimate.
    """
    # noinspection PyUnresolvedReferences
    ptilde = ((samples < estimate) * 1).mean()
    Z = norm.ppf(ptilde)
    Zci = z_score(conf)
    Zlow, Zhigh = -Zci + 2 * Z, Zci + 2 * Z
    plow, phigh = norm._cdf(Zlow), norm._cdf(Zhigh)
    llci = np.percentile(samples, plow * 100, interpolation="lower")
    ulci = np.percentile(samples, phigh * 100, interpolation="higher")
    return llci, ulci
示例#7
0
def transform_hr(t, lamda):
    '''model of Huesler Reiss 1989

    special case:  a1=a2=1 : symmetric negative logistic of Galambos 1978

    restrictions:
     - lambda in (0,inf)
    '''
    def _check_args(lamda):
        cond = (lamda > 0)
        return cond

    if not np.all(_check_args(lamda)):
        raise ValueError('invalid args')

    term = np.log((1. - t) / t) * 0.5 / lamda

    from scipy.stats import norm  #use special if I want to avoid stats import
    transf = (1 - t) * norm._cdf(lamda + term) + t * norm._cdf(lamda - term)
    return transf
示例#8
0
def transform_hr(t, lamda):
    '''model of Huesler Reiss 1989

    special case:  a1=a2=1 : symmetric negative logistic of Galambos 1978

    restrictions:
     - lambda in (0,inf)
    '''

    def _check_args(lamda):
        cond = (lamda > 0)
        return cond

    if not np.all(_check_args(lamda)):
        raise ValueError('invalid args')

    term = np.log((1. - t) / t) * 0.5 / lamda

    from scipy.stats import norm  #use special if I want to avoid stats import
    transf = (1 - t) * norm._cdf(lamda + term) + t * norm._cdf(lamda - term)
    return transf
示例#9
0
    def estimate_power(self):
        """
        Estimate the power of the tests entered in the-pcurve, correcting for publication bias.
        :return: p, lbp, ubp: The power, and (lower bound, upper bound) of the 95% CI for power.
        """
        p = self._solve_power_for_pct(.50)
        p05 = norm._cdf(self._compute_stouffer_z_at_power(.051))
        p99 = norm._cdf(self._compute_stouffer_z_at_power(.99))
        if p05 <= .95:
            lbp = .05
        elif p99 >= .95:
            lbp = .99
        else:
            lbp = self._solve_power_for_pct(.95)

        if p05 <= .05:
            ubp = .05
        elif p99 >= .05:
            ubp = .99
        else:
            ubp = self._solve_power_for_pct(.05)
        return p, (lbp, ubp)
示例#10
0
def implied_vol_C(S, K, t, T, q, r, cTrue):
    tol = 1e-8
    sigma = initial_guess(S, K, t, T, q, r)
    sigmaDiff = 1.0
    n = 1
    nMax = 1000
    while sigmaDiff >= tol and n < nMax :
        #f(xn)
        c = call_black_scholes(S, K, t, T, sigma, q, r)
        fn = c - cTrue
        #f'(xn)
        fn1 = S * math.exp((0 - q) * (T - t)) * math.sqrt(T - t) * norm._cdf(dOne(S,K,t,T,sigma, q, r))
        increment = fn / fn1 * 0.1
        sigma = sigma - increment
        n += 1
        sigmaDiff = abs(increment)
    return sigma
示例#11
0
def gaussian_cdf(h, Xi, x):
    return h * norm._cdf((x - Xi)/h)
示例#12
0
def Increment(option_price,true_data,Stock,d1):
    increment = (option_price-true_data)/(Stock*math.exp(-q*(maturity-float(t)))*math.sqrt(maturity-float(t)*norm._cdf(d1)))
    return increment
示例#13
0
def Increment(option_price, true_data, Stock, d1):
    increment = (option_price -
                 true_data) / (Stock * math.exp(-q * (maturity - float(t))) *
                               math.sqrt(maturity - float(t) * norm._cdf(d1)))
    return increment
示例#14
0
rate=0.01
repo = 0
tol = 1e-8
i=0
sigmadiff=1

sigmahat = math.sqrt(2*abs((math.log(stock/strike)+rate*maturity)/maturity))
sigma = sigmahat
#print sigmahat
while sigmadiff>=tol and i<=100:
#d1= ((math.log(stock/strike)+(rate-repo)*(maturity-float(t)))/sigma*math.sqrt(maturity-float(t)))+0.5*sigma*math.sqrt(maturity-float(t))
    d1 = (math.log(stock/strike)+float(rate-repo)*maturity)/(float(sigma)*math.sqrt(maturity))+0.5*float(sigma)*math.sqrt(maturity)

#d2= ((math.log(stock/strike)+(rate-repo)*(maturity-float(t)))/sigma*math.sqrt(maturity-float(t)))-0.5*sigma*math.sqrt(maturity-float(t))
    d2 = (math.log(stock/strike)+float(rate-repo)*maturity)/(float(sigma)*math.sqrt(maturity))-0.5*float(sigma)*math.sqrt(maturity)

    call_price = call_option_price(stock, strike, maturity, rate, repo)
    increment = (call_price-c_true)/(stock*math.exp(-repo*(maturity-float(t)))*math.sqrt(maturity-float(t)*norm._cdf(d1)))
    sigma = sigma-increment
    sigmadiff=abs(increment)
    i=i+1

    print "---------------------------------"
    print 'd1: %.8f'% d1
    print 'd2: %.8f'% d2
    print 'Call price: %.8f'% call_price
    print 'sigma: %.8f'% sigma



def mytruncpdf_01bounds(x, loc, scale):
    Z = norm._cdf((1-loc)/scale) - norm._cdf(-loc/scale)
    pdf_psi = norm._pdf((x - loc) / scale)
    return pdf_psi / (scale * Z)
示例#16
0
def my_tn_cdf(upper, mn, scale):
    """Assumes that clipping between 0 and 1.
    Some notation from wiki page on truncated normal"""
    norm_cdf_alpha = norm._cdf(-mn/scale)
    return ((norm._cdf((upper - mn) / scale) - norm_cdf_alpha) / 
            (norm._cdf((1 - mn) / scale) - norm_cdf_alpha))
示例#17
0
def tn_pdf_01(x, loc, scale):
    Z = norm._cdf((1-loc)/scale) - norm._cdf(-loc/scale)
    pdf_psi = norm._pdf((x - loc) / scale)
    return pdf_psi / (scale * Z)