Example #1
0
def estimate_ar_coefficients(r, M):
    '''
    Runs Levinson-Durbin to compute the AR coefficients,
    reflection coefficients, and prediction error powers up to M.
    This function then uses a least squares fit to compute the 
    reflection coefficients from the AR coefficients and compares
    the results.
    '''
    # Levinson Durbin:
    _, ar_coeff, reflec_coeff, _, _ = stattools.levinson_durbin(r, M)
    delta_0 = r.mean()*np.sum(ar_coeff)
    
    print('\nLevinson Durbin Reflection coefficients:', -reflec_coeff)
    print('Levinson-durbin delta:', delta_0)

    # Least squares: (the actual regression is needed for the delta term)
    y = np.transpose(np.array(r[M:], ndmin=2))
    x = np.zeros([r.size-M, M])
    x = np.insert(x, 0, 1, axis=1) # for the delta term (y-intercept)
    for i in range(M, r.size):
        for j in range(1, M+1):
            x[i-M, j] = r[i-j]
        
    beta = np.linalg.inv(np.transpose(x) @ x) @ np.transpose(x) @ y

    ols_pacf = stattools.pacf_ols(r, M)
    print('\nLeast Squares Reflection coefficients:', -1*ols_pacf) 
    print('Least Squares delta:', np.sum(beta[1:])*r.mean(), end='\n\n')

    return np.sum((y - x @ beta)**2)/y.size
Example #2
0
def _do_levdur_array(eng, x):
    _check_1d_ndarray(x)

    err, arcoeff, _, _, _ = levinson_durbin(x)
    a_identify = np.concatenate([[1], -arcoeff])  # [1, az^-1, a^z-2, ...]
    a_identify = eng.spec_decomp(_to_matlab(a_identify))
    err_list.append(err)
    return a_identify
Example #3
0
def test_levinson_durbin_acov():
    rho = 0.9
    m = 20
    acov = rho ** np.arange(200)
    sigma2_eps, ar, pacf, _, _ = levinson_durbin(acov, m, isacov=True)
    assert_allclose(sigma2_eps, 1 - rho ** 2)
    assert_allclose(ar, np.array([rho] + [0] * (m - 1)), atol=1e-8)
    assert_allclose(pacf, np.array([1, rho] + [0] * (m - 1)), atol=1e-8)
def test_levinson_durbin_acov():
    rho = 0.9
    m = 20
    acov = rho**np.arange(200)
    sigma2_eps, ar, pacf, _, _ = levinson_durbin(acov, m, isacov=True)
    assert_allclose(sigma2_eps, 1 - rho ** 2)
    assert_allclose(ar, np.array([rho] + [0] * (m - 1)), atol=1e-8)
    assert_allclose(pacf, np.array([1, rho] + [0] * (m - 1)), atol=1e-8)
Example #5
0
def test_pacf2acf_levinson_durbin():
    pacf = -0.9 ** np.arange(11.)
    pacf[0] = 1
    ar, acf = levinson_durbin_pacf(pacf)
    _, ar_ld, pacf_ld, _, _ = levinson_durbin(acf, 10, isacov=True)
    assert_allclose(ar, ar_ld, atol=1e-8)
    assert_allclose(pacf, pacf_ld, atol=1e-8)

    # From R, FitAR, PacfToAR
    ar_from_r = [-4.1609, -9.2549, -14.4826, -17.6505, -17.5012, -14.2969, -9.5020, -4.9184,
                 -1.7911, -0.3486]
    assert_allclose(ar, ar_from_r, atol=1e-4)
def test_pacf2acf_levinson_durbin():
    pacf = -0.9 ** np.arange(11.)
    pacf[0] = 1
    ar, acf = levinson_durbin_pacf(pacf)
    _, ar_ld, pacf_ld, _, _ = levinson_durbin(acf, 10, isacov=True)
    assert_allclose(ar, ar_ld, atol=1e-8)
    assert_allclose(pacf, pacf_ld, atol=1e-8)

    # From R, FitAR, PacfToAR
    ar_from_r = [-4.1609, -9.2549, -14.4826, -17.6505, -17.5012, -14.2969, -9.5020, -4.9184,
                 -1.7911, -0.3486]
    assert_allclose(ar, ar_from_r, atol=1e-4)
