def demo():
    skew1 = skew_normal_density(L=25, unit=unit, a=1.5)
    densities = [skew1, skew1, skew1]
    densityAll, multiplicityAll = winner_of_many(densities)
    densityAllCheck = sample_winner_of_many(densities, nSamples=50000)
    if PLOTS:
        densitiesPlot([densityAll, densityAllCheck], unit=0.1)
def demo():
    densities = [
        skew_normal_density(L=L, unit=unit, a=0.3 * i) for i in range(25)
    ]
    best, multiplicity = winner_of_many(densities)
    densitiesPlot([d / unit
                   for d in densities[:5]] + [best / unit, multiplicity], unit)
def demo():
    skew1 = skew_normal_density(L=L, unit=unit, a=1.5)
    skew2 = skew_normal_density(L=L, unit=unit, a=1.5, loc=-0.5)
    skew3 = skew_normal_density(L=L, unit=unit, a=1.5, loc=-1.0)
    best, multiplicity = winner_of_many([skew1, skew2, skew3])
    densitiesPlot(
        [skew1 / unit, skew2 / unit, skew3 / unit, best / unit, multiplicity],
        unit,
        legend=['1', '2,', '3', 'best', 'multiplicity'])
def demo():
    skew1                   = skew_normal_density(L=L, unit = unit, a=1.5)
    prices                  = [ 0.2, 0.3, 0.5 ]
    implied_offsets         = solve_for_implied_offsets(prices = prices, density = skew1, nIter = 2)
    inferred_prices         = state_prices_from_offsets( skew1, implied_offsets )
    print(str(inferred_prices))
    densities               = densities_from_offsets( skew1, implied_offsets )
    densityAllAgain, multiplicityAll  = winner_of_many(densities)
    if PLOTS:
        densitiesPlot( [ densityAllAgain ] + densities, unit = 0.1, legend = ['guess','analytic','1','2','3'] )
from winning.lattice import densities_from_events, state_prices_from_densities
from winning.lattice_plot import densitiesPlot


def test_golf():
    event_18 = [0, 0, 0.5, 0.5, 0, 0, 0]
    event_17 = [0, 0, 0.5, 0.5, 0, 0, 0]
    scores = [-15, -15]
    densities = densities_from_events(scores=scores,
                                      events=[[event_17, event_18],
                                              [event_18]],
                                      unit=1,
                                      L=6)

    return densities


if __name__ == '__main__':
    d = test_golf()
    import matplotlib.pyplot as plt
    densitiesPlot(d, unit=1, legend=['two to play', 'one to play'])
    plt.show()
    p = state_prices_from_densities(densities=d)
    print(p)
def solve_for_implied_offsets(prices,
                              density,
                              offset_samples=None,
                              implied_offsets_guess=None,
                              nIter=3,
                              verbose=False,
                              visualize=False):
    """
    This is the main routine.

    See the paper for details, in the /doc folder
    https://github.com/microprediction/winning/blob/main/docs/Horse_Race_Problem__SIAM_updated.pdf

        offset_samples   Optionally supply a list of offsets which are used in the interpolation table  a_i -> p_i

    """

    L = implied_L(density)
    if offset_samples is None:
        offset_samples = list(range(int(-L / 2), int(L / 2)))[::-1]
    else:
        _assert_descending(offset_samples)

    if implied_offsets_guess is None:
        implied_offsets_guess = list(range(int(L / 3)))

    # First guess at densities
    densities, coefs = densities_and_coefs_from_offsets(
        density, implied_offsets_guess)
    densityAllGuess, multiplicityAllGuess = winner_of_many(densities)
    densityAll = densityAllGuess.copy()
    multiplicityAll = multiplicityAllGuess.copy()

    if verbose:
        guess_prices = [
            np.sum(
                expected_payoff(density,
                                densityAll,
                                multiplicityAll,
                                cdf=None,
                                cdfAll=None)) for density in densities
        ]

    for _ in range(nIter):
        if visualize:
            from winning.lattice_plot import densitiesPlot
            # temporary hack to check progress of optimization
            densitiesPlot([densityAll] + densities, unit=0.1)

        # Main iteration...
        implied_prices = implicit_state_prices(density=density,
                                               densityAll=densityAll,
                                               multiplicityAll=multiplicityAll,
                                               offsets=offset_samples)
        implied_offsets = np.interp(prices, implied_prices, offset_samples)
        densities = densities_from_offsets(density, implied_offsets)
        densityAll, multiplicityAll = winner_of_many(densities)

        if verbose:
            guess_prices = [
                np.sum(
                    expected_payoff(density,
                                    densityAll,
                                    multiplicityAll,
                                    cdf=None,
                                    cdfAll=None)) for density in densities
            ]
            approx_prices = [np.round(pri, 3) for pri in prices]
            approx_guesses = [np.round(pri, 3) for pri in guess_prices]

            print(zip(approx_prices, approx_guesses)[:5])

    return implied_offsets
