def main(filename, seed, fntSize=18):
    fgsz = (6, 6)
    results = {}
    ns = range(2, 13)  # number of states
    plt.rcParams.update({'font.size': fntSize})

    for n in ns:
        with open(f"{filename}_{n}_{seed}.json") as f:
            results[n] = json.load(f)
    ylabels = ['log-likelihood', 'K']
    scales = [1, 1]
    for i, idx in enumerate(['likelihood', 'K-S']):
        fig = plt.figure(figsize=fgsz)
        ax1 = fig.add_subplot(111)
        fig.subplots_adjust(left=0.2)
        training = [results[n][idx + f'_100'][0] for n in ns]
        test = [results[n][idx + f'_100'][1] for n in ns]
        ax1.plot(ns, training, '-.')
        ax1.plot(ns, test, ':')
        ax1.legend(['Training', 'Test'])
        ax1.set_xlabel('n')
        ax1.xaxis.set_major_locator(MaxNLocator(integer=True))
        ax1.set_ylabel(ylabels[i])
        fmmts = matplotlib.ticker.ScalarFormatter()
        fmmts.set_powerlimits((-4, 3))
        ax1.yaxis.set_major_formatter(fmmts)
        auxFs.clean_axes(ax1)
        if i == 1:
            ax1.set_ylim(0, 0.07)
        fig.savefig(f"{filename}_{idx}.pdf")
Example #2
0
def main(fileName, fntSize=16):
    numLags = 60
    ds = np.loadtxt(f"{fileName}.txt").transpose()
    trDs, valDs = auxFs.divide_data(ds)
    data = valDs.flatten(order="C")
    lengthdata = data.shape[0]
    plt.rcParams.update({'font.size': fntSize})
    cols = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']
    lnstyle = ['-', ':', '-.', '--']
    fig1 = plt.figure(figsize=(8, 8))
    fig2 = plt.figure(figsize=(8, 8))
    ax1 = fig1.add_subplot(111, position=[0.175, 0.1, 0.65, 0.65])
    ax2 = fig2.add_subplot(111, position=[0.175, 0.1, 0.65, 0.65])
    xKde = np.linspace(0.0, 1.5, num=150)
    ax1.plot(xKde, kde_fit_estimate(data, xKde), 'k')
    acfTst = acf(data, nlags=numLags)
    maeDict = {}
    ax2.plot(acf(trDs.flatten(order="c"), nlags=numLags),
             color='k',
             linestyle='-')
    ax2.plot(acfTst, color='k', linestyle=':')
    scales = [20, 30, 40, 50, 60, 80, 100]
    filenames = [f"{fileName}_3_{i}_fixedMeans.txt" for i in scales]
    for i, v in enumerate(filenames):
        X = np.loadtxt(v)
        ax1.plot(xKde,
                 kde_fit_estimate(X.flatten(), xKde),
                 linestyle=lnstyle[i % 3],
                 color=cols[i % 10])
        acfModel = auxFs.ACF(X.flatten(), numLags)
        ax2.plot(acfModel, linestyle=lnstyle[i % 3], color=cols[i % 10])
        maeDict[str(scales[i] / 100)] = mean_abs_error(acfTst, acfModel)

    legendTxt = ["Test data"
                 ] + [r"$\phi$" + f" = {i/100:0.2f}" for i in scales]
    ax1.legend(legendTxt,
               loc='lower center',
               bbox_to_anchor=(0.5, 1.01),
               ncol=4)
    ax1.set_xlim(0, 1.75)
    ax1.set_ylim(0, 4.0)
    ax1.set_ylabel("PDF")
    ax1.set_xlabel("CSI")
    auxFs.clean_axes(ax1)
    ax2.legend(["Training data"] + legendTxt,
               loc='lower center',
               bbox_to_anchor=(0.5, 1.01),
               ncol=3)
    ax2.set_xlim(0, 60)
    ax2.set_ylabel("ACF")
    ax2.set_xlabel("lags (minute)")
    auxFs.clean_axes(ax2)
    fig1.savefig(f"{fileName}_scaled_histograms.pdf")
    fig2.savefig(f"{fileName}_scaled_acf.pdf")
    with open(f"{fileName}_acf_mae_scaled.json", 'w') as f:
        json.dump(maeDict, f, indent=4)
