def debug_msg(status):
    caller_levels = dict(troubleshoot_cleaner=1,
                         troubleshoot_obj_call=2,
                         troubleshoot_test=3)
    frame = gfi(gof(cf(caller_levels['troubleshoot_test']))[1][0])
    print("{} [filename: {}, line: {}]".format(status, frame.filename,
                                               frame.lineno))
def find_date_index(date_to_find, time_list_hours, next_date=0):
    c_year, c_month, c_day, c_hour, c_minute = fix_time_list(
        date_to_find, single_time_stamp=1)
    year, month, day, hour, minute = fix_time_list(time_list_hours)

    #print("   supp.%i: searching for %s " % (gf(cf()).lineno, date_to_find))
    index = 0
    while year[index] < c_year:
        index += 1
    while month[index] < c_month:
        index += 1
    while day[index] < c_day and day[index] < 31:
        index += 1
    while hour[index] < c_hour and hour[index] < 23:
        index += 1
    while minute[index] < c_minute and minute[index] < 59:
        index += 1

    if date_to_find != time_list_hours[index]:
        if next_date == 0:
            #print("   supp.%i: Date %s not found in time list. Returning nothing" % (gf(cf()).lineno, date_to_find))
            return "Error"
        else:
            print(
                "   supp.%i: Date %s not found in time list. Returning %s instead"
                % (gf(cf()).lineno, date_to_find, time_list_hours[index]))
            return index
    else:
        return index
Beispiel #3
0
def handle_flagged_args(args, config):
    """handle_flagged_args

This function will execute the appropriate steps for what arguments have been
given.  This is, essentially, a good "drop in" point for outside code to use
this module as long as args is a dict within the appropriate format or is an
empty dictionary.
    """
    error = None
    os_version = str()
    os_type = str()
    os_category = \
        BuildError(args, msg="Invalid build config", frame=gfi(cf()))
    # we're building for just one, and it should already be in
    # expected_operations...
    for item in expected_operations:
        if item.name == args['os_type']:
            for version in item.versions:
                os_version = version
                os_type = item.os_type
                if item.os_category == 'redhat':
                    os_category = build_redhat
                elif item.os_category == 'debian':
                    os_category = build_debian
                else:
                    error = False
                    os_category = os_category.print_msg
                break
    error = os_category(config, os_version, os_type=os_type) if error \
        else error
Beispiel #4
0
def build_packages(config):
    """build_packages

Builds for the expected_operations' OperationSettings entries.

This function is intentionaly blind, dumb and stupid to produce what it can.
However, it should also exit with a non-zero when an error does occur to keep
from the caller thinking that things are 'okay' when they are not.
    """
    error = 0
    print("Building packages...")
    for operation in expected_operations:
        if operation.os_category == 'redhat':
            print("For Redhat...")
            function = build_redhat
        elif operation.os_category == 'debian':
            print("For Debian...")
            function = build_debian
        else:
            # can overwrite a previous error statement, but this does need to
            # be preserved...
            error = BuildError(operation, msg="Unexpected operation specified",
                               frame=gfi(cf()))
            function = Error.print_msg
        # preserve a previous iteration's error, if needed
        for os_version in operation.versions:
            error = function(config, os_version) if not error else error
            if error:
                break
        if error:
            print("A Failure occurred...", str(error))
            break
    if not error:
        print("Completed package builds...")
    return error
Beispiel #5
0
 def timing():
     nonlocal previous_time, previous_line
     from inspect import currentframe as cf
     new_time = time()
     new_line = cf().f_back.f_lineno
     if previous_time is not None:
         print(
             f'Took {new_time-previous_time:.3f}s from {previous_line} to {new_line}'
         )
     previous_time = new_time
     previous_line = new_line
Beispiel #6
0
def _load_json(fl):
    data = {}
    if os.path.isfile(fl) and os.access(fl, os.R_OK):
        with open(fl, 'r') as fh:
            data = json.loads(fh.read())
    else:
        print("Unable to open JSON file: " + fl)
        reason = 'Cannot Access!' if os.path.isfile(fl) else \
            "File does not exist!"
        print(reason)
        raise BuildError(fl, reason, msg="Unable to open JSON file",
                         frame=gfi(cf()), errnum=errno.ENODATA)
    return data
