def db2(samples=1000): import numpy as np import testSystems as ts var = 0.01 noise = np.sqrt(var)*np.random.randn(samples) s = ts.testSystems(samples = samples+2, systemType = "4.2_AKB") u = np.array([s[-samples-1:-1],s[-samples-2:-2]]).T d = np.array(s[-samples:]).reshape(-1,1) + noise.reshape(-1,1) return u,d
def db3(samples=1000): import testSystems as ts return ts.testSystems(samples=samples, systemType="4.1_AKB")
def kafSearch_MC(filterName,systemName,n_samples,trainSplit,MC_runs): import KAF from tqdm import tqdm from sklearn.metrics import r2_score from sklearn.metrics import mean_squared_error import pandas as pd import numpy as np n_paramters = 0 # 1. Generate data for grid search n_train = int(n_samples*trainSplit) n_test = n_samples - n_train folder = 'GridSearchWang2/' attractors = ['lorenz','wang', 'rossler', 'rikitake'] if systemName in attractors: signalEmbedding = 5 import TimeSeriesGenerator inputSignal, y, z = TimeSeriesGenerator.chaoticSystem(samples=(n_samples+signalEmbedding)*MC_runs,systemType=systemName) inputSignal -= inputSignal.mean() inputSignal /= inputSignal.std() y -= y.mean() y /= y.std() z -= z.mean() z /= z.std() elif systemName == "4.1": signalEmbedding = 5 import testSystems as ts inputSignal, targetSignal = ts.testSystems(samples = (n_samples+signalEmbedding)*MC_runs, systemType = "4.1_AKB") # inputSignal -= inputSignal.mean() # inputSignal /= inputSignal.std() elif systemName == "4.2": signalEmbedding = 2 import testSystems as ts inputSignal = ts.testSystems(samples = (n_samples+signalEmbedding)*MC_runs, systemType = "4.2_AKB") inputSignal -= inputSignal.mean() inputSignal /= inputSignal.std() else: raise ValueError("Database does not exist") # 2. Pick filter if filterName == "QKLMS": # 2.1. Generate parameters for QKLMS grid search if systemName in attractors: eta = [0.05, 0.1, 0.3, 0.5, 0.9] sigma = [1e-2, 1e-1, 1, 2, 4, 8] epsilon = [1e-1, 0.5, 1, 2] elif systemName == "chua": eta = [0.05, 0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 0, 2, 4] epsilon = [1e-2, 1e-1, 1,2] elif systemName == "4.1": eta = [0.05, 0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 0, 2, 4] epsilon = [1e-2, 1e-1, 1,2] elif systemName == "4.2": eta = [0.05, 0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 0, 2, 4] epsilon = [1e-2, 1e-1, 1,2] params = [{'eta':et,'epsilon':ep, 'sigma':s } for et in eta for ep in epsilon for s in sigma] # 2.2. Search over QKLMS with Montecarlo Simulation results = [] testing_mse_xRun = [] CB_size_xRun = [] tradeOff_dist_xRun = [] signalPower = inputSignal.var() singleRunDataSize = n_samples trainLength = n_train for run in range(MC_runs): print("\n\nRunning Monte Carlo simulation #{}...\n".format(run)) try: u_train, u_test, d_train, d_test = trainAndTestSplitWithEmbedding(inputSignal, targetSignal,signalEmbedding, run, singleRunDataSize, trainLength) except: u_train, u_test, d_train, d_test = customEmbeddingForKAFs(inputSignal, signalEmbedding, run, singleRunDataSize, trainLength) # u_train, u_test, d_train, d_test = customExogenousEmbeddingForKAFs(inputSignal, y, z, signalEmbedding, run, singleRunDataSize, trainLength) for p in tqdm(params): try: f = KAF.QKLMS(eta=p['eta'],epsilon=p['epsilon'],sigma=p['sigma']) y = f.evaluate(u_train,d_train) y_pred = f.predict(u_test) err = d_test-y_pred.reshape(-1,1) t_mse = np.mean(err**2) testing_mse_xRun.append(t_mse/signalPower) CB_size_xRun.append(len(f.CB)) except: testing_mse_xRun.append(np.nan) CB_size_xRun.append(np.nan) allTestingMSEs = np.array(testing_mse_xRun).reshape(MC_runs,-1) allCB_sizes = np.array(CB_size_xRun).reshape(MC_runs,-1) testing_mse_average = np.nanmean(allTestingMSEs, axis=0) CB_size_average = np.nanmean(allCB_sizes, axis=0) tradeOff_distance = [tradeOffDistance(testing_mse_average[j],CB_size_average[j]/n_train) for j in range(len(params))] results = [p for p in params] results_df = pd.DataFrame(data=results) results_df['testing_mse'] = testing_mse_average results_df['CB_size'] = CB_size_average results_df['CB_size'] = results_df['CB_size'].astype(int) results_df['tradeOff_dist'] = tradeOff_distance results_df.to_csv(folder + filterName + '_' + systemName + '_' + str(n_samples) + '.csv') elif filterName == "QKLMS_AKB": # 2.1. Generate parameters for QKLMS_AKB grid search import numpy as np if systemName in attractors: eta = [0.05, 0.1, 0.3, 0.9] sigma = [ 1e-1, 1, 2] epsilon = [1e-1, 0.5, 1, 2] mu = [1e-3, 1e-1, 0.1, 1] K = [2,5,10,20] elif systemName == "chua": eta = [0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 2, 4] epsilon = [1e-2, 1e-1, 1] mu = [1e-3, 1e-1, 0.1, 1] K = [2,5,10,20] elif systemName == "4.1": eta = [0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 2, 4] epsilon = [1e-2, 1e-1, 1] mu = [1e-3, 1e-1, 0.1, 1] K = [2,5,10,20] elif systemName == "4.2": eta = [0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 2, 4] epsilon = [1e-2, 1e-1, 1] mu = [1e-3, 1e-1, 0.1, 1] K = [2,5,10,20] params = [{'eta':et,'epsilon':ep, 'sigma_init':s, 'mu':m, 'K':int(k) } for et in eta for ep in epsilon for s in sigma for m in mu for k in K] # 2.2. Search over QKLMS AKB with Montecarlo Simulation results = [] testing_mse_xRun = [] CB_size_xRun = [] tradeOff_dist_xRun = [] signalPower = inputSignal.var() singleRunDataSize = n_samples trainLength = n_train for run in range(MC_runs): print("\n\nRunning Monte Carlo simulation #{}...\n".format(run)) try: u_train, u_test, d_train, d_test = trainAndTestSplitWithEmbedding(inputSignal, targetSignal,signalEmbedding, run, singleRunDataSize, trainLength) except: # u_train, u_test, d_train, d_test = customEmbeddingForKAFs(inputSignal, signalEmbedding, run, singleRunDataSize, trainLength) u_train, u_test, d_train, d_test = customExogenousEmbeddingForKAFs(inputSignal, y, z, signalEmbedding, run, singleRunDataSize, trainLength) for p in tqdm(params): try: f = KAF.QKLMS_AKB(eta=p['eta'],epsilon=p['epsilon'],sigma_init=p['sigma_init'], mu=p['mu'], K=p['K']) y = f.evaluate(u_train,d_train) y_pred = f.predict(u_test) err = d_test-y_pred.reshape(-1,1) t_mse = np.mean(err**2) testing_mse_xRun.append(t_mse/signalPower) CB_size_xRun.append(len(f.CB)) except: testing_mse_xRun.append(np.nan) CB_size_xRun.append(np.nan) allTestingMSEs = np.array(testing_mse_xRun).reshape(MC_runs,-1) allCB_sizes = np.array(CB_size_xRun).reshape(MC_runs,-1) testing_mse_average = np.nanmean(allTestingMSEs, axis=0) CB_size_average = np.nanmean(allCB_sizes, axis=0) tradeOff_distance = [tradeOffDistance(testing_mse_average[j],CB_size_average[j]/n_train) for j in range(len(params))] results = [p for p in params] results_df = pd.DataFrame(data=results) results_df['testing_mse'] = testing_mse_average results_df['CB_size'] = CB_size_average results_df['CB_size'] = results_df['CB_size'].astype(int) results_df['tradeOff_dist'] = tradeOff_distance results_df.to_csv(folder + filterName + '_' + systemName + '_' + str(n_samples) + '.csv') elif filterName == "QKLMS_AMK": # 2.1. Generate parameters for QKLMS_AKB grid search import numpy as np if systemName in attractors: mu = [0.1, 0.5,1,1.5] eta = [0.05, 0.1, 0.3, 0.5, 0.9] epsilon = [1e-1, 0.5, 1, 2] K = [5,10,15,20] elif systemName == "chua": eta = np.linspace(0.02,1,n_paramters) epsilon = np.linspace(1e-3,100,n_paramters) mu = np.linspace(1e-4,1,n_paramters) K = np.linspace(2,20,n_paramters) elif systemName == "4.1": mu = [1e-3,1e-1, 0.1,1] eta = [0.05, 0.1,0.5,0.9] epsilon = [1e-2, 1e-1, 1, 1.5, 2] K = [2,5,10,15,20] elif systemName == "4.2": mu = [1e-3,1e-1, 0.1,1] eta = [0.05, 0.1,0.5,0.9] epsilon = [1e-2, 1e-1, 1, 1.5, 2] K = [2,5,10,15,20] params = [{'eta':et,'epsilon':ep, 'mu':m, 'K':int(k)} for et in eta for ep in epsilon for m in mu for k in K] # 2.2. Search over QKLMS_AMK with Montecarlo Simulation results = [] testing_mse_xRun = [] CB_size_xRun = [] tradeOff_dist_xRun = [] signalPower = inputSignal.var() singleRunDataSize = n_samples trainLength = n_train for run in range(MC_runs): print("\n\n Running Monte Carlo simulation #{}...\n".format(run)) try: u_train, u_test, d_train, d_test = trainAndTestSplitWithEmbedding(inputSignal, targetSignal,signalEmbedding, run, singleRunDataSize, trainLength) except: u_train, u_test, d_train, d_test = customEmbeddingForKAFs(inputSignal, signalEmbedding, run, singleRunDataSize, trainLength) # u_train, u_test, d_train, d_test = customExogenousEmbeddingForKAFs(inputSignal, y, z, signalEmbedding, run, singleRunDataSize, trainLength) for p in tqdm(params): try: f = KAF.QKLMS_AMK(eta=p['eta'],epsilon=p['epsilon'], mu=p['mu'], Ka=p['K'], A_init="pca") y = f.evaluate(u_train,d_train) y_pred = f.predict(u_test) err = d_test-y_pred.reshape(-1,1) t_mse = np.mean(err**2) testing_mse_xRun.append(t_mse/signalPower) CB_size_xRun.append(len(f.CB)) except: testing_mse_xRun.append(np.nan) CB_size_xRun.append(np.nan) allTestingMSEs = np.array(testing_mse_xRun).reshape(MC_runs,-1) allCB_sizes = np.array(CB_size_xRun).reshape(MC_runs,-1) testing_mse_average = np.nanmean(allTestingMSEs, axis=0) CB_size_average = np.nanmean(allCB_sizes, axis=0) tradeOff_distance = [tradeOffDistance(testing_mse_average[j],CB_size_average[j]/n_train) for j in range(len(params))] results = [p for p in params] results_df = pd.DataFrame(data=results) results_df['testing_mse'] = testing_mse_average results_df['CB_size'] = CB_size_average results_df['CB_size'] = results_df['CB_size'].astype(int) results_df['tradeOff_dist'] = tradeOff_distance results_df.to_csv(folder + filterName + '_' + systemName + '_' + str(n_samples) + '.csv') else: raise ValueError("Filter does not exist") return
def kafSearch(filterName,systemName,n_samples,trainSplit): import KAF from tqdm import tqdm from sklearn.metrics import r2_score from sklearn.metrics import mean_squared_error import pandas as pd import numpy as np n_paramters = 0 # 1. Generate data for grid search n_train = int(n_samples*trainSplit) n_test = n_samples - n_train if systemName == "lorenz" or systemName == "chua": embedding = 5 import TimeSeriesGenerator x, y, z = TimeSeriesGenerator.chaoticSystem(samples=200000,systemType='lorenz') x -= x.mean() x /= x.std() u_train = np.array([x[i-embedding:i] for i in range(embedding,n_train)]) d_train = np.array([x[i] for i in range(embedding,n_train)]).reshape(-1,1) u_test = np.array([x[i-embedding:i] for i in range(n_train,n_samples)]) d_test = np.array([x[i] for i in range(n_train,n_samples)]).reshape(-1,1) elif systemName == "4.2": embedding = 2 import testSystems as ts x = ts.testSystems(samples = n_samples+embedding, systemType = "4.2_AKB") x -= x.mean() x /= x.std() u_train = np.array([x[i-embedding:i] for i in range(embedding,n_train)]) d_train = np.array([x[i] for i in range(embedding,n_train)]).reshape(-1,1) u_test = np.array([x[i-embedding:i] for i in range(n_train,n_samples)]) d_test = np.array([x[i] for i in range(n_train,n_samples)]).reshape(-1,1) else: raise ValueError("Database does not exist") # 2. Pick filter if filterName == "QKLMS": # 2.1. Generate parameters for QKLMS grid search import numpy as np if systemName == "lorenz": eta = [0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 0, 2, 4] epsilon = [1e-2, 1e-1, 1] elif systemName == "chua": # eta = np.linspace(0.1,0.9,n_paramters) # sigma = np.linspace(100,500,n_paramters) # epsilon = np.linspace(0.01,20,n_paramters) a = 0 elif systemName == "4.2": eta = [0.1, 0.5, 0.9] sigma = [1e-2, 1e-1, 0, 2, 4] epsilon = [1e-2, 1e-1, 1] params = [{'eta':et,'epsilon':ep, 'sigma':s } for et in eta for ep in epsilon for s in sigma] # 2.2. Search over QKLMS results = [] for p in tqdm(params): try: f = KAF.QKLMS(eta=p['eta'],epsilon=p['epsilon'],sigma=p['sigma']) y = f.evaluate(u_train,d_train) y_pred = f.predict(u_test) err = d_test-y_pred.reshape(-1,1) t_mse = np.mean(err**2) signalPower = x.var() p['testing_mse'] = t_mse/signalPower p['CB_size'] = len(f.CB) p['tradeOff_dist'] = tradeOffDistance(p['testing_mse'],p['CB_size']/n_train) except: p['testing_mse'] = np.nan p['CB_size'] = np.nan p['tradeOff_dist'] = np.nan results.append(p) pd.DataFrame(data=results).to_csv('GridSearchResults2ndRun/' + filterName + '_' + systemName + '_' + str(n_samples) + '.csv') elif filterName == "QKLMS_AKB": # 2.1. Generate parameters for QKLMS_AKB grid search import numpy as np if systemName == "lorenz": eta = [0.1, 0.9] sigma = [1e-2, 1e-1, 2] epsilon = [1e-2, 1e-1, 1] mu = [1e-3, 1e-1, 0.1,1] K = [2,5,10,20] elif systemName == "chua": eta = np.linspace(0.1,0.9,n_paramters) sigma = np.linspace(0.01,200,n_paramters) epsilon = np.linspace(1e-3,100,n_paramters) mu = np.linspace(1e-4,1,n_paramters) K = np.linspace(2,20,n_paramters) elif systemName == "4.2": eta = np.linspace(0.1,0.9,n_paramters) sigma = np.linspace(0.01,200,n_paramters) epsilon = np.linspace(1e-3,100,n_paramters) mu = np.linspace(1e-4,1,n_paramters) K = np.linspace(2,20,n_paramters) params = [{'eta':et,'epsilon':ep, 'sigma_init':s, 'mu':m, 'K':int(k) } for et in eta for ep in epsilon for s in sigma for m in mu for k in K] # 2.2. Search over QKLMS results = [] for p in tqdm(params): try: f = KAF.QKLMS_AKB(eta=p['eta'],epsilon=p['epsilon'],sigma_init=p['sigma_init'], mu=p['mu'], K=p['K']) y = f.evaluate(u_train,d_train) y_pred = f.predict(u_test) err = d_test-y_pred.reshape(-1,1) t_mse = np.mean(err**2) signalPower = x.var() p['testing_mse'] = t_mse/signalPower p['CB_size'] = len(f.CB) p['tradeOff_dist'] = tradeOffDistance(p['testing_mse'],p['CB_size']/n_train) except: p['testing_mse'] = np.nan p['CB_size'] = np.nan p['tradeOff_dist'] = np.nan results.append(p) pd.DataFrame(data=results).to_csv('GridSearchResults2ndRun/' + filterName + '_' + systemName + '_' + str(n_samples) + '.csv') elif filterName == "QKLMS_AMK": # 2.1. Generate parameters for QKLMS_AKB grid search import numpy as np if systemName == "lorenz": mu = [1e-3,1e-1, 0.1,1,1.5,2] eta = [0.05, 0.1,0.5,0.9] epsilon = [1e-2, 1e-1, 1, 1.5, 2] K = [2,5,10,15,20] elif systemName == "chua": eta = np.linspace(0.02,1,n_paramters) epsilon = np.linspace(1e-3,100,n_paramters) mu = np.linspace(1e-4,1,n_paramters) K = np.linspace(2,20,n_paramters) elif systemName == "4.2": mu = [1e-3,1e-1, 0.1,1] eta = [0.05, 0.1,0.5,0.9] epsilon = [1e-2, 1e-1, 1, 1.5, 2] K = [2,5,10,15,20] params = [{'eta':et,'epsilon':ep, 'mu':m, 'K':int(k)} for et in eta for ep in epsilon for m in mu for k in K] # 2.2. Search over QKLMS_AMK results = [] for p in tqdm(params): try: f = KAF.QKLMS_AMK(eta=p['eta'],epsilon=p['epsilon'], mu=p['mu'], Ka=p['K'], A_init="pca") y = f.evaluate(u_train,d_train) y_pred = f.predict(u_test) err = d_test-y_pred.reshape(-1,1) t_mse = np.mean(err**2) signalPower = x.var() p['testing_mse'] = t_mse/signalPower p['CB_size'] = len(f.CB) p['tradeOff_dist'] = tradeOffDistance(p['testing_mse'],p['CB_size']/n_train) except: p['testing_mse'] = np.nan p['CB_size'] = np.nan p['tradeOff_dist'] = np.nan results.append(p) pd.DataFrame(data=results).to_csv('GridSearchResults2ndRun/' + filterName + '_' + systemName + '_' + str(n_samples) + '.csv') else: raise ValueError("Filter does not exist") return
sR_KLMS_BGMM = search.searchKLMS_BGMM(u, d, wcp, "wang", batchSize) QKLMS_RESULTS = pd.concat([QKLMS_RESULTS, sR_QKLMS]) KRLS_RESULTS = pd.concat([KRLS_RESULTS, sR_KRLS]) QKLMS_base_RESULTS = pd.concat([QKLMS_base_RESULTS, sR_QKLMS_base]) KLMS_BGMM_RESULTS = pd.concat([KLMS_BGMM_RESULTS, sR_KLMS_BGMM]) """ SISTEMA 1 : x = Proceso Gaussiano con media 0 y varianza 1+ t = Filtro FIR en x """ import testSystems u, d = testSystems.testSystems(samples=samples, systemType="1") u = u.reshape(-1, 1) d = d.reshape(-1, 1) epList = np.linspace(1e-1, 300, 300) sR_QKLMS = search.searchQKLMS(u, d, epList, "Sistema 2") sgmList = np.linspace(0.1, np.max(u), 20) epList = np.linspace(1e-6, 1e-1, 20) sR_KRLS = search.searchKRLS_ALD(u, d, sgmList, epList, "Sistema 1") sgmList = np.linspace(0.1, np.max(u), 20) epList = np.linspace(1e-1, 300, 300) sR_QKLMS_base = search.searchQKLMS_base(u, d, sgmList, epList, "Sistema 1") wcp = np.linspace(1e-6, 1e-2, 50)
params = selectBestResultFromKafSearch(folder + '/' + AMK_42) import KAF import TimeSeriesGenerator n_samples = 5000 n_train = int(n_samples * 0.8) '''lorenz''' # embedding = 5 # x, y, z = TimeSeriesGenerator.chaoticSystem(samples=n_samples+embedding,systemType='lorenz') # x -= x.mean() # x /= x.std() '''4.2''' embedding = 2 import testSystems as ts x = ts.testSystems(samples=n_samples + embedding, systemType="4.2_AKB") x -= x.mean() x /= x.std() u_train = np.array([x[i - embedding:i] for i in range(embedding, n_train)]) d_train = np.array([x[i] for i in range(embedding, n_train)]).reshape(-1, 1) u_test = np.array([x[i - embedding:i] for i in range(n_train, n_samples)]) d_test = np.array([x[i] for i in range(n_train, n_samples)]).reshape(-1, 1) # f = KAF.QKLMS(eta=params['eta'], epsilon=params['epsilon'], sigma=params['sigma']) # f = KAF.QKLMS_AKB(eta=params['eta'], epsilon=params['epsilon'], sigma_init= params['sigma_init'], mu=params['mu'], K=int(params['K'])) f = KAF.QKLMS_AMK(eta=params['eta'], epsilon=params['epsilon'], mu=params['mu'], Ka=int(params['K']),
# -*- coding: utf-8 -*- """ Created on Wed Apr 21 00:28:14 2021 @author: USUARIO """ import numpy as np import testSystems as ts N = 5000 u,d = ts.testSystems(N,'4.1_AKB') import TimeSeriesGenerator x, y, z = TimeSeriesGenerator.chaoticSystem(samples=6000,systemType='wang') def std(x): x = x - x.mean() return x/x.std() # signalEmbedding = 5 # X = np.array([u[i-signalEmbedding:i] for i in range(signalEmbedding,len(u))]) # y = np.array([d[i] for i in range(signalEmbedding,len(u))]).reshape(-1,1) u = d = std(x) import KAF f = KAF.QKLMS_AMK(eta=0.01,epsilon=0.5,embedding=10, Ka=10, mu=0.05) f.evaluate(u[:100],d[:100]) f.fit(u[:4000],d[:4000])
# from OA import artifactRemoval # ar = artifactRemoval(th=2) # ar.fit(Xdata,labels) # cleanEEG = ar.transform(Xdata) from KAF import QKLMS_v2 f = QKLMS_v2(epsilon=0.0, sigma=0.1) import testSystems as ts import numpy as np N = 200 L = 5 u, d = ts.testSystems(N, "4.1_AKB") X = np.array([u[i - L:i] for i in range(L, len(u))]) y = np.array([d[i] for i in range(L, len(d))]) f.fit(X, y) y_pred = f.predict(X) plt.plot(y) plt.plot(y_pred) plt.show() # from KAF import QKLMS # f = QKLMS(epsilon=0.0) # import testSystems as ts