Esempio n. 1
0
except FileNotFoundError:
    db = loadmat(os.path.join(TEMPORARY_DB, 'db_OptionStrategy'), squeeze_me=True)

OptionStrategy = struct_to_dict(db['OptionStrategy'])

try:
    db = loadmat(os.path.join(GLOBAL_DB, 'db_VIX'), squeeze_me=True)
except FileNotFoundError:
    db = loadmat(os.path.join(TEMPORARY_DB, 'db_VIX'), squeeze_me=True)

VIX = struct_to_dict(db['VIX'])

# invariants (daily P&L)
pnl = OptionStrategy.cumPL
epsi = diff(pnl)
dates_x = array([datenum(i) for i in OptionStrategy.Dates])
dates_x = dates_x[1:]

# conditioning variable (VIX)
z = VIX.value
dates_z = VIX.Date

    # merging datasets
[dates, i_epsi, i_z] = intersect(dates_x, dates_z)

pnl = pnl[i_epsi + 1]
epsi = epsi[i_epsi]
z = z[i_z]
t_ = len(epsi)
# -
Esempio n. 2
0
option = namedtuple('option', 'n_bins')
option.n_bins = round(10 * log(t_))
p, x = HistogramFP(pnl.reshape(1, -1), flex_probs, option)

# ## Compute the MMFP pdf

# +
xx = sort(x)
xx = r_[xx, npmax(xx) + arange(0.001, 0.051, 0.001)]
m1 = flex_probs @ pnl.T
m3 = flex_probs @ ((pnl - m1)**3).T

sln = lognorm.pdf(sign(m3) * xx - c, sqrt(sig2), scale=exp(mu))  # fitted pdf

date_dt = array([date_mtop(datenum(i)) for i in date])
myFmt = mdates.DateFormatter('%d-%b-%Y')
date_tick = arange(200 - 1, t_, 820)
# -

# ## Generate the figure

# +
f = figure()
# HFP histogram with MMFP pdf superimposed
h1 = plt.subplot(3, 1, 1)
b = bar(x[:-1],
        p[0],
        width=x[1] - x[0],
        facecolor=[.8, .8, .8],
        edgecolor=[.6, .6, .6])
Esempio n. 3
0
def RollPrices2Prices(t_end_str, tau, dates, z_roll):
    # This function uses rolling values to compute the zero-coupon bond value
    # (i.e., discount factors) with maturities in t_end_str.
    # INPUTS
    #  t_end_str [vector]: (k_ x 1) selected maturities (as .Tdd-mmm-yy.T strings)
    #  tau [vector]: (n_ x 1) times to maturity corresponding to rows of z_roll
    #  Date [vector]: (1 x t_end) dates corresponding to columns of z_roll
    #  z_roll [matrix]: (n_ x t_end) rolling values
    # OUTPUTS
    #  date [cell vector]: (k_ x 1) cell date{j} contains the numerical value of
    #                              dates corresponding to columns of z{j}
    #  z [cell vector]: (k_ x 1) cell z{j} contains the evolution of zero-coupon
    #                           bond value with maturity t_end{j}
    #  t_end [vector]: (k_ x 1) contains the numerical value corresponding to
    #                          date strings in t_end_string

    # tau_int: vector of maturities for interpolation
    tauRange = ceil((dates[-1] - dates[0]) / 365)
    _, _, tauIndex = intersect(tauRange, tau)
    if not tauIndex:
        tauIndex = tau.shape[0]
    tau_int = arange(tau[0], tau[tauIndex] + tau[0], tau[0])
    # declaration and preallocation of variables
    t_ = z_roll.shape[1]
    n_ = npmax(tau_int.shape)
    z_roll_int = zeros((n_, t_))
    expiry = zeros((n_, t_))
    expiry_f = zeros((n_, t_))
    k_ = t_end_str.shape[0]
    t_end = zeros((k_, 1), dtype=int)
    z = {}
    date = {}

    for t in range(t_):
        # remove zeros
        indexPolished = np.where(abs(z_roll[:, t]) > 0)[0]
        # matrix of rolling values: z_roll(i,t)=z_{t}(tau[i]+t)
        z_roll_int[:, t] = interp(tau_int, tau[indexPolished],
                                  z_roll[indexPolished, t])
        # expiries
        for i in range(n_):
            expiry[i, t] = tau_int[i] * 365 + dates[t]
            expiry_f[i, t] = floor(expiry[i, t])  # to remove HH:mm:ss

    # zero-coupon bond values (i.e., discount factors) with fixed expiry
    for j in range(k_):
        z[j] = zeros((1, t_))
        date[j] = zeros((1, t_))
        t_end[j] = datenum(t_end_str[j])
        # z[j] = np.where(expiry_f==t_end[j],z_roll_int,z[j])
        # date[j] = np.where(expiry_f==t_end[j],dates,date[j])
        for t in range(t_):
            for i in range(n_):
                if expiry_f[i, t] == t_end[j]:
                    z[j][0, t] = z_roll_int[i, t]
                    date[j][0, t] = dates[t]
        # remove zeros
        indexzeros = np.where(date[j] == 0)
        date[j][indexzeros] = np.NAN
        z[j][indexzeros] = np.NaN
    return date, z, t_end