Example #7
0
def train(train_data, k):
    '''
    Find the coefficients (len(coefficients) = k) for the time serie train_data
    :param train_data: the time series
    :param k: order of the AR model (number of coefficients)
    :return: array of coefficients
    '''
    # isacov need to be set to true if the first argument contains the autocovariance
    sigmav, arcoefs, pacf, sigma, phi = levinson_durbin(train_data,
                                                        nlags=k,
                                                        isacov=False)
    return arcoefs
Example #8
0
def levinson(r, n):
    """ Adapts the levinson() routine from matlab.

    Basically re-arranges the outputs from statsmodels.tsa.stattools.levinson_durbin() to match
    the matlab outputs. Includes a sign change and swapping a "1".

    For more info, see `<https://ch.mathworks.com/help/signal/ref/levinson.html?s_tid=srchtitle>`__.

    Todo:
        * fix this docstring

    """

    out = stattools.levinson_durbin(r, nlags=n, isacov=True)

    return (np.array([1] + list(-out[1])), out[0], -out[2][1:])
Example #9
0
def predictBeat(signal):
    try_signal, _ = wfdb.rdsamp(signal, channels=[0], sampfrom=0, sampto=9000)
    try_signal = try_signal.flatten()
    indexes = peakutils.indexes(try_signal, thres=0.5, min_dist=30)

    a, b = scipy.signal.butter(3, 1 / 180, btype='highpass', analog=True)
    fil1 = scipy.signal.lfilter(b, a, try_signal)
    c, d = scipy.signal.butter(3, [58 / 180, 62 / 180],
                               btype='bandstop',
                               analog=True)
    fil2 = scipy.signal.lfilter(d, c, fil1)
    e, f = scipy.signal.butter(4, 25 / 180, btype='lowpass', analog=True)
    filtered = scipy.signal.lfilter(f, e, fil2)
    minutes = (len(filtered) / (360 * 60))
    heart_rate = len(indexes) / minutes
    globalList = list()
    for i in range(len(indexes)):
        to_append = list()
        r_peak = indexes[i]
        qrs_start = r_peak - int(len_qrs / 2) + 1

        qrs_segment = filtered[qrs_start:qrs_start + len_qrs]
        stt_segment = filtered[qrs_start + len_qrs + 1:qrs_start + len_qrs +
                               len_stt]

        if len(qrs_segment) > 0:
            _, qrs_arcoeffs, _, _, _ = levinson_durbin(qrs_segment,
                                                       nlags=autoreg_ord,
                                                       isacov=False)
        else:
            qrs_arcoeffs = ['nan'] * 9

        #AR Coefficients of STT

        if len(stt_segment) > 0:
            _, stt_arcoeffs, _, _, _ = levinson_durbin(stt_segment,
                                                       nlags=autoreg_ord,
                                                       isacov=False)
        else:
            stt_arcoeffs = ['nan'] * 9

    # Pre RR and Post RR length
        if i > 0:
            pre_rr = indexes[i] - indexes[i - 1]
        else:
            pre_rr = None

        if i + 1 < len(indexes):
            post_rr = indexes[i + 1] - indexes[i]
        else:
            post_rr = None

        to_append.append(qrs_start)
        to_append.append(qrs_start + len_qrs + len_stt)
        to_append.append(pre_rr)
        to_append.append(post_rr)
        to_append.extend(qrs_arcoeffs)
        to_append.extend(stt_arcoeffs)
        if None not in to_append and 'nan' not in to_append:
            globalList.append(to_append)

    df = pd.DataFrame(globalList,
                      columns=[
                          'start', 'end', 'pre_rr', 'post_rr', 'arqrs1',
                          'arqrs2', 'arqrs3', 'arqrs4', 'arqrs5', 'arqrs6',
                          'arqrs7', 'arqrs8', 'arqrs9', 'arstt1', 'arstt2',
                          'arstt3', 'arstt4', 'arstt5', 'arstt6', 'arstt7',
                          'arstt8', 'arstt9'
                      ])
    test = df[[
        'pre_rr', 'post_rr', 'arqrs1', 'arqrs2', 'arqrs3', 'arqrs4', 'arqrs5',
        'arqrs6', 'arqrs7', 'arqrs8', 'arqrs9', 'arstt1', 'arstt2', 'arstt3',
        'arstt4', 'arstt5', 'arstt6', 'arstt7', 'arstt8', 'arstt9'
    ]]
    test = scale(test)
    predictions = clf.predict(test)
    predictions = list(predictions)
    pred_dict = dict(Counter(predictions))

    if 0 in pred_dict.keys():
        pred_dict['Normal'] = pred_dict.pop(0)
    if 1 in pred_dict.keys():
        pred_dict['Premature Beats'] = pred_dict.pop(1)
    if 2 in pred_dict.keys():
        pred_dict['Escape Beats'] = pred_dict.pop(2)
    if 3 in pred_dict.keys():
        pred_dict['Fusion Beats'] = pred_dict.pop(3)
    if 4 in pred_dict.keys():
        pred_dict['Unrecognized'] = pred_dict.pop(4)
    if 5 in pred_dict.keys():
        pred_dict['Bundled Branch Block Beat'] = pred_dict.pop(5)

    fig = matplotlib.pyplot.figure(figsize=(10, 7))
    matplotlib.pyplot.pie(pred_dict.values(),
                          labels=pred_dict.keys(),
                          autopct='%1.0f%%')
    matplotlib.pyplot.savefig('images\pie.jpg', bbox_inches='tight')
    matplotlib.pyplot.close()

    for ind in df.index:
        if predictions[ind] != 0:
            sig = filtered[df['start'][ind]:df['end'][ind]]
            matplotlib.pyplot.axis('off')
            matplotlib.pyplot.plot(sig)
            matplotlib.pyplot.savefig('images\\' + str(ind) + '.jpg',
                                      bbox_inches='tight')
            matplotlib.pyplot.close()
    keymax = max(pred_dict, key=pred_dict.get)
    print('Your maximum beats are: ', keymax)
    print('Heart Rate: ', heart_rate)

    ans['beats'] = keymax
    ans['rate'] = int(heart_rate)

    return ans
