예제 #1
0
def get_ytw_from_date(fromdate, srcfile=r'src/YTW-All-Values.xlsx'):
    '''
    load data from source file into dataframe
    columns:
        Corp - corporate bond rate
        TB - treasury bond rate
        CS - credit spread
        Econ - economic data
    '''

    import nb_credit_spread as cslibrary
    cslib = cslibrary.creditspread()
    return cslib.get_ytw_from_date_delta(srcfile=srcfile)

    import pandas as pd
    import cs_logger as cslog

    source_file = srcfile
    src_file = pd.read_excel(source_file,
                             sheet_name='data',
                             header=0,
                             index_col='Date')
    ytw_df = pd.DataFrame(src_file)
    ytw_df = ytw_df.asfreq(pd.infer_freq(
        ytw_df.index))  # infer data frequency; monthly

    start_date = pd.to_datetime(fromdate)
    ytw_df = ytw_df[start_date:]  # filter records by date

    cslog.debug(f"df: {ytw_df.head()}")
    return ytw_df
예제 #2
0
def __run_adf_stat_p_value(logDest):

    import datetime
    import cs_data_analysis as cs
    import cs_logger as cslog
    log = setlogfile(logDest)

    try:
        now = datetime.datetime.now()
        log.info(f"{'-'*10} start {now:%Y-%m-%d %H:%M} {'-'*10}")

        import nb_credit_spread as cslibrary
        cslib = cslibrary.creditspread()

        srcfile = r'src/YTW-All-Values.xlsx'
        df = cslib.get_ytw_from_date_delta(srcfile=srcfile, start='2009-01-31')
        cs.adf_stat_p_value(df)

        log.info(f"{'-'*10} done {'-'*10}")
        assert True
    except:
        import sys
        message = f"Unexpected error: {sys.exc_info()[0]} {sys.exc_info()[1]}"
        log_error = setlogfile(logDest, logging.ERROR)
        log_error.error(message)
        raise
예제 #3
0
def test_arima_model():
    '''
    unit test for arima
    '''
    import cs_data_analysis as cs
    import cs_logger as cslog
    import logging

    log = setlogfile(cslog.arima_model_log)

    try:
        import nb_credit_spread as cslibrary
        cslib = cslibrary.creditspread()
        srcfile = r'src/YTW-All-Values.xlsx'
        df = cslib.get_ytw_from_date_delta(srcfile=srcfile, start='2009-01-31')

        return

        log.info('**** AR(1) ****')
        cs.print_full(cs.arima_model(df), log)

        log.info('**** AR(1) diff period 1 ****')
        cs_df = cs.get_ytw_from_date(
            '1989-12-31')  # fetch month before 31 Jan 1990
        cs_df = cs_df.diff(periods=1)  # diff with previous period
        cs_df = cs_df[
            '1990-01-31':]  # get only records from 31 Jan 1990; this avoids NaN in first row
        cs.print_full(cs.arima_model(cs_df), log)  # get AR(1) coefficients
        assert True

    except:
        import sys
        message = f"Unexpected error: {sys.exc_info()[0]} {sys.exc_info()[1]}"
        log.error(message)
        raise
예제 #4
0
def test_lag_selection():
    import cs_logger as cslog
    log = setlogfile(cslog.lag_selection_log)

    try:
        import cs_data_analysis as cs
        import cs_lag_selection as lag

        import datetime
        now = datetime.datetime.now()
        log.info(f"{'-'*10} start lag selection {now:%Y-%m-%d %H:%M} {'-'*10}")

        import nb_credit_spread as cslibrary

        cslib = cslibrary.creditspread()
        srcfile = r'src/YTW-All-Values.xlsx'
        ytw = cslib.get_ytw_from_date_delta(srcfile=srcfile,
                                            start='2009-01-31')

        cs = [
            'CS-Aaa', 'CS-Aa', 'CS-A', 'CS-Baa', 'CS-DCF-Aaa', 'CS-DCF-Aa',
            'CS-DCF-A', 'CS-DCF-Baa'
        ]
        tb = ['3MO', '1YR', '5YR']

        for i in cs:
            for j in tb:
                lag.call_lag_selection(ytw, f"{i}-{j}", f"TB-{j}-TY")
                lag.call_lag_selection(ytw, f"{i}-{j}-diff", f"TB-{j}-TY")
        return
    except:
        import sys
        log.error(f"Unexpected error:", sys.exc_info()[0], sys.exc_info()[1])
        raise