Beispiel #7
0
def load_json():
    """load_json

Loads the config.JSON file system object's contents and translate it into a
Python object.  This python object is then returned.
    """
    try:
        env = configure.get_env()
        configure.export_to_json(env)
    except SystemExit:
        raise BuildError(msg="configure failed!  Could not build targets!",
                         frame=gfi(cf()), errno=errno.ENOSYS)
    json_fl = os.path.dirname(sys.argv[0]) + "/scripts/config.JSON"
    data = _load_json(json_fl)
    return data
Beispiel #8
0
def _preliminary_construct(config):
    # this is an internal function; however, allows for quick and easy access
    # to execute these operations...
    error = 0
    construct_setups._construct_cfgs_from_json(config)
    try:
        pass
    except Exception as error:
        print("ASDASD")
        return error
    except SystemExit:
        error = BuildError(
            config['setup_cfg'],
            config['stdeb_cfg'],
            frame=gfi(cf()),
            errno=errno.ESPIPE,
            msg="Could not construct the setup files"
        )
    return error
def debug_msg(status):
    caller_levels = dict(troubleshoot_cleaner=1, troubleshoot_obj_call=2,
                         troubleshoot_test=3)
    frame = gfi(gof(cf(caller_levels['troubleshoot_test']))[1][0])
    print("{} [filename: {}, line: {}]".format(status, frame.filename,
                                               frame.lineno))
Beispiel #10
0
# # This works, but using sys path is really not recommended
# import os, sys
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# # import ../db.py
# from Week1.bi2_2_patternCount import PatternCount

# # Let's look at the path it gives
# from os.path import dirname, abspath
# print(dirname(dirname(abspath(__file__))))

# Trying another method, apparently voted as best method if not dealing with packages
# note i added in my own aliases
from sys import path as sp
from inspect import currentframe as cf, getfile as gf
from os.path import dirname as odirname, abspath as oabspath
currentdir = odirname(oabspath(gf(cf())))
parentdir = odirname(currentdir)
sp.insert(0, parentdir)

# # This is a shorter form someone made of the above
# sys.path.insert(1, os.path.join(sys.path[0], '..'))

from Week1.bi2_2_patternCount import PatternCount