def extract_features (record_path, length_qrs, length_stt, ar_order_qrs, ar_order_stt, sampfrom=0, sampto=-1, use_filter=True):

    """
    A list holding tuples with values 'N' or 'VEB', and the length in samples of each corresponding QRS
    and ST/T complexes, plus the length in samples of pre- and post-RR
    """
    print(record_path)
    qrs_stt_rr_list = list()
    sampto = 1000
    #print(sampto)

    if sampto < 0:
        raw_signal, _ = wfdb.rdsamp(record_path, channels=[0], sampfrom=sampfrom, sampto="end")
        annotations = wfdb.rdann(record_path, extension="atr", sampfrom=sampfrom, sampto=None)
    else:
        raw_signal= wfdb.rdsamp(record_path, channels=[0], sampfrom=sampfrom, sampto=sampto)
        annotations = wfdb.rdann(record_path, extension="atr", sampfrom=sampfrom, sampto=sampto)

    raw_signal = raw_signal.reshape(-1)
    
    # Filtering
    if use_filter:
        filter_1 = butter_filter(raw_signal, filter_type="highpass", order=3, cutoff_freqs=[1], sampling_freq=annotations.fs)
        filter_2 = butter_filter(filter_1, filter_type="bandstop", order=3, cutoff_freqs=[58, 62], sampling_freq=annotations.fs)
        signal = butter_filter(filter_2, filter_type="lowpass", order=4, cutoff_freqs=[25], sampling_freq=annotations.fs)
    else:
        signal = raw_signal
    
    annotation2sample = list(zip(annotations.symbol, annotations.sample))

    for idx, annot in enumerate(annotation2sample):
        beat_type       = annot[0]    # "N", "V", ... etc.
        r_peak_pos      = annot[1]    # The R peak position
        pulse_start_pos = r_peak_pos - int(length_qrs / 2) + 1    # The sample postion of pulse start (start of QRS)

        # We treat only Normal, VEB, and SVEB signals 
        print(beat_type)
        if beat_type == "N" or beat_type == "S" or beat_type == "V":
            qrs = signal[pulse_start_pos : pulse_start_pos + length_qrs]
            stt = signal[pulse_start_pos + length_qrs + 1 : pulse_start_pos + length_qrs + length_stt]
            #print(qrs.size)
            if qrs.size > 0:
                _, qrs_arcoeffs, _, _, _ = levinson_durbin(qrs, nlags=ar_order_qrs, isacov=False)
            else:
                qrs_arcoeffs = None
            #print(stt.size)  
            if stt.size > 0:
                #print(stt.shape)
                _, stt_arcoeffs, _, _, _ = levinson_durbin(stt, nlags=ar_order_stt, isacov=False)
            else:
                stt_arcoeffs = None

            pre_rr_length  = annotation2sample[idx][1] - annotation2sample[idx - 1][1] if idx > 0 else None
            post_rr_length = annotation2sample[idx + 1][1] - annotation2sample[idx][1] if idx + 1 < annotations.ann_len  else None
            _type = 1 if beat_type == "V" else 0

            """
            beat_dict = OrderedDict([("record", record_path.rsplit(sep="/", maxsplit=1)[-1]), ("type", _type),
                                     ("QRS", qrs), ("ST/T", stt),
                                     ("QRS_ar_coeffs", qrs_arcoeffs), ("ST/T_ar_coeffs", stt_arcoeffs),
                                     ("pre-RR", pre_rr_length), ("post-RR", post_rr_length)])
            """
            beat_list = list()
            beat_list = [("record", record_path.rsplit(sep="/", maxsplit=1)[-1]), ("type", _type), 
                         # ("QRS", qrs), ("ST/T", stt),
                         # ("QRS_ar_coeffs", qrs_arcoeffs), ("ST/T_ar_coeffs", stt_arcoeffs),
                         ("pre-RR", pre_rr_length), ("post-RR", post_rr_length)
                        ]
            #print(qrs_arcoeffs)
            #print('gdlgh')
            for idx, coeff in enumerate(qrs_arcoeffs):
                beat_list.append(("qrs_ar{}".format(idx), coeff))
            print(stt_arcoeffs)
            for idx, coeff in enumerate(stt_arcoeffs):
                beat_list.append(("stt_ar{}".format(idx), coeff))
            
            beat_dict = OrderedDict(beat_list)
            
            qrs_stt_rr_list.append(beat_dict)
    return qrs_stt_rr_list