Esempio n. 4
0
try:
    db = loadmat(os.path.join(GLOBAL_DB, 'db_OptionStrategy'), squeeze_me=True)
except FileNotFoundError:
    db = loadmat(os.path.join(TEMPORARY_DB, 'db_OptionStrategy'),
                 squeeze_me=True)

OptionStrategy = struct_to_dict(db['OptionStrategy'], as_namedtuple=False)

try:
    db = loadmat(os.path.join(GLOBAL_DB, 'db_VIX'), squeeze_me=True)
except FileNotFoundError:
    db = loadmat(os.path.join(TEMPORARY_DB, 'db_VIX'), squeeze_me=True)

VIX = struct_to_dict(db['VIX'], as_namedtuple=False)

DateOptStrat = array([datenum(i) for i in OptionStrategy['Dates']]).T
common, i_spvix, i_rates = intersect(SPX['Date'], DF_Rolling['Dates'])
SPX['Date'] = SPX['Date'][i_spvix]
SPX['Price_close'] = SPX['Price_close'][i_spvix]
VIX['value'] = VIX['value'][i_spvix]
VIX['Date'] = VIX['Date'][i_spvix]
DF_Rolling['Dates'] = DF_Rolling['Dates'][i_rates]
DF_Rolling['Prices'] = DF_Rolling['Prices'][:, i_rates]
common, i_others, i_options = intersect(common, DateOptStrat)
SPX['Date'] = SPX['Date'][i_others]
SPX['Price_close'] = SPX['Price_close'][i_others]
VIX['value'] = VIX['value'][i_others]
VIX['Date'] = VIX['Date'][i_others]
DF_Rolling['Dates'] = DF_Rolling['Dates'][i_others]
DF_Rolling['Prices'] = DF_Rolling['Prices'][:, i_others]
DateOptStrat = DateOptStrat[i_options]
Esempio n. 5
0
from intersect_matlab import intersect
# -

# ## Upload rolling values from 03-Oct-2002 to 03-Oct-2007 with 1 year to maturity, contained in db_SwapCurve

# +
try:
    db = loadmat(os.path.join(GLOBAL_DB, 'db_SwapCurve'), squeeze_me=True)
except FileNotFoundError:
    db = loadmat(os.path.join(TEMPORARY_DB, 'db_SwapCurve'), squeeze_me=True)

DF_Rolling = struct_to_dict(db['DF_Rolling'])

# extraction of rolling values from 03-Oct-2002 to 03-Oct-2007 with tau = 1 year
_, _, dateIndices = intersect(
    [datenum('03-Oct-2002'), datenum('03-Oct-2007')], DF_Rolling.Dates)
_, _, tauIndex = intersect(1, DF_Rolling.TimeToMat)
zroll = DF_Rolling.Prices[tauIndex, dateIndices[0]:dateIndices[1] + 1]
dates = DF_Rolling.Dates[dateIndices[0]:dateIndices[1] + 1]
time = arange(dates[0], dates[-1] + 1)

t_end = array([
    '03-Oct-2003', '03-Oct-2004', '03-Oct-2005', '03-Oct-2006', '03-Oct-2007'
])

_, timeindex, _ = intersect(time, list(map(datenum, t_end)))
# -

# ## Interpolate the rolling value on an yearly spaced grid

zroll = interp(time, dates, zroll[0])