Text = "ATCAATGATCAACGTAAGCTTCTAAGCATGATCAAGGTGCTCACACAGTTTATCCACAACCTGAGTGGA"
"TGACATCAAGATAGGTCGTTGTATCTCCTTCCTCTCGTACTCTCATGACCACGGAAAGATGATCAAGAGAGGATGA"
"TTTCTTGGCCATATCGCAATGAATACTTGTGACTTGTGCTTCCAATTGACATCTTCAGCGCCATATTGCGCTGGCC"
"AAGGTGACGGAGCGGGATTACGAAAGCATGATCATGGCTGTTGTTCTGTTTATCTTGTTTTGACTGAGACTTGTTA"
"GGATAGACGGTTTTTCATCACTGACTAGCCAAAGCCTTACTCTGCCTGACATCGACCGTAAATTGATAATGAATTT"
"ACATGCTTCCGCGACGATTTACCTCTTGATCATCGATCCGATTGAAGATCTTCAATTGTTAATTCTCTTGCCTCGA"
"CTCATAGCCATGATGAGCTCTTGATCATGTTTCCTTAACCCTCTATTTTTTACGGAAGAATGATCAAGCTGCTGCT"
Beispiel #11
0
def clean_series_hour(time_listM,
                      pricesM,
                      volumesM,
                      exc=0,
                      convert_time_zones=1,
                      plot_for_extreme=0):
    remove_extremes = 1
    print(" \033[32;0;0mRunning 'clean_series_hour' ...\033[0;0;0m")
    if convert_time_zones:  # Flytter nå Coincheck ni timer, men lar Bitstamp stå
        if exc == 0 or exc == 5:
            n_hours = 1
        elif exc == 1:
            n_hours = 9
        elif exc == 2:
            n_hours = 8
        elif exc == 3:
            n_hours = -5
        elif exc == 4:
            n_hours = 9
        else:
            n_hours = 0
        print("  Converting time zones: moving series %i hour(s)" % n_hours)
    else:
        n_hours = 0

    if n_hours != 0:
        year, month, day, hour, minute = supp.fix_time_list(
            time_listM, move_n_hours=n_hours)
        time_listM = supp.make_time_list(
            year, month, day, hour,
            minute)  # Lager en ny tidsliste fra de flyttede listene

    returnsM = jake_supp.logreturn(pricesM)
    time_listH, pricesH, volumesH = convert_to_hour(time_listM, pricesM,
                                                    volumesM)
    returnsH = jake_supp.logreturn(pricesH)

    spread_abs, spreadH, time_list_spread, count_value_error = rolls.rolls(
        pricesM, time_listM, calc_basis="h", kill_output=1)
    illiq_hours_time, illiqH = ILLIQ.illiq(time_listM,
                                           returnsM,
                                           volumesM,
                                           hourly_or_daily="h",
                                           threshold=0)
    rvolH, time_list_rvol = realized_volatility.RVol(time_listM,
                                                     pricesM,
                                                     daily=0,
                                                     annualize=1)

    time_list_removed = []
    # Removing all hours where Volume is zero
    time_listH, time_list_removed, volumesH, spreadH, returnsH, rvolH = supp.remove_list1_zeros_from_all_lists(
        time_listH, time_list_removed, volumesH, spreadH, returnsH, rvolH)

    print("  dis.%i: Number of hours removed due to zero-volume: %i" %
          (gf(cf()).lineno, len(time_list_removed)))
    end_time = ""

    if exc == 0:
        cutoff_date = "01.01.2013 00:00"
        start_averaging_date = "01.01.2012 00:00"
    elif exc == 1:
        cutoff_date = "01.06.2016 00:00"
        start_averaging_date = "30.10.2014 00:00"
    elif exc == 2:
        cutoff_date = "01.01.2013 00:00"
        start_averaging_date = "01.01.2012 00:00"
        end_time = "29.09.2017 00:00"
    elif exc == 3:
        cutoff_date = "01.01.2015 00:00"
        start_averaging_date = "02.12.2014 00:00"
    elif exc == 4:
        cutoff_date = "01.01.2014 00:00"
        start_averaging_date = "01.10.2013 00:00"
    elif exc == 5:
        cutoff_date = "01.01.2015 00:00"
        start_averaging_date = "01.01.2014 00:00"
    else:
        print("  TEST SET")
        cutoff_date = "01.01.2017 00:00"
        start_averaging_date = "01.01.2017 00:00"

    cutoff_hour = supp.find_date_index(cutoff_date, time_listH, next_date=1)
    start_averaging_hour = supp.find_date_index(start_averaging_date,
                                                time_listH,
                                                next_date=1)
    if len(end_time) > 1:
        end_hour = supp.find_date_index(end_time, time_listH)
    else:
        end_hour = len(time_listH) - 1

    mean_volume_prev_year = np.average(
        volumesH[start_averaging_hour:cutoff_hour])

    time_listH = time_listH[cutoff_hour:end_hour]
    print("  Time period:", time_listH[0], "to", time_listH[-1])
    returnsH = returnsH[cutoff_hour:end_hour]
    volumesH = volumesH[cutoff_hour:end_hour]
    spreadH = spreadH[cutoff_hour:end_hour]
    illiqH = illiqH[cutoff_hour:end_hour]
    rvolH = rvolH[cutoff_hour:end_hour]

    if plot_for_extreme == 1:
        plt.plot(rvolH)
        plt.title("Raw rvol")
        plt.figure()
        plt.plot(spreadH)
        plt.title("Raw spreadH")
        plt.figure()
        plt.plot(volumesH)
        plt.title("Raw volume")
        plt.figure()
        plt.plot(illiqH)
        plt.title("Raw illiq")
        plt.figure()
        plt.plot(returnsH)
        plt.title("Raw returnsH")
        plt.figure()

    hours_to_remove = []
    if remove_extremes == 1:
        if exc == 0:
            hours_to_remove = supp.remove_extremes(hours_to_remove,
                                                   returnsH,
                                                   0.1,
                                                   threshold_lower=-0.1)
            hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2)
            hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH,
                                                   0.01)
            hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH,
                                                   0.1)
        elif exc == 1:
            hours_to_remove = supp.remove_extremes(hours_to_remove,
                                                   returnsH,
                                                   0.075,
                                                   threshold_lower=-0.075)
            hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2)
            hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH,
                                                   0.1)
            hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH,
                                                   0.1)
        elif exc == 2:
            hours_to_remove = supp.remove_extremes(hours_to_remove,
                                                   returnsH,
                                                   0.075,
                                                   threshold_lower=-0.075)
            hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2)
            hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH,
                                                   0.1)
            hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH,
                                                   0.1)
        elif exc == 3:
            hours_to_remove = supp.remove_extremes(hours_to_remove,
                                                   returnsH,
                                                   0.075,
                                                   threshold_lower=-0.075)
            hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2)
            hours_to_remove = supp.remove_extremes(hours_to_remove, volumesH,
                                                   15000)
            hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH,
                                                   0.1)
            hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH,
                                                   0.02)
        elif exc == 4:
            hours_to_remove = supp.remove_extremes(hours_to_remove,
                                                   returnsH,
                                                   0.075,
                                                   threshold_lower=-0.075)
            hours_to_remove = supp.remove_extremes(hours_to_remove, rvolH, 2)
            hours_to_remove = supp.remove_extremes(hours_to_remove, volumesH,
                                                   15000)
            hours_to_remove = supp.remove_extremes(hours_to_remove, spreadH,
                                                   0.1)
            hours_to_remove = supp.remove_extremes(hours_to_remove, illiqH,
                                                   0.02)

    time_listH = np.delete(time_listH, hours_to_remove)
    returnsH = np.delete(returnsH, hours_to_remove)
    volumesH = np.delete(volumesH, hours_to_remove)
    spreadH = np.delete(spreadH, hours_to_remove)
    illiqH = np.delete(illiqH, hours_to_remove)
    rvolH = np.delete(rvolH, hours_to_remove)

    if plot_for_extreme == 1:
        plt.plot(rvolH)
        plt.title("rvol")
        plt.figure()
        plt.plot(spreadH)
        plt.title("spreadH")
        plt.figure()
        plt.plot(volumesH)
        plt.title("volume")
        plt.figure()
        plt.plot(illiqH)
        plt.title("illiq")
        plt.figure()
        plt.plot(returnsH)
        plt.title("returnsH")
        plt.show()

    # Removing all days where Roll is zero
    time_listH, time_list_removed, spreadH, volumesH, returnsH, illiqH, rvolH = supp.remove_list1_zeros_from_all_lists(
        time_listH, time_list_removed, spreadH, volumesH, returnsH, illiqH,
        rvolH)

    # Removing all hours where Rvol is zero
    time_listH, time_list_removed, rvolH, spreadH, volumesH, returnsH, illiqH = supp.remove_list1_zeros_from_all_lists(
        time_listH, time_list_removed, rvolH, spreadH, volumesH, returnsH,
        illiqH)

    # Removing all hours where ILLIQ is zero
    time_listH, time_list_removed, illiqH, rvolH, spreadH, volumesH, returnsH = supp.remove_list1_zeros_from_all_lists(
        time_listH, time_list_removed, illiqH, rvolH, spreadH, volumesH,
        returnsH)

    # Turning ILLIQ, Volume and rvol into log
    log_illiqH = np.log(illiqH)
    log_volumesH = volume_transformation(volumesH, mean_volume_prev_year)
    log_rvolH = np.log(rvolH)

    #print("  dis.%i: Length of time %i, spread %i, rvol %i, illiq %i, and log_illiq %i" % (gf(cf()).lineno, len(time_listH), len(spreadH), len(rvolH), len(illiqH), len(log_illiqH)))
    print(" \033[32;0;0mFinished running 'clean_series_hour' ...\033[0;0;0m")
    return time_listH, returnsH, spreadH, volumesH, log_volumesH, illiqH, log_illiqH, rvolH, log_rvolH