Example #11
0
def play():

    p = pyaudio.PyAudio()

    CHUNK = 1024
    PRE_EMP_COEF = 0.8

    for i in range(p.get_device_count()):
        print(p.get_device_info_by_index(i))

    carrier_path = 'wavs/carrier_2.wav'
    modulator_path = 'wavs/modulator_2.wav'

    mod_wave = wave.open(modulator_path, 'rb')
    carr_wave = wave.open(carrier_path, 'rb')

    FORMAT = p.get_format_from_width(carr_wave.getsampwidth())
    CHANNELS = carr_wave.getnchannels()
    RATE = carr_wave.getframerate()

    out_stream = p.open(format=pyaudio.paFloat32,
                    channels=CHANNELS,
                    rate=RATE,
                    output=True,
                    frames_per_buffer=CHUNK,
                    input_device_index=5)
    data = []
    carr_buffer = Buffer.Buffer(CHUNK)
    mod_buffer = Buffer.Buffer(CHUNK)
    out_buffer = Buffer.Buffer(CHUNK)
    window = signal.windows.hann(2*CHUNK)
    int16max = np.iinfo(np.int16).max
    for i in range(0, 100):
        carr_frame = carr_wave.readframes(1024)
        mod_frame = mod_wave.readframes(1024)
        carr_frame = np.frombuffer(carr_frame, np.int16)/int16max
        mod_frame = np.fromstring(mod_frame, np.int16)/int16max
        carr_buffer.add_new_chunk(carr_frame)
        mod_buffer.add_new_chunk(mod_frame)

        carr_signal = carr_buffer.get_whole_buffer()
        mod_signal = mod_buffer.get_whole_buffer()

        carr_rms = sqrt(mean(square(carr_signal)))
        mod_rms = sqrt(mean(square(carr_signal)))
        mod_signal = signal.lfilter([1, -PRE_EMP_COEF], 1, mod_signal)
        #plt.plot(mod_signal)

        X = np.fft.fft(mod_signal)
        R = np.fft.ifft(np.square(np.abs(X)))
        R2 = np.real(R)/len(X)
        ret = levinson_durbin(R2, nlags=20, isacov=False)
        error = ret[0]
        lpc_coef = ret[2]
        plt.plot(lpc_coef)
        output_signal = signal.lfilter([error], lpc_coef,  carr_signal)
        plt.plot(carr_signal)
        plt.plot(output_signal)

        out_rms = sqrt(mean(square(output_signal)))
        gain_factor = mod_rms/out_rms
        output_signal_windowed = np.float32(output_signal * window * gain_factor)


        out_buffer.add_to_whole_buffer(output_signal_windowed)
        out = out_buffer.get_old_chunk()
        out_stream.write(out.astype(np.float32).tobytes())

        carr_buffer.move_chunks()
        mod_buffer.move_chunks()
        out_buffer.move_chunks()

    out_stream.stop_stream()
    out_stream.close()
    p.terminate()