Example #3
0
def main(fileName, ymax, fntSize=16, numLags=60, fgSz=(6, 6)):
    ds = np.loadtxt(f"{fileName}.txt").transpose()
    trDs, valDs = auxFs.divide_data(ds)
    lengthTr = auxFs.calculate_length_sequence(trDs)
    lengthVal = auxFs.calculate_length_sequence(valDs)
    trDs = trDs.reshape(-1, 1)
    valDs = valDs.reshape(-1, 1)
    dataSets = [trDs, valDs]
    lengths = [lengthTr, lengthVal]

    sample = np.loadtxt(f"{fileName}_3_100_fixedMeans.txt")

    fig = plt.figure(figsize=fgSz)
    plt.rcParams.update({'font.size': fntSize})
    ax = fig.add_subplot(111)
    acfTrnData = acf(trDs, nlags=numLags)
    acfTstData = acf(valDs, nlags=numLags)
    acfSample = auxFs.ACF(sample, numLags)
    ax.plot(acfTrnData, color='k', linestyle='-')
    ax.plot(acfTstData, color='k', linestyle=':')
    ax.plot(acfSample)
    ax.set_xlabel("lags (minute)")
    ax.set_ylabel("ACF")
    ax.set_xlim(0, 60)

    auxFs.clean_axes(ax)
    ax.legend(["Training data", "Test data", "model sample"])
    fig.savefig(f"{fileName}_acf_fixedMeans.pdf")
    print(mean_abs_error(acfTstData, acfSample))

    histFigure = plt.figure(figsize=(6, 6))
    ax2 = histFigure.add_subplot(111)
    ax2.hist(valDs, 100, density=True)

    def do_on_axes(ax):
        ax.set_xlabel("CSI")
        ax.set_ylabel("PDF")
        auxFs.clean_axes(ax)
        ax.set_xlim(0, 1.75)
        ax.set_ylim(top=ymax)
        #ax.set_xticklabels([])
        #ax.set_yticklabels([])

    do_on_axes(ax2)
    histFigure.savefig(f"{fileName}_histogram_fixedMeans_test.pdf")

    hist2 = plt.figure(figsize=(6, 6))
    ax2 = hist2.add_subplot(111)
    ax2.hist(sample, 100, density=True)
    do_on_axes(ax2)
    hist2.savefig(f"{fileName}_histogram_fixedMeans_sample.pdf")
def main(fileName, ymax=4.0, fntSize=14):
	nbins = 100
	seed = 100

	ds = np.loadtxt(f"{fileName}.txt").transpose()
	trDs, valDs = auxFs.divide_data(ds)
	data = valDs.flatten(order="C")
	lengthdata = data.shape[0]
	plt.rcParams.update({'font.size': fntSize})


	# Plot histogram
	fig = plt.figure(figsize=(6,8.5))
	ax = plt.subplot(2,4,1)
	ax.hist(data, nbins, density=True)
	ax.set_ylabel("PDF")
	#ax.set_xlabel("CSI")
	ax.set_title("Test data")
	ax.set_xlim(0,1.75)
	ax.set_ylim(0,ymax)
	ax.set_yticklabels([])
	ax.set_xticklabels([])
	auxFs.clean_axes(ax)

	scales = [20, 30, 40, 50, 60, 80, 100]
	filenames = [f"{fileName}_3_{i}_fixedMeans.txt" for i in scales]
	for i, v in enumerate(filenames):
		X = np.loadtxt(v)
		#fig = plt.figure(figsize=(6,6))
		ax = plt.subplot(2,4, i+2)
		ax.hist(X, nbins, density=True)
		if i in [3]:
			ax.set_ylabel("PDF")
		if i in range(3,8):
			ax.set_xlabel("CSI")
		ax.set_title(r"$\phi$ = "+ f"{scales[i]/100:.2f}")
		#ax.set_xlabel("CSI")
		auxFs.clean_axes(ax)
		ax.set_xlim(0,1.75)
		ax.set_ylim(0,ymax)
		ax.set_yticklabels([])
		ax.set_xticklabels([])
	fig.savefig(f"{fileName}_scaled_process_histogram.pdf")
