def gener_init_pq(self): # 生成初始搜索点(p0,q0) rgs = BassEstimate(self.s) P0, Q0 = rgs.optima_search()[1:3] # SABM最优点(P0,Q0) p_range = np.linspace(0.4 * P0, P0, num=3) q_range = np.linspace(0.2 * Q0 / self.k, 0.6 * Q0 / self.k, num=3) to_fit = {} params_cont = [] for p in p_range: # 取9个点用于确定参数与估计值之间的联系 for q in q_range: diffu = Diffuse(p, q, g=self.G, num_runs=self.s_len, multi_proc=self.m_p) s_estim = diffu.repete_diffuse() s_estim_avr = np.mean(s_estim, axis=0) rgs_1 = BassEstimate(s_estim_avr) P, Q = rgs_1.optima_search()[1:3] params_cont.append([p, q, P, Q]) to_fit = pd.DataFrame(params_cont, columns=['p', 'q', 'P', 'Q']) result_p = smf.ols('p~P+Q-1', data=to_fit).fit() result_q = smf.ols('q~P+Q-1', data=to_fit).fit() p0 = result_p.params['P'] * P0 + result_p.params['Q'] * Q0 q0 = result_q.params['P'] * P0 + result_q.params['Q'] * Q0 return round(p0, 5), round(q0, 5) # 保留5位小数位,防止出错
def add_data(self, p, q): diff = Diffuse(p, q, g=self.g, alpha=self.alpha, sigma=self.sigma) x = np.mean(diff.repete_diffuse(), axis=0) max_idx = np.argmax(x) s = x[:(max_idx + 2)] para_range = [[1e-6, 0.1], [1e-5, 0.8], [sum(s), 4*self.num_nodes]] bassest = BassEstimate(s, para_range) bassest.t_n = 1000 res = bassest.optima_search(c_n=200, threshold=10e-6) return res[1:3] # P, Q
def r2_mse_abm(params, S=S): p, q = params T = len(S) diffuse = Diffuse(p, q, num_runs=T, multi_proc=True) diffuse_cont = diffuse.repete_diffuse(repetes=10) X = np.mean(diffuse_cont, axis=0) a = np.sum(np.square(X)) / np.sum(S) # 除以np.sum(S)是为减少a的大小 b = -2*np.sum(X*S) / np.sum(S) c = np.sum(np.square(S)) / np.sum(S) sigma = -b/(2*a) # 倍数 mse = np.sqrt(np.sum(S) * (4*a*c - b**2) / (4*a*T)) hat_S = list(X*sigma) tse = np.sum(np.square(S - hat_S)) mean_y = np.mean(S) ssl = np.sum(np.square(S - mean_y)) R_2 = (ssl - tse) / ssl return R_2, mse, sigma
def fitness(individual, S=S): p, q = individual T = len(S) if p <= 0: raise Exception(f"p={p}小于0!") elif q <= 0: raise Exception(f"q={q}小于0!") else: diffuse = Diffuse(p, q, num_runs=T, multi_proc=True) diffuse_cont = diffuse.repete_diffuse(repetes=10) X = np.mean(diffuse_cont, axis=0) a = np.sum(np.square(X)) / np.sum(S) # 除以np.sum(S)是为减少a的大小 b = -2*np.sum(X*S) / np.sum(S) c = np.sum(np.square(S)) / np.sum(S) mse = np.sqrt(np.sum(S) * (4*a*c - b**2) / (4*a*T)) # sigma = -b/(2*a) # 倍数 return mse
def get_M(self, p, q): # 获取对应扩散率曲线的最优潜在市场容量 if p <= 0: raise Exception(f"p={p}小于0!") elif q <= 0: raise Exception(f"q={q}小于0!") else: diffu = Diffuse(p, q, g=self.G, num_runs=self.s_len, multi_proc=self.m_p) s_estim = diffu.repete_diffuse() x = np.mean(s_estim, axis=0) a = np.sum(np.square(x)) / np.sum( self.s) # 除以np.sum(self.s)是为减少a的大小 b = -2 * np.sum(x * self.s) / np.sum(self.s) c = np.sum(np.square(self.s)) / np.sum(self.s) mse = np.sqrt( sum(self.s) * (4 * a * c - b**2) / (4 * a * self.s_len)) sigma = -b / (2 * a) m = sigma * self.G.number_of_nodes() return [mse, p, q, m], list(x * sigma)
def gener_init_params(S, g=nx.gnm_random_graph(10000, 30000)): # 生成初始搜索点(p0,q0) rgs = BassEstimate(S) P0, Q0 = rgs.optima_search()[1 : 3] # BASS最优点(P0,Q0) p_range = np.linspace(0.3*P0, P0, num=3) q_range = np.linspace(0.06*Q0, 0.3*Q0, num=3) to_fit = {} params_cont = [] for p in p_range: # 取9个点用于确定参数与估计值之间的联系 for q in q_range: diffu = Diffuse(p, q, g=g, num_runs=len(S), multi_proc=True) s_estim = diffu.repete_diffuse() s_estim_avr = np.mean(s_estim, axis=0) rgs_1 = BassEstimate(s_estim_avr) P, Q = rgs_1.optima_search()[1: 3] params_cont.append([p, q, P, Q]) to_fit = pd.DataFrame(params_cont, columns=['p', 'q', 'P', 'Q']) result_p = smf.ols('p~P+Q-1', data=to_fit).fit() result_q = smf.ols('q~P+Q-1', data=to_fit).fit() p0 = result_p.params['P']*P0 + result_p.params['Q']*Q0 q0 = result_q.params['P']*P0 + result_q.params['Q']*Q0 return round(p0, 5), round(q0, 5) # 保留5位小数位,防止出错
def func_1(p, q, sigma): p = round(p, 5) q = round(q, 5) diff = Diffuse(p=p, q=q, sigma=sigma, num_runs=35, multi_proc=True) x = np.mean(diff.repete_diffuse(), axis=0) return str([p, q]), list(np.concatenate(([p, q], x)))
def func(p, q, sigma): diff = Diffuse(p, q, sigma=sigma, num_runs=40) x = np.mean(diff.repete_diffuse(), axis=0) return np.concatenate(([p, q], x))
prj = db.indivHeter #%% def func(p, q, sigma): diff = Diffuse(p, q, sigma=sigma, num_runs=40) x = np.mean(diff.repete_diffuse(), axis=0) return np.concatenate(([p, q], x)) #%%[markdown] #### 扩散示例 #%% sigma = 0.1 p, q = 0.001, 0.05 t1 = time.perf_counter() diff = Diffuse(p, q, sigma=sigma) diff_cont = diff.repete_diffuse() S = np.mean(diff_cont, axis=0) print(f"Time elasped:{time.perf_counter()-t1:.2f}s") print(f"网络:{sigma}") print(f"最大采纳量{np.max(S):.0f}, 最大时间步:{np.argmax(S)}") #%% m_idx = np.argmax(S) s = S[:m_idx + 2] t1 = time.process_time() para_range = [[1e-5, 0.1], [1e-5, 0.8], [sum(s), 10*sum(s)]] bassest = BassEstimate(s, para_range) mse, P, Q, M = bassest.optima_search(c_n=100, threshold=10e-6) r_2 = bassest.r2([P, Q, M]) print(f'Time elapsed: {(time.process_time() - t1):.2f}s')
# coding=utf-8 import numpy as np import networkx as nx import time import random from abmdiffuse import Diffuse if __name__ == '__main__': t1 = time.perf_counter() p, q, alpha = 0.01, 0.5, 1 diffu = Diffuse(p, q, alpha=alpha) diffu_cont = diffu.repete_diffuse(repetes=10) print( f"参数设置: p--{p}, q--{q}, alpha--{alpha} network--{diffu.g.number_of_nodes()}" ) print(f"用时{time.perf_counter() - t1:.2f}秒")
def func(p, q, g): p = round(p, 5) q = round(q, 5) diff = Diffuse(p, q, g=g, num_runs=35) x = np.mean(diff.repete_diffuse(), axis=0) return str([p, q]), list(np.concatenate(([p, q], x)))
for line in diff_list: pl.plot(line[2:]) #%% for line in diff_list[:15]: pl.plot(line[2:]) #%% param_bound = ws0["param_boundary"] print(param_bound) #%% t1 = time.perf_counter() g = nx.watts_strogatz_graph(10000, 6, 0) p, q = 0.001, 0.1 diff = Diffuse(p, q, g=g) diff_cont = diff.repete_diffuse(20) S = np.mean(diff_cont, axis=0) print(f"Time elasped:{time.perf_counter()-t1:.2f}s") print(f"网络: nx.watts_strogatz_graph(10000, 6, 0)") print(f"最大采纳量{np.max(S):.0f}, 最大时间步:{np.argmax(S)}") m_idx = np.argmax(S) s = S[:m_idx + 2] t1 = time.process_time() para_range = [[1e-5, 0.1], [1e-5, 0.8], [sum(s), 10 * sum(s)]] bassest = BassEstimate(s, para_range) mse, P, Q, M = bassest.optima_search(c_n=100, threshold=10e-6) r_2 = bassest.r2([P, Q, M]) print(f'Time elapsed: {(time.process_time() - t1):.2f}s') print(f'P:{P:.4f} Q:{Q:.4f} M:{M:.0f}\nr^2:{r_2:.4f}')
# coding=utf-8 import numpy as np import networkx as nx import time import random from abmdiffuse import Diffuse if __name__ == '__main__': import pylab as pl t1 = time.perf_counter() p, q = 0.001, 0.05 diffu = Diffuse(p=p, q=q, sigma=0.1) diffu_cont = diffu.repete_diffuse(repetes=20) print(f"参数设置: p--{p}, q--{q} sigma--{diffu.sigma}") print(f"用时{time.perf_counter() - t1:.2f}秒") fig = pl.figure(figsize=(12, 6)) ax = fig.add_subplot(1, 1, 1) for line in diffu_cont: ax.plot(line, 'k-', lw=0.5, alpha=0.5) ax.plot(np.mean(diffu_cont, axis=0), 'r-', lw=2) pl.show()