Example #12
0
def extract_features(record_path,
                     length_qrs,
                     length_stt,
                     ar_order_qrs,
                     ar_order_stt,
                     sampfrom=0,
                     sampto=-1,
                     use_filter=True):
    """
    A list holding tuples with values 'N' or 'VEB', and the length in samples of each corresponding QRS
    and ST/T complexes, plus the length in samples of pre- and post-RR
    """
    qrs_stt_rr_list = list()

    #These signals are read using library of wfdb (waveform database). It uses rdsamp function inorder to read the sample file. Input to this function is path of file channel in which recording was done, and start point of sample index from which we need to read the files
    #Likewise the corresponding atr file is also read using rdann function. This function will return the corresponding annotations

    if sampto < 0:
        raw_signal, _ = wfdb.rdsamp(record_path,
                                    channels=[0],
                                    sampfrom=sampfrom)
        annotations = wfdb.rdann(record_path,
                                 extension="atr",
                                 sampfrom=sampfrom,
                                 sampto=None)
    else:
        raw_signal, _ = wfdb.rdsamp(record_path,
                                    channels=[0],
                                    sampfrom=sampfrom,
                                    sampto=sampto)
        annotations = wfdb.rdann(record_path,
                                 extension="atr",
                                 sampfrom=sampfrom,
                                 sampto=sampto)

    #Here reshape is used to change 2D array to single array. That is it will bring 2D array to 1D array of row major side.

    raw_signal = raw_signal.reshape(-1)

    # Filtering
    if use_filter:
        filter_1 = butter_filter(raw_signal,
                                 filter_type="highpass",
                                 order=3,
                                 cutoff_freqs=[1],
                                 sampling_freq=annotations.fs)
        filter_2 = butter_filter(filter_1,
                                 filter_type="bandstop",
                                 order=3,
                                 cutoff_freqs=[58, 62],
                                 sampling_freq=annotations.fs)
        signal = butter_filter(filter_2,
                               filter_type="lowpass",
                               order=4,
                               cutoff_freqs=[25],
                               sampling_freq=annotations.fs)
    else:
        signal = raw_signal

    annotation2sample = list(zip(annotations.symbol, annotations.sample))

    for idx, annot in enumerate(annotation2sample):
        beat_type = annot[0]  # "N", "V", ... etc.
        r_peak_pos = annot[1]  # The R peak position
        pulse_start_pos = r_peak_pos - int(
            length_qrs /
            2) + 1  # The sample postion of pulse start (start of QRS)

        # We treat only Normal, VEB, and SVEB signals (See the paper)
        if beat_type == "N" or beat_type == "S" or beat_type == "V":
            qrs = signal[pulse_start_pos:pulse_start_pos + length_qrs]
            stt = signal[pulse_start_pos + length_qrs + 1:pulse_start_pos +
                         length_qrs + length_stt]

            if qrs.size > 0:
                _, qrs_arcoeffs, _, _, _ = levinson_durbin(qrs,
                                                           nlags=ar_order_qrs,
                                                           isacov=False)
            else:
                qrs_arcoeffs = None

            if stt.size > 0:
                _, stt_arcoeffs, _, _, _ = levinson_durbin(stt,
                                                           nlags=ar_order_stt,
                                                           isacov=False)
            else:
                stt_arcoeffs = None

            pre_rr_length = annotation2sample[idx][1] - annotation2sample[
                idx - 1][1] if idx > 0 else None
            post_rr_length = annotation2sample[idx + 1][1] - annotation2sample[
                idx][1] if idx + 1 < annotations.ann_len else None

            _type = 1 if beat_type == "V" else 0

            beat_list = list()
            beat_list = [("record", record_path.rsplit(sep="/",
                                                       maxsplit=1)[-1]),
                         ("type", _type), ("pre-RR", pre_rr_length),
                         ("post-RR", post_rr_length)]
            for idx, coeff in enumerate(qrs_arcoeffs):
                beat_list.append(("qrs_ar{}".format(idx), coeff))
            for idx, coeff in enumerate(stt_arcoeffs):
                beat_list.append(("stt_ar{}".format(idx), coeff))

            beat_dict = OrderedDict(beat_list)

            qrs_stt_rr_list.append(beat_dict)
    return qrs_stt_rr_list