Example #5
0
def main(fntSize=18):
    maeDict1 = read_json(f"Ahawaii_acf_mae.json")
    maeDict2 = read_json(f"A_acf_mae.json")
    labels = ['Hawaii', 'Norrköping']
    ns = range(2,13)
    fgsz = (6,6)
    seed = 100
    plt.rcParams.update({'font.size': fntSize})

    trainTimeHawaii, trainTimeNorr = {}, {}
    for n in ns:
        trainTimeHawaii[n] = read_json(f"Ahawaii_{n}_{seed}.json")
        trainTimeNorr[n] = read_json(f"A_{n}_{seed}.json")

    # Plot the acf error as function of the state
    fig1 = plt.figure(figsize=fgsz)
    ax1 = fig1.add_subplot(111)
    fig1.subplots_adjust(left=0.2)
    ax1.plot(ns, maeDict1.values())
    ax1.plot(ns, maeDict2.values())
    ax1.set_xlabel('n')
    ax1.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax1.set_ylabel('ACF MAE')
    ax1.legend(labels)
    auxFs.clean_axes(ax1)
    ax1.set_ylim(0)
    fig1.savefig(f"acf_mae.pdf")

    # Plot the training time
    fig2 = plt.figure(figsize=fgsz)
    ax2 = fig2.add_subplot(111)
    fig2.subplots_adjust(left=0.2)
    timeH = [trainTimeHawaii[n][f'train_time_100'] for n in ns]
    timeN = [trainTimeNorr[n][f'train_time_100'] for n in ns]
    ax2.plot(ns, timeH)
    ax2.plot(ns, timeN)
    ax2.set_xlabel('n')
    ax2.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax2.legend(labels)
    ax2.set_ylabel('Training time (second)')
    auxFs.clean_axes(ax2)
    ax2.set_ylim(0)
    fig2.savefig(f"train_time.pdf")
def main(fileName, ymax=3.5, fntSize=14):
    nbins = 100
    seed = 100

    ds = np.loadtxt(f"{fileName}.txt").transpose()
    trDs, valDs = auxFs.divide_data(ds)
    data = valDs.flatten(order="C")
    lengthdata = data.shape[0]
    plt.rcParams.update({'font.size': fntSize})

    # Plot histogram
    fig = plt.figure(figsize=(6, 8.5))
    ax = plt.subplot(4, 3, 1)
    ax.hist(data, nbins, density=True)
    ax.set_ylabel("PDF")
    #ax.set_xlabel("CSI")
    ax.set_title("Test data")
    ax.set_xlim(0, 1.75)
    ax.set_ylim(0, ymax)
    ax.set_yticklabels([])
    ax.set_xticklabels([])
    auxFs.clean_axes(ax)

    for i in range(2, 13):
        fn = f"{fileName}_{i}_100.txt"
        X = np.loadtxt(fn)
        #fig = plt.figure(figsize=(6,6))
        ax = plt.subplot(4, 3, i)
        ax.hist(X, nbins, density=True)
        if i in [4, 7, 10]:
            ax.set_ylabel("PDF")
        if i in [10, 11, 12]:
            ax.set_xlabel("CSI")
        ax.set_title(f"n = {i}")
        #ax.set_xlabel("CSI")
        auxFs.clean_axes(ax)
        ax.set_xlim(0, 1.75)
        ax.set_ylim(0, ymax)
        ax.set_yticklabels([])
        ax.set_xticklabels([])
    fig.savefig(f"{fileName}_test_histogram.pdf")
def main(fntSize=18):
    plt.rcParams.update({'font.size': fntSize})
    numLags = 60
    length = 9000
    mu = [0.433, 0.982, 1.025]
    variance = [0.0312, 0.00032, 0.016]

    samples = [
        np.random.normal(mu[i], np.sqrt(variance[i]), int(length / 3))
        for i in range(3)
    ]
    series1 = np.concatenate(samples)
    #series1.reshape(3,-1).flatten(order='F') # flatten row wise
    fig = plt.figure(figsize=(10.5, 10.5 * 9 / 16))
    ax1 = fig.add_subplot(111)
    ax1.plot(acf(series1, nlags=numLags))
    np.random.shuffle(series1)
    ax1.plot(acf(series1, nlags=numLags))
    ax1.set_xlim(0.0)
    ax1.set_ylabel("ACF")
    ax1.set_xlabel("lags")
    auxFs.clean_axes(ax1)
    ax1.legend(["Process 1", "Process 2"])
    fig.savefig(f"ACF_twoProcesses.pdf")
Example #8
0
 def do_on_axes(ax):
     ax.set_xlabel("CSI")
     ax.set_ylabel("PDF")
     auxFs.clean_axes(ax)
     ax.set_xlim(0, 1.75)
     ax.set_ylim(top=ymax)