예제 #5
0
def test_summary_stats_full():
    '''
    unit test for summary stats
    '''
    import cs_data_analysis as cs
    import cs_logger as cslog
    import logging

    log = setlogfile(cslog.summary_stats_full_log)

    try:
        import nb_credit_spread as cslibrary

        cslib = cslibrary.creditspread()
        srcfile = r'src/YTW-All-Values.xlsx'
        ytw_df = cslib.get_ytw_from_date_delta(srcfile=srcfile,
                                               start='2014-01-31')

        cs_stats, cs_stats_p1 = cs.summary_stats_full(ytw_df)

        log.info('**** cs stats ****')
        cs.print_full(cs_stats, log)

        log.info('**** cs stats p1 diff ****')
        cs.print_full(cs_stats_p1, log)

        assert True
    except:
        import sys
        log.error(f"Unexpected error:", sys.exc_info()[0], sys.exc_info()[1])
        raise
예제 #6
0
def get_ytw_test():
    import os
    print(os.getcwd())
    import nb_credit_spread as cslibrary
    import logging
    cslib = cslibrary.creditspread()
    srcfile = 'src/YTW-All-Values.xlsx'
    srcfile = 'YTW-All-Values.xlsx'
    srcfile = f"{os.getcwd()}/src/YTW-All-Values.xlsx"
    start_date = '2009-01-31'
    return cslib.get_ytw_from_date_delta(srcfile=srcfile, start=start_date)
예제 #7
0
def test_summary_stats():
    '''
    unit test for summary stats
    '''
    import cs_data_analysis as cs
    import cs_logger as cslog
    import logging
    import nb_credit_spread as cslibrary

    cslib = cslibrary.creditspread()
    srcfile = r'src/YTW-All-Values.xlsx'
    ytw_df = cslib.get_ytw_from_date_delta(srcfile=srcfile, start='2014-01-31')
    cs.print_full(cs.summary_stats(ytw_df),
                  setlogfile(cslog.summary_stats_log))

    assert True
예제 #8
0
def test_causation():
    '''
    '''
    import cs_data_analysis as cs
    import cs_logger as cslog

    log = setlogfile(cslog.causation_log)
    try:
        import nb_credit_spread as cslibrary

        cslib = cslibrary.creditspread()
        srcfile = r'src/YTW-All-Values.xlsx'
        ytw_df = cslib.get_ytw_from_date_delta(srcfile=srcfile,
                                               start='2009-01-31')

        cs.print_full(cs.causation(ytw_df), log)
        assert True
    except:
        import sys
        log.error(f"Unexpected error:", sys.exc_info()[0], sys.exc_info()[1])
        raise
예제 #9
0
def test_get_model_results():

    from statsmodels.tsa.arima.model import ARIMA
    import nb_credit_spread as cslibrary

    cslib = cslibrary.creditspread()
    start_date = '2009-01-31'  #'1990-01-31' # '2009-01-31'
    ytw_delta = cslib.get_ytw_from_date_delta(
        start=start_date, srcfile='src/YTW-All-Values.xlsx')
    endog_col = 'CS-Aaa-3MO-DCF'
    order = (1, 1, 0)
    endog, exog = ytw_delta[endog_col], None
    model = ARIMA(endog=endog, exog=exog, order=order, trend='ct')
    model_fit = model.fit()
    import logging
    # log = setlogging('test_get_model_results', logging.INFO)
    log = old_setlogger('test_get_model_results', logging.INFO)
    # logging.getLogger('test_get_model_results').info('test')

    log.info(model_fit.summary())

    if (log.hasHandlers()):
        log.handlers.clear()
예제 #10
0
def test_causation_cs_pce():
    import cs_data_analysis as cs
    import cs_logger as cslog

    log = setlogfile(cslog.causation_log)
    try:
        import nb_credit_spread as cslibrary

        cslib = cslibrary.creditspread()
        srcfile = r'src/YTW-All-Values.xlsx'
        ytw_df = cslib.get_ytw_from_date_delta(srcfile=srcfile,
                                               start='2009-01-31')

        df = ytw_df[['CS-Aaa-3MO', 'Econ-PCE']]
        from cs_causation import grangers_causation
        r = grangers_causation(df)
        # print(r)
        cs.print_full(r, log)

        assert True
    except:
        import sys
        log.error(f"Unexpected error:", sys.exc_info()[0], sys.exc_info()[1])
        raise