Example #13
0
def RegressionAnalysis(df, Independent, Explanatory, Indicators, prefix=None):
    """
    This function performs regression models, comparaison between series

    Arguments:
    ----------
        - df: Pandas DataFrame
            Contains the data to be analyzed
        - Independent: str
            The name of column in df for the Independent variable data
        - Explanatory: str or list
            The name of the column in df for the Explanatory variable data. In case of a multivariate analysis, needed to pass a list object of all column names.
        - Indicators: list
            The list of the indicators/models names to compute
    Return:
    ----------
        - df: Pandas DataFrame
            - Contains the initial df and all series indicators are added like the Residuals or the Fitted Values
        - OneValueIndicators: Pandas DataFrame
            - Contains all the indicators calculated with only one value like the FTest or the TTest

    """

    if Indicators == None:
        Indicators = [
            "OLS", "GLSAR", "RecursiveLS", "Yule Walker Order 1",
            "Yule Walker Order 2", "Yule Walker Order 3", "Burg Order 1",
            "Burg Order 2", "Burg Order 3", "QuantReg", "GLM Binomial",
            "GLM Gamma", "GLM Gaussian", "GLM Inverse Gaussian",
            "GLM Negative Binomial", "GLM Poisson", "GLM Tweedie"
            "AR", "ARMA", "ARIMA", "Granger Causality", "Levinson Durbin",
            "Cointegration"
        ]

    # Pre-processing
    Independent = df[Independent]
    Independent = pd.DataFrame(Independent)

    Explanatory = df[Explanatory]
    Explanatory = pd.DataFrame(Explanatory)

    y_sm = np.array(Independent).reshape((-1, 1))

    x_sm = np.array(Explanatory)
    x_sm = sm.add_constant(x_sm)

    NumDecimal = 3  # Number of decimals for rounding numbers

    OneValueIndicators = {}

    if prefix == None:
        prefix = ""

    ##################################################
    ##### PART 1: Linear Regression
    ##################################################
    """
    ########## Section 1: OLS
    """
    name = "OLS"

    if name in Indicators:
        name = prefix + name

        model = sm.OLS(y_sm, x_sm)
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 2: WLS
    """

    ### Not Implemented
    """
    ########## Section 3: GLS
    """

    ### Not Implemented
    """
    ########## Section 4: GLSAR
    """

    name = "GLSAR"

    if name in Indicators:
        name = prefix + name

        model = sm.GLSAR(y_sm, x_sm, 1)
        results = model.iterative_fit(1)

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 5: RLS
    """

    name = "RecursiveLS"

    if name in Indicators:
        name = prefix + name

        model = sm.RecursiveLS(y_sm, x_sm)
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators[name + " Z Value"] = results.zvalues

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)

        # Cumsum
        # Not Implemented
    """
    ########## Section 6: Yule Walker ORder 1
    """
    name = "Yule Walker Order 1"

    if name in Indicators and len(Explanatory.columns) == 1:
        name = prefix + name

        rho, sigma = statsmodels.regression.linear_model.yule_walker(
            x_sm[:, 1].flatten(), order=1)

        ### One Value Indicators

        # Rho
        OneValueIndicators[name + " Rho"] = round(rho[0], NumDecimal)

        # Sigma
        OneValueIndicators[name + " Sigma"] = round(sigma, NumDecimal)
    """
    ########## Section 7: Yule Walker ORder 2
    """
    name = "Yule Walker Order 2"

    if name in Indicators and len(Explanatory.columns) == 1:
        name = prefix + name

        rho, sigma = statsmodels.regression.linear_model.yule_walker(
            x_sm[:, 1].flatten(), order=2)

        ### One Value Indicators

        # Rho
        OneValueIndicators[name + " Rho"] = round(rho[0], NumDecimal)

        # Sigma2
        OneValueIndicators[name + " Sigma"] = round(sigma, NumDecimal)
    """
    ########## Section 8: Yule Walker ORder 3
    """
    name = "Yule Walker Order 3"

    if name in Indicators and len(Explanatory.columns) == 1:
        name = prefix + name

        rho, sigma = statsmodels.regression.linear_model.yule_walker(
            x_sm[:, 1].flatten(), order=3)

        ### One Value Indicators

        # Rho
        OneValueIndicators[name + " Rho"] = round(rho[0], NumDecimal)

        # Sigma
        OneValueIndicators[name + " Sigma"] = round(sigma, NumDecimal)
    """
    ########## Section 9: Burg's AR(p) ORder 1
    """

    name = "Burg Order 1"

    if name in Indicators and len(Explanatory.columns) == 1:
        name = prefix + name

        rho, sigma2 = statsmodels.regression.linear_model.burg(
            x_sm[:, 1].flatten(), order=1)

        ### One Value Indicators

        # Rho
        OneValueIndicators[name + " Rho"] = round(rho[0], NumDecimal)

        # Sigma2
        OneValueIndicators[name + " Sigma2"] = round(sigma2, NumDecimal)
    """
    ########## Section 10: Burg's AR(p) ORder 2
    """

    name = "Burg Order 2"

    if name in Indicators and len(Explanatory.columns) == 1:
        name = prefix + name

        rho, sigma2 = statsmodels.regression.linear_model.burg(
            x_sm[:, 1].flatten(), order=2)

        ### One Value Indicators

        # Rho
        OneValueIndicators[name + " Rho"] = round(rho[0], NumDecimal)

        # Sigma2
        OneValueIndicators[name + " Sigma2"] = round(sigma2, NumDecimal)
    """
    ########## Section 11: Burg's AR(p) ORder 3
    """

    name = "Burg Order 3"

    if name in Indicators and len(Explanatory.columns) == 1:
        name = prefix + name

        rho, sigma2 = statsmodels.regression.linear_model.burg(
            x_sm[:, 1].flatten(), order=3)

        ### One Value Indicators

        # Rho
        OneValueIndicators[name + " Rho"] = round(rho[0], NumDecimal)

        # Sigma2
        OneValueIndicators[name + " Sigma2"] = round(sigma2, NumDecimal)
    """
    ########## Section 12: Quantile Regression
    """

    name = "QuantReg"

    if name in Indicators:
        name = prefix + name

        model = sm.QuantReg(y_sm, x_sm)
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)

    ##################################################
    ##### PART 2: Generalized Linear Models
    ##################################################
    """
    ########## Section 1: GLM Binomial
    """

    name = "GLM Binomial"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.Binomial())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 2: GLM Gamma
    """

    name = "GLM Gamma"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.Gamma())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 3: GLM Gaussian
    """

    name = "GLM Gaussian"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.Gaussian())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 3: GLM InverseGaussian
    """

    name = "GLM Inverse Gaussian"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.InverseGaussian())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 4: GLM NegativeBinomial
    """

    name = "GLM Negative Binomial"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.NegativeBinomial())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 5: GLM Poisson
    """

    name = "GLM Poisson"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.Poisson())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)
    """
    ########## Section 6: GLM Tweedie
    """

    name = "GLM Tweedie"

    if name in Indicators:
        name = prefix + name

        model = sm.GLM(y_sm, x_sm, family=sm.families.Tweedie())
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators["Pearson chi2"] = round(results.pearson_chi2,
                                                   NumDecimal)

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)

    ##################################################
    ##### PART 3: Robust Linear Models
    ##################################################

    ##################################################
    ##### PART 4: AR models
    ##################################################

    name = "AR"

    if name in Indicators:
        name = prefix + name

        model = statsmodels.tsa.ar_model.AR(Independent)
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators[name + " Final Prediction Error"] = results.fpe

        OneValueIndicators[
            name + " Hannan-Quinn Information Criterion"] = results.hqic

        OneValueIndicators[name + " Roots"] = results.roots

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)

    ##################################################
    ##### PART 5: ARMA
    ##################################################

    name = "ARMA"

    if name in Indicators:

        name = prefix + name

        model = statsmodels.tsa.arima_model.ARMA(y_sm, (5, 5), x_sm)
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators[name + " AR Params"] = results.arparams

        OneValueIndicators[name + " AR Roots"] = results.arroots

        OneValueIndicators[name + " AR Freq"] = results.arfreq

        OneValueIndicators[
            name + " Hannan-Quinn Information Criterion"] = results.hqic

        OneValueIndicators[name + " MA Params"] = results.maparams

        try:
            OneValueIndicators[name + " MA Roots"] = results.maroots
        except:
            pass

        try:
            OneValueIndicators[name + " MA Freq"] = results.mafreq
        except:
            pass

        OneValueIndicators[name + " Sigma2"] = results.sigma2

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)

    ##################################################
    ##### PART 6: ARIMA
    ##################################################

    name = "ARIMA"

    if name in Indicators:

        name = prefix + name

        model = statsmodels.tsa.arima_model.ARIMA(Independent, (2, 2, 2),
                                                  Explanatory)
        results = model.fit()

        ### One Value Indicators

        OneValueIndicators = Statsmodels_Regression_All_OneValueIndicators(
            OneValueIndicators, name, results, Explanatory, NumDecimal)

        OneValueIndicators[name + " AR Params"] = results.arparams

        OneValueIndicators[name + " AR Roots"] = results.arroots

        OneValueIndicators[name + " AR Freq"] = results.arfreq

        OneValueIndicators[
            name + " Hannan-Quinn Information Criterion"] = results.hqic

        OneValueIndicators[name + " MA Params"] = results.maparams

        OneValueIndicators[name + " MA Roots"] = results.maroots

        OneValueIndicators[name + " MA Freq"] = results.mafreq

        OneValueIndicators[name + " Sigma2"] = results.sigma2

        ### Time Series Indicators

        # Fitted Values
        df = Statsmodels_FittedValues(df, results, name)

        # Residuals
        df = Statsmodels_LR_Residuals(df, results, name)

        ##################################################
        ##### PART 7: Univariate Analysis
        ##################################################

        # Granger Causality
        name = "Granger Causality"
        name = prefix + name
        if name in Indicators:
            OneValueIndicators[name] = ts.grangercausalitytests(
                Independent.merge(Explanatory,
                                  how="inner",
                                  left_index=True,
                                  right_index=True),
                maxlag=10)

        # Levinson Durbin
        name = "Levinson Durbin"
        name = prefix + name
        if name in Indicators:
            OneValueIndicators[name] = ts.levinson_durbin(Independent)

        # Cointegration
        name = "Cointegration"
        name = prefix + name
        if name in Indicators:
            OneValueIndicators[name] = ts.coint(Independent,
                                                Explanatory,
                                                trend="ct",
                                                return_results=False)

    ##################################################
    ##### Not Implemented
    ##################################################

    # BDS Statistic (residuals analysis)
    # Not Implemented

    # Return’s Ljung-Box Q Statistic (AR)
    # Not Implemented
    OneValueIndicators = pd.DataFrame.from_dict(OneValueIndicators,
                                                orient="index")

    return df, OneValueIndicators