Пример #1
0
def protectedExp(a):
    if (a >= 255):
        a = 1
        return math.exp(a)
    else:
        a = a / 255.0
        return math.exp(a)
Пример #2
0
def optionBS(spot,strike, vol, T, Rf):
    '''
    call option using Black Scholes
    '''
    denom = vol * math.sqrt(T)
    d1 = (math.log(spot/strike) + (Rf + 0.5*vol**2)*T) / denom
    d2 = d1 - denom
    
    call = spot * stats.norm.cdf(d1) - strike * math.exp(-Rf * T) * stats.norm.cdf(d2)
    put = strike * math.exp(-Rf * T) * stats.norm.cdf(-d2) - spot * stats.norm.cdf(-d1)
    
    return call, put
Пример #3
0
def square_gaussian_filter1d(input,
                             sigma,
                             axis=-1,
                             output=None,
                             mode="reflect",
                             cval=0.0):
    """One-dimensional Squared Gaussian filter.

    The standard-deviation of the Gaussian filter is given by
    sigma.
    """
    sd = float(sigma)
    # make the length of the filter equal to 4 times the standard
    # deviations:
    lw = int(4.0 * sd + 0.5)
    weights = [0.0] * (2 * lw + 1)
    weights[lw] = 1.0
    sum = 1.0
    sd = sd * sd
    # calculate the kernel:
    for ii in range(1, lw + 1):
        tmp = math.exp(-0.5 * float(ii * ii) / sd)
        weights[lw + ii] = tmp
        weights[lw - ii] = tmp
        sum += 2.0 * tmp
    for ii in range(2 * lw + 1):
        weights[ii] /= sum
    weights[ii] = weights[ii]**2
    return correlate1d(input, weights, axis, output, mode, cval, 0)
Пример #4
0
def prob_exp(t, tau):
    if tau <= 0:
        return 0
    else:
        p = 1.0
        for i in range(0, len(t)):
            tt = t[i]
            p = p * (1 / tau) * math.exp(-tt / tau) if tt >= 0 else 0
        return p
Пример #5
0
def prob_poisson(n, mu):
    if mu <= 0 or n < 0:
        return 0
    else:
        p = 1
        for i in range(0, len(n)):
            nn = int(n[i])
            p *= (mu**nn) * math.exp(-mu) / math.factorial(nn)
        return p
Пример #6
0
def chi2_exp(tau, *args):
    if (tau <= 0):
        return 1e99
    hist = args[0]
    q = 0
    #  print(tau)
    ntot = hist.GetEntries()
    for i in range(1, hist.GetNbinsX() + 1):
        bc = hist.GetBinCenter(i)
        width = hist.GetBinWidth(i)
        tmin = bc - width / 2.
        tmax = bc + width / 2.
        fi = (math.exp(-tmin / tau) - math.exp(-tmax / tau)) * ntot
        if fi > 0:
            di = hist.GetBinContent(i)
            q = q + (di - fi)**2 / fi**2
            #  print(di,fi)
    return q
Пример #7
0
def square_gaussian_filter1d(input, sigma, axis = -1, output = None, mode = "reflect", cval = 0.0):
    """One-dimensional Squared Gaussian filter.

    The standard-deviation of the Gaussian filter is given by
    sigma.
    """
    sd = float(sigma)
    # make the length of the filter equal to 4 times the standard
    # deviations:
    lw = int(4.0 * sd + 0.5)
    weights = [0.0] * (2 * lw + 1)
    weights[lw] = 1.0
    sum = 1.0
    sd = sd * sd
    # calculate the kernel:
    for ii in range(1, lw + 1):
        tmp = math.exp(- 0.5 * float(ii * ii) / sd)
        weights[lw + ii] = tmp
        weights[lw - ii] = tmp
        sum += 2.0 * tmp
    for ii in range(2 * lw + 1):
        weights[ii] /= sum
    weights[ii] = weights[ii]**2
    return correlate1d(input, weights, axis, output, mode, cval, 0)
def irl_gradient_ascent(sample_trajectories,
                        feature_matrix,
                        feature_e,
                        scaler,
                        error_term,
                        thre,
                        learning_rate,
                        base_p,
                        execu_p,
                        show_flag=False,
                        show_count=50):
    '''
    Relative Entropy Inverse Reinforcement Learning
    '''
    print('feature_matrix=' + feature_matrix)
    print('feature_e=' + feature_e)
    feature_expectation = feature_e
    diff = 10000
    last_diff = 0
    n_feature = len(feature_e)
    max_fe = []
    for l_fe in feature_matrix.T:
        max_fe.append(np.max(l_fe))
    theta = np.random.random(size=(n_feature, ))
    base_vector = np.zeros(n_feature)

    prob_ratio = get_prob_ratio_from_policies(sample_trajectories, base_p,
                                              execu_p)
    loop_counter = 0
    last_theta = []
    ll_diff = 0
    back_flag = False
    mem_alpha = learning_rate
    set_alpha = False

    while abs(diff) > thre:
        if not back_flag:
            last_diff = diff
        iterated_rewards = np.dot(theta, feature_matrix.T) + prob_ratio
        alphas = theta / np.abs(theta) * error_term
        feature_count = np.zeros(n_feature)
        sample_importance = 0
        m_reward = max(iterated_rewards)
        ind_re = np.argmax(iterated_rewards)
        iterated_rewards -= m_reward + 20
        for i in range(len(feature_matrix)):
            if prob_ratio[i] == 0:
                continue
            try:
                feature_count += math.exp(
                    iterated_rewards[i]) * feature_matrix[i]
                sample_importance += math.exp(iterated_rewards[i])
            except:
                print(feature_matrix[i])
                print(theta)
                print(iterated_rewards[i])
                print(prob_ratio[i])
                raise (ValueError)
        gradient = feature_expectation - feature_count / sample_importance
        theta = theta + learning_rate * gradient
        t = judge_norm(theta, base_vector, 2)
        diff = judge_norm(gradient, base_vector, 2)

        if loop_counter % show_count == 0 and show_flag:
            print(loop_counter, diff)
            print('reward', m_reward)
            print('fe', feature_count / sample_importance)
            print('differ', abs(diff - last_diff))
            print('theta', theta)
            print('grad', gradient)
            print('    ')
        loop_counter += 1
        if loop_counter >= 5000000:
            break
        last_theta = theta
    print(loop_counter, diff)
    print('theta', theta)
    return diff, theta
Пример #9
0
def getXt1(filename):
    y = getPointOfIntersection(filename)[1]
    Xt1 = 1 - math.exp(-math.exp(y))
    return Xt1
Пример #10
0
def getXt2(filename):
    y = getY2(filename)
    Xt2 = 1 - math.exp(-math.exp(y))
    return Xt2