from winning.lattice_copula import gaussian_copula_margin_0
from winning.lattice import skew_normal_density
from winning.lattice_plot import densitiesPlot
from pprint import pprint



def test_ensure_scipy():
    from winning.scipyinclusion import using_scipy
    from scipy.integrate import quad_vec
    assert using_scipy


def test_five_skew():
    mus = [-0.5, -0.25, 0, 1, 1.5]
    scales = [1.0, 1.5, 1.2, 1.3, 2.0]
    densities = [skew_normal_density(L=500, unit=0.01, scale=scale, loc=mu, a=1.0) for mu, scale in zip(mus, scales)]
    margin_0 = gaussian_copula_margin_0(densities, rho=0.9)
    return densities[0], margin_0



if __name__=='__main__':
    density1, density2 = test_five_skew()
    legend = ['margin','reconstructed']
    densitiesPlot(densities=[density1,density2], unit=0.1, legend=legend)
    print(sum(density2))

def demo():
    skew = skew_normal_density(L=L, unit=unit, a=1.0)
    print("mean is " + str(mean_of_density(skew, unit=unit)))
    densitiesPlot([skew], unit=unit)
Example #9
0
from winning.lattice import skew_normal_density
from winning.lattice_plot import densitiesPlot
from pprint import pprint


def test_ensure_scipy():
    from winning.scipyinclusion import using_scipy
    from scipy.integrate import quad_vec
    assert using_scipy


def test_five_skew():
    mus = [-0.5, -0.25, 0, 1, 1.5]
    scales = [1.0, 1.5, 1.2, 1.3, 2.0]
    densities = [
        skew_normal_density(L=100, unit=0.1, scale=scale, loc=mu, a=1.0)
        for mu, scale in zip(mus, scales)
    ]
    rank_probs = gaussian_copula_five(densities, rho=0.01)
    return densities, rank_probs


if __name__ == '__main__':
    import time
    st = time.time()
    densities, rank_probs = test_five_skew()
    legend = ['Asset ' + str(i) for i in range(1, 6)]
    print({'elapsed': time.time() - st})
    densitiesPlot(densities=densities, unit=0.1, legend=legend)
    pprint(rank_probs)
Example #10
0
from winning.lattice_copula import gaussian_copula_win
from winning.lattice import skew_normal_density
from winning.lattice_plot import densitiesPlot


def test_ensure_scipy():
    from winning.scipyinclusion import using_scipy
    assert using_scipy


def test_two_skew():
    density1 = skew_normal_density(L=50, unit=0.1, scale=1.0, loc=1.0, a=1.0)
    density2 = skew_normal_density(L=50, unit=0.1, scale=1.0, loc=0, a=1.0)
    densities = [density1, density2]
    state_prices = gaussian_copula_win(densities=densities, rho=0.85)
    return densities, state_prices


if __name__ == '__main__':
    densities, state_prices = test_two_skew()
    densitiesPlot(densities=densities, unit=0.1)
    print(state_prices)