Beispiel #12
0
def clean_series_days(time_listM,
                      pricesM,
                      volumesM,
                      exc=0,
                      print_days_excluded=0,
                      convert_time_zones=1,
                      plot_for_extreme=0):
    print(" \033[32;0;0mRunning 'clean_series_days' ...\033[0;0;0m")
    if convert_time_zones:
        if exc == 0 or exc == 5:
            n_hours = 1
        elif exc == 1:
            n_hours = 9
        elif exc == 2:
            n_hours = 8
        elif exc == 3:
            n_hours = -5
        elif exc == 4:
            n_hours = 9
        else:
            n_hours = 0
        print("  Converting time zones: moving series %i hour(s)" % n_hours)
    else:
        n_hours = 0

    if n_hours != 0:
        year, month, day, hour, minute = supp.fix_time_list(
            time_listM, move_n_hours=n_hours)
        time_listM = supp.make_time_list(year, month, day, hour, minute)

    time_listD, pricesD, volumesD = convert_to_day(time_listM, pricesM,
                                                   volumesM)

    end_time_D = ""
    if exc == 0:
        cutoff_date = "01.01.2013 00:00"
        cutoff_min_date = "01.01.2013 01:00"
        start_averaging_date = "01.01.2012 00:00"
    elif exc == 1:
        cutoff_date = "01.01.2016 00:00"
        cutoff_min_date = "01.01.2016 09:00"
        start_averaging_date = "30.10.2014 00:00"
    elif exc == 2:
        cutoff_date = "01.01.2013 00:00"
        cutoff_min_date = "01.01.2013 08:00"
        end_time_D = "01.01.2017 00:00"
        end_time_M = "01.01.2017 08:00"
        start_averaging_date = "01.01.2012 00:00"
    elif exc == 3:
        cutoff_date = "01.01.2015 00:00"
        cutoff_min_date = "01.01.2015 19:00"
        start_averaging_date = "02.12.2014 00:00"
    elif exc == 4:
        cutoff_date = "01.01.2014 00:00"
        cutoff_min_date = "01.01.2014 09:00"
        start_averaging_date = "01.10.2013 00:00"
    elif exc == 5:
        cutoff_date = "01.01.2015 00:00"
        cutoff_min_date = "01.01.2015 01:00"
        start_averaging_date = "01.03.2014 00:00"
    else:
        print("  TEST SET")
        cutoff_date = "01.01.2017 00:00"
        cutoff_min_date = "01.01.2017 00:00"
        start_averaging_date = "01.01.2017 00:00"

    cutoff_day = supp.find_date_index(cutoff_date, time_listD, next_date=1)
    cutoff_min = supp.find_date_index(cutoff_min_date, time_listM, next_date=1)
    start_averaging_day = supp.find_date_index(start_averaging_date,
                                               time_listD,
                                               next_date=1)
    mean_volume_prev_year = np.average(
        volumesD[start_averaging_day:cutoff_day])
    if len(end_time_D) > 1:
        cutoff_endD = supp.find_date_index(end_time_D, time_listD)
        cutoff_endM = supp.find_date_index(end_time_M, time_listM)
    else:
        cutoff_endD = len(time_listD)
        cutoff_endM = len(time_listM)

    time_listM = time_listM[cutoff_min:cutoff_endM]
    print("  Time period:", time_listM[0], "to",
          time_listM[len(time_listM) - 1])
    pricesM = pricesM[cutoff_min:cutoff_endM]
    volumesM = volumesM[cutoff_min:cutoff_endM]
    pricesD = pricesD[cutoff_day:cutoff_endD]
    volumesD = volumesD[cutoff_day:cutoff_endD]
    time_listD = time_listD[cutoff_day:cutoff_endD]

    # Rolls
    spread_abs, spreadD, time_list_rolls, count_value_error = rolls.rolls(
        pricesM, time_listM, calc_basis="d", kill_output=1)
    # Realized volatility
    rvolD, RVol_time = realized_volatility.RVol(time_listM,
                                                pricesM,
                                                daily=1,
                                                annualize=1)
    # Returns
    returnsM = jake_supp.logreturn(pricesM)
    returnsD = jake_supp.logreturn(pricesD)
    # Amihud's ILLIQ
    illiq_timeD, illiqD = ILLIQ.illiq(time_listM,
                                      returnsM,
                                      volumesM,
                                      threshold=0)  # Already clean

    if plot_for_extreme == 1:
        plt.plot(rvolD)
        plt.title("Raw rvol")
        plt.figure()
        plt.plot(spreadD)
        plt.title("Raw spreadH")
        plt.figure()
        plt.plot(volumesD)
        plt.title("Raw volume")
        plt.figure()
        plt.plot(illiqD)
        plt.title("Raw illiq")
        plt.figure()
        plt.plot(returnsD)
        plt.title("Raw returnsH")
        plt.figure()

    time_list_removed = []
    # Removing all days where Volume is zero
    time_listD, time_list_removed, volumesD, spreadD, returnsD, rvolD = supp.remove_list1_zeros_from_all_lists(
        time_listD, time_list_removed, volumesD, spreadD, returnsD, rvolD)

    # --------------------------------------------
    days_to_remove = []
    if exc == 0:
        days_to_remove = supp.remove_extremes(days_to_remove,
                                              returnsD,
                                              0.1,
                                              threshold_lower=-0.1)
        days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2)
        days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01)
        days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.1)
    elif exc == 1:
        days_to_remove = supp.remove_extremes(days_to_remove,
                                              returnsD,
                                              0.1,
                                              threshold_lower=-0.1)
        days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2)
        days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01)
        days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.1)
    elif exc == 2:
        days_to_remove = supp.remove_extremes(days_to_remove,
                                              returnsD,
                                              0.1,
                                              threshold_lower=-0.1)
        days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2)
        days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01)
        days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.1)
    elif exc == 3:
        days_to_remove = supp.remove_extremes(days_to_remove,
                                              returnsD,
                                              0.1,
                                              threshold_lower=-0.1)
        days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2)
        days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01)
        days_to_remove = supp.remove_extremes(days_to_remove, volumesD, 50000)
        days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.01)
    elif exc == 4:
        days_to_remove = supp.remove_extremes(days_to_remove,
                                              returnsD,
                                              0.1,
                                              threshold_lower=-0.1)
        days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2)
        days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01)
        days_to_remove = supp.remove_extremes(days_to_remove, volumesD, 50000)
        days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.01)
    elif exc == 5:
        days_to_remove = supp.remove_extremes(days_to_remove,
                                              returnsD,
                                              0.1,
                                              threshold_lower=-0.1)
        days_to_remove = supp.remove_extremes(days_to_remove, rvolD, 2)
        days_to_remove = supp.remove_extremes(days_to_remove, spreadD, 0.01)
        days_to_remove = supp.remove_extremes(days_to_remove, volumesD, 50000)
        days_to_remove = supp.remove_extremes(days_to_remove, illiqD, 0.01)

    for d in days_to_remove:
        time_list_removed = np.append(time_list_removed, time_listD[d])
    time_listD = np.delete(time_listD, days_to_remove)
    returnsD = np.delete(returnsD, days_to_remove)
    volumesD = np.delete(volumesD, days_to_remove)
    spreadD = np.delete(spreadD, days_to_remove)
    rvolD = np.delete(rvolD, days_to_remove)
    illiqD = np.delete(illiqD, days_to_remove)
    illiq_timeD = np.delete(illiq_timeD, days_to_remove)

    if plot_for_extreme == 1:
        plt.plot(rvolD)
        plt.title("rvol")
        plt.figure()
        plt.plot(spreadD)
        plt.title("spreadH")
        plt.figure()
        plt.plot(volumesD)
        plt.title("volume")
        plt.figure()
        plt.plot(illiqD)
        plt.title("illiq")
        plt.figure()
        plt.plot(returnsD)
        plt.title("returnsH")
        plt.show()

    # Removing all days where Roll is zero
    time_listD, time_list_removed, spreadD, volumesD, returnsD, \
    rvolD, illiqD = supp.remove_list1_zeros_from_all_lists(time_listD, time_list_removed, spreadD, volumesD, returnsD,
                                                           rvolD, illiqD)

    # Removing all days where Volatility is zero
    time_listD, time_list_removed, rvolD, volumesD, returnsD, \
    spreadD, illiqD = supp.remove_list1_zeros_from_all_lists(time_listD, time_list_removed, rvolD, volumesD, returnsD,
                                                             spreadD, illiqD)

    # Turning ILLIQ, Volume and RVol into log
    log_illiqD = np.log(illiqD)
    log_rvolD = np.log(rvolD)
    log_volumesD = volume_transformation(volumesD, mean_volume_prev_year)

    print(
        "  dis.%i: Length of time %i, spread %i, rvol %i, illiq %i, and log_illiq %i"
        % (gf(cf()).lineno, len(time_listD), len(spreadD), len(rvolD),
           len(illiqD), len(log_illiqD)))
    print(" \033[32;0;0m Finished running 'clean_series_days' ...\033[0;0;0m")

    return time_listD, returnsD, volumesD, log_volumesD, spreadD, illiqD, log_illiqD, rvolD, log_rvolD
 def _set_frame(self, kargs):
     if 'frame' in kargs:
         self.frame = kargs['frame']
     else:
         # gof gets a stack of [inner->outer] tuples. tuple[0] is frame
         self.frame = gfi(gof(cf())[2][0])
Beispiel #14
0
 def build_pkg(config, os_type, os_version):
     result = _build_package(config, os_type, os_version)
     if not result.succeeded:
         print(result.cli)
         raise RedhatError(result, os_type, os_version, frame=gfi(cf()))
Beispiel #15
0
def benchmark_hourly(Y,
                     time_listH,
                     HAR_config=0,
                     hours_in_period=4,
                     prints=1,
                     force_max_lag=0):
    hours_to_remove = []
    if hours_in_period != -1:
        X_dummies, n_dummies = time_of_day_dummies(
            time_listH,
            hours_in_period=hours_in_period)  # Dette gir dummy variable
        if prints == 1:
            print(
                "  rs.%i: x1 through x%i in the benchmark are time-based dummy variables"
                % (gf(cf()).lineno, n_dummies))
    else:
        n_dummies = 0
    n_x = n_dummies

    if HAR_config == 1:
        AR_order = 1
        max_lag = max(AR_order, force_max_lag)
        X_AR, hours_to_remove = AR_matrix(Y, time_listH, AR_order,
                                          hours_to_remove)

        X_AR = X_AR[max_lag - AR_order:, :]
        hours_to_remove = adjust_hours_for_removal(hours_to_remove,
                                                   n_hours=(max_lag -
                                                            AR_order))
        X_HAR = X_AR

        n_x += 1
        if prints == 1:
            print("  rs.%i: x%i through x%i is the X_AR(%i) model" %
                  (gf(cf()).lineno, n_x, n_x + AR_order - 1, AR_order))

    elif HAR_config == 2:  # Denne skal inkludere AR(24)
        AR_order = 5
        max_lag = max(AR_order, force_max_lag)
        X_AR, hours_to_remove = AR_matrix(Y, time_listH, AR_order,
                                          hours_to_remove)

        print(
            "   \033[32;0;0mrs.%i: %0.1f percent of hours removed due to AR()\033[0;0;0m"
            % (gf(cf()).lineno, 100 * float(len(hours_to_remove) / len(Y))))
        X_AR = X_AR[max_lag - AR_order:, :]
        hours_to_remove = adjust_hours_for_removal(hours_to_remove,
                                                   n_hours=(max_lag -
                                                            AR_order))
        X_HAR = X_AR

        n_x += 1
        if prints == 1:
            print("  rs.%i: x%i through x%i is the X_AR(%i) model" %
                  (gf(cf()).lineno, n_x, n_x + AR_order - 1, AR_order))

    elif HAR_config == 3:  # Denne skal inkludere X_AR(1) verdi 24 timer før, snitt 24 timer før
        AR_order = 1
        max_lag = max(24, force_max_lag)
        X_AR, hours_to_remove = AR_matrix(Y, time_listH, AR_order,
                                          hours_to_remove)

        # print("  rs.%i: X_AR is (%i,%i)" % (gf(cf()).lineno, np.size(X_AR,0), np.size(X_AR,1)))
        #X_AR = X_AR[max_lag - AR_order:, :]  # Hvis max lag er 24, men order=1, så vil vi kutte bort 23 entries til
        # print("  rs.%i: X_AR is (%i,%i)" % (gf(cf()).lineno, np.size(X_AR,0), np.size(X_AR,1)))
        #hours_to_remove = adjust_hours_for_removal(hours_to_remove, n_hours=(max_lag - AR_order))

        lagged_list, index_list_prev_lag, hours_to_remove = get_lagged_list(
            Y, time_listH, lag=24, hours_to_remove_prev=hours_to_remove)
        # print("  rs.%i: lagged list is (%i)" % (gf(cf()).lineno, len(lagged_list)))

        #X_lagged = np.transpose(np.matrix(lagged_list[max_lag:]))
        X_lagged = np.transpose(np.matrix(lagged_list))
        X_HAR = np.append(X_AR, X_lagged, axis=1)
        # print("  rs.%i: X_HAR is (%i,%i)" % (gf(cf()).lineno, np.size(X_HAR,0), np.size(X_HAR,1)))

        last_day_average, hours_to_remove = get_last_day_average(
            Y,
            time_listH,
            index_list_prev_lag,
            hours_to_remove_prev=hours_to_remove)
        # print("  rs.%i: last day average is (%i)" % (gf(cf()).lineno, len(last_day_average)))
        #last_day_average = np.transpose(np.matrix(last_day_average[max_lag:]))
        last_day_average = np.transpose(np.matrix(last_day_average))

        if prints == 1:
            n_x += 1
            print("  rs.%i: x%i through x%i is the X_AR(%i) model" %
                  (gf(cf()).lineno, n_x, n_x + AR_order - 1, AR_order))
            n_x += AR_order
            print("  rs.%i: x%i is the value 24 hours prior" %
                  (gf(cf()).lineno, n_x))
            n_x += 1
            print("  rs.%i: x%i is the average for the previous 24 hours" %
                  (gf(cf()).lineno, n_x))

        X_HAR = np.append(X_HAR, last_day_average, axis=1)

    elif HAR_config == 4:  # Denne skal inkludere verdi 24 timer før, 48 timer før og snitt av 48 timer
        1

    elif HAR_config == 5:  # Denne skal inkludere X_AR(1), verdi 24 timer før, 48 timer før og snitt av 48 timer
        AR_order = 1
        max_lag = max(48, force_max_lag, AR_order)

        X_AR, hours_to_remove = AR_matrix(Y, time_listH, AR_order,
                                          hours_to_remove)
        X_AR = X_AR[
            max_lag -
            AR_order:, :]  # Hvis max lag er 24, men order=1, så vil vi kutte bort 23 entries til
        hours_to_remove = adjust_hours_for_removal(hours_to_remove,
                                                   n_hours=(max_lag -
                                                            AR_order))

        lagged_list_24, index_list_prev_lag_24 = get_lagged_list(Y,
                                                                 time_listH,
                                                                 lag=24)
        X_lagged = np.transpose(np.matrix(lagged_list_24[max_lag:]))

        lagged_list_48, index_list_prev_lag_48 = get_lagged_list(Y,
                                                                 time_listH,
                                                                 lag=48)
        X_lagged = np.append(X_lagged,
                             np.transpose(np.matrix(lagged_list_48[max_lag:])),
                             axis=1)

        X_HAR = np.append(X_AR, X_lagged, axis=1)
        last_day_average = get_last_day_average(Y, time_listH,
                                                index_list_prev_lag_48)
        last_day_average = np.transpose(np.matrix(last_day_average[max_lag:]))

        if prints == 1:
            n_x += 1
            print("  rs.%i: x%i through x%i is the X_AR(%i) model" %
                  (gf(cf()).lineno, n_x, n_x + AR_order - 1, AR_order))
            n_x += AR_order
            print("  rs.%i: x%i is the value 24 hours prior" %
                  (gf(cf()).lineno, n_x))
            n_x += 1
            print("  rs.%i: x%i is the value 48 hours prior" %
                  (gf(cf()).lineno, n_x))
            n_x += 1
            print("  rs.%i: x%i is the average for the previous 48 hours" %
                  (gf(cf()).lineno, n_x))

        X_HAR = np.append(X_HAR, last_day_average, axis=1)

    if hours_in_period != -1:
        X_benchmark = X_dummies
    else:
        X_benchmark = X_HAR

    if HAR_config > 0 and hours_in_period != -1:
        X_benchmark = np.append(X_benchmark, X_HAR, axis=1)

        # if prints == 1:
        # print("  rs.%i (END): Y is: %i, X_benchmark is (%i,%i)" % (gf(cf()).lineno, len(Y), np.size(X_benchmark, 0), np.size(X_benchmark, 1)))

    return Y, X_benchmark, max_lag, hours_to_remove