def clustering2(G, alpha=0.9, K=2, n_vec=1): # basé sur l'article de fragkiskos mallarios A = nx.adjacency_matrix(G) A = A.toarray() T = np.array(np.sum(A, axis=1).reshape(-1)) T[T == 0] = 1 D_1 = np.diag(1 / T) P = D_1 @ A P2 = alpha * P + (1 - alpha) * (1 / A.shape[0]) * np.ones(A.shape) mc = markovChain(P2) mc.computePi('eigen') Pi12 = np.sqrt(mc.pi) Pi_12 = 1 / Pi12 Pi12 = np.diag(Pi12) Pi_12 = np.diag(Pi_12) L = np.eye(*A.shape) - (1 / 2) * (Pi12 @ P @ Pi_12 + Pi_12 @ P.T @ Pi12) eigenValues, eigenVectors = np.linalg.eig(L) idx = eigenValues.argsort() eigenVectors = eigenVectors[:, idx] X = eigenVectors[:, 1:n_vec + 1] kmeans = KMeans(n_clusters=K).fit(X) return kmeans.labels_
def calcSD(tranM): # Input: tranM probability transition matrix (transitions from row to column) # Output: SD stationary distribution from discreteMarkovChain import markovChain mc = markovChain(tranM) mc.computePi('eigen') # We can use 'linear', 'power', 'krylov' or 'eigen' SD = (mc.pi).reshape(-1, 1) return SD
def calculateStationaryVector(matrix): """ -Calculates the stationnary vector of the matrix by solving the equation PI = PI * M for PI - Returns the stationnary vector PI """ markovCh = markovChain(matrix) markovCh.computePi('linear') return markovCh.pi
def calcProbabilisticExecutabilityOfPolicy(pi, s_p, gamma_p, Pr): s, p = s_p S_O = exitStatesPolicy(pi, [s], gamma_p, Pr) P, states = stateActionTransitionFunctionToMatrix(pi, gamma_p, Pr) mc = markovChain(P) mc.computePi('power') return list( filter(lambda x: x[0] in S_O, zip(states, (lambda y: p * y, mc.pi.values()))))
def main(): #Aperiodic, irreducible and double stochastic Markov chain (e.g., random walk on a cyclic graph) P = np.matrix([[0, 0.2, 0.2, 0.2, 0.2, 0.2], [0.2, 0, 0.2, 0.2, 0.2, 0.2], [0.2, 0.2, 0, 0.2, 0.2, 0.2], [0.2, 0.2, 0.2, 0, 0.2, 0.2], [0.2, 0.2, 0.2, 0.2, 0, 0.2], [0.2, 0.2, 0.2, 0.2, 0.2, 0]]) mc = markovChain(P) #Calculate the limiting distribution or the steady state distribution of a Markov chain mc.computePi('linear') #We can also use 'power', 'krylov' or 'eigen' print mc.pi print calculateSteadyStateDistribution(P)
def calc_seq_prob(input_seq): # transition matrix tm = np.zeros((2, 2)) for (x, y), c in Counter(zip(input_seq, input_seq[1:])).iteritems(): tm[x - 1, y - 1] = c P = tm / tm.sum( axis=1)[:, None] # or ....... P = b/b.sum(axis=1, keepdims=True mc = markovChain(P) mc.computePi('linear') # 'linear', 'power', 'krylov', 'eigen' return mc.pi
def sem_similarty_scoring(self, clusters): """ Run biased PageRank using Universal Sentence Encoder to receive embedding. Calls add add_tp_ratio() and add_syn_similarity(). Computes similarity to conclusion. :param clusters: :return: """ messages = [] for idx, cluster in enumerate(clusters): messages = [] for argument in cluster: messages = messages + argument.sentences message_embedding = [ message.numpy() for message in self.embed(messages) ] #self.tf_session.run(self.embed_result, feed_dict={self.text_input: messages}) sim = np.inner(message_embedding, message_embedding) #sim_message = self.normalize_by_rowsum(sim) matrix = self.add_tp_ratio(cluster) M = np.array(sim) * (1 - self.d) + np.array(matrix) * self.d #p = self.power_method(M, 0.0000001) mc = markovChain(M) mc.computePi('power') p = mc.pi x = 0 for i in range(len(cluster)): if not cluster[i].score: score_exists = False else: score_exists = True for j in range(len(cluster[i].sentences)): if score_exists: cluster[i].score[j] += p[x] cluster[i].score[j] = cluster[i].score[j] else: cluster[i].score.append(p[x]) x += 1 if (len(cluster[i].score) > 1): cluster[i].score = list( (cluster[i].score - min(cluster[i].score)) / (max(cluster[i].score) - min(cluster[i].score))) else: cluster[i].score = [1]
def compute_steady_state(DG): #Compute the steady state distribution P = nx.adjacency_matrix(DG) mc = markovChain(P) mc.computePi('linear') #We can also use 'power', 'krylov' or 'eigen' steady_state = mc.pi #print(P.todense()) map_steady_dist = {} i = 0 for profile in DG.nodes(): map_steady_dist[profile] = steady_state[i] i = i + 1 return map_steady_dist
def calcPolicyRobustness(Pi, S, gamma_p_dict, gamma_p, delta): states = gamma_p_dict['states'] output = calcPolicyOutput(Pi, list(map(lambda e: e[0], S)), gamma_p, delta) output_converted = list( map(lambda e: state_convert(e, gamma_p_dict), output)) P = gammaDictToMatrix(gamma_p_dict, Pi, delta) mc = markovChain(P) mc.computePi('power') r = [] for i in range(len(mc.pi)): if state_convert(states[i], gamma_p_dict) in output_converted: state_r = output[output_converted.index( state_convert(states[i], gamma_p_dict))] p = mc.pi[i] r.append((state_r, p)) return r
def mc(alpha): min_positive_state = max(1, int(poisson.ppf(1 - alpha, mu))) def state_to_index(state): # states are in the range [-n_state, n_state] but array indices begin at 0 return state + n_state + 1 def index_to_state(index): return -n_state + index - 1 trans = dict() for i in range(-n_state, n_state + 1): trans[i] = dict() for j in range(-n_state, n_state + 1): trans[i][j] = .0 # Pr[i->j] where i is non-positive for i in range(-n_state, min_positive_state): for j in range(-n_state, n_state + 1): if j >= i - 1: trans[i][j] = poisson.pmf(j - i + 1, mu) trans[-n_state][-n_state] += 1 - sum(trans[-n_state].values()) for i in range(min_positive_state, n_state + 1): for j in range(-1, n_state): trans[i][j] = poisson.pmf(j + 1, mu) trans_matrix = [] for i in range(-n_state, n_state + 1): od = OrderedDict(sorted(trans[i].items())) trans_matrix.append(od.values()) trans_matrix = np.matrix(trans_matrix) markov = markovChain(trans_matrix) markov.computePi('linear') # We can also use 'power', 'krylov' or 'eigen' stationary = markov.pi p_rej = 0. for i in range(state_to_index(min_positive_state), len(stationary)): p_rej += index_to_state(i) * stationary[i] return p_rej, 1 / (1 - p_rej)
P[I2a(J), I2a(N)] = probJtoN(J) # probability of transitioning to a lower level without restocking # for K in xrange(resupply + 1, J + 1): P[I2a(J), I2a(K)] = probJtoK(J, K) # overwrites same entry computed in above loop, the Nth-Nth element is special # P[I2a(N), I2a(N)] = probNtoN() # check the transition matrix for double stochasticity # First the rows, each has to sum to 1 # for i in xrange(resupply + 1, N + 1): sum = 0.0 for j in xrange(resupply + 1, N + 1): sum += P[I2a(i), I2a(j)] if 1e-9 < fabs(1.0 - sum): print('suspect transition matrix row') print(P) exit(0) # use the discrete state markov solver to get the steady state probabilities # mc = markovChain(P) mc.computePi('linear') # mc.pi is the solution vector, print it out print(mc.pi)
from discreteMarkovChain import markovChain import numpy as np d_matrix = np.genfromtxt('/Users/tomereldor/Downloads/Cgraph.csv', delimiter=',') d_matrix[0,0] = 0 P = np.array([[0.5,0.5],[0.6,0.4]]) mc = markovChain(d_matrix) mc.computePi('linear') #We can also use 'power', 'krylov' or 'eigen' print(mc.pi) from discreteMarkovChain import partition class randomWalkNumpy(markovChain): #Now we do the same thing with a transition function that returns a 2d numpy array. #We also specify the statespace function so we can use the direct method. #This one is defined immediately for general n. def __init__(self,m,M,n,direct=True): super(randomWalkNumpy, self).__init__(direct=direct) self.initialState = m*np.ones(n,dtype=int) self.n = n self.m = m self.M = M self.uprate = 1.0 self.downrate = 1.0 #It is useful to define the variable 'events' for the the transition function. #The possible events are 'move up' or 'move down' in one of the random walks.
def get_stationary(transmat): mc = markovChain(transmat) mc.computePi('eigen') # We can also use 'power', 'krylov' or 'eigen' return mc.pi
import numpy as np from discreteMarkovChain import markovChain import discreteMarkovChain P = np.array([[0.99, 0.01, 0.00, 0.00, 0.00, 0.00], [0.00, 0.80, 4 / 100, 1 / 100, 0.00, 0.15], [0.00, 0.15, 0.80, 0.05, 0.00, 0.00], [0.00, 0.00, 0.05, 0.80, 0.15, 0.00], [0.00, 0.00, 0.00, 0.00, 1.00, 0.00], [0, 0, 0, 0, 0, 1]]) mc = markovChain(P, ['S', 'I', 'H', 'UCI', 'O', 'R']) mc.computePi('krylov') #We can also use 'power', 'krylov' or 'eigen' print(np.round(mc.pi, 3))
Q[i][j] = state[i] * probability probability = 1 - probability ''' Q = [[], [], []] Q[0] = [-0.0196078431372549, 0.0026227642309271603, 0.016985078906327743] Q[1] = [0.014030240457579134, -0.017241379310344827, 0.0032111388527656937] Q[2] = [0.0012304579488692923, 0.006232228618294887, -0.007462686567164179] print("##MATRIX##") print(Q[0]) print(Q[1]) print(Q[2]) #Calculate stationary values mc = markovChain(np.array(Q)) mc.computePi('linear') print("##STATIONARY") print(mc.pi) #Begin simulation simulate_cmc(Q, 10) #Histogram for interarrival time plt.figure(figsize=(12, 8)) radio_time = plt.hist(data[0][0], bins=100) video_time = plt.hist(data[1][0], bins=100) social_time = plt.hist(data[2][0], bins=100) #Histogram for packet length plt.figure(figsize=(12, 8))
def __init__(self, num_states, rand=True, arm_count=0): self.num_states = num_states self.cumulative_reward = 0.0 self.pull_count = 0 self.rand = rand self.arm_count = arm_count if self.rand: # create ergodic transition matrix self.tran_matrix = np.zeros((num_states, num_states)) ergodic = False while ergodic == False: P = np.random.rand(num_states, num_states) P = preprocessing.normalize(P, norm='l1', axis=1, copy=True, return_norm=False) dic = {} for i in range(num_states): for j in range(num_states): dic[(i, j)] = P[i, j] T = pykov.Chain(dic) G = nx.DiGraph(list(T.keys())) if nx.is_strongly_connected(G) and nx.is_aperiodic(G): ergodic = True self.tran_matrix = P else: if self.arm_count == 0: self.tran_matrix = np.array([[0., 1.], [.01, .99]]) if self.arm_count == 1: self.tran_matrix = np.array([[.99, .01], [1., 0.]]) # find the steady state distribution mc = markovChain(self.tran_matrix) mc.computePi('linear') # We can also use 'power', 'krylov' or 'eigen' self.steady_state = np.asmatrix(mc.pi) # find ergodic constants P_tilde = np.zeros( (self.num_states, self.num_states)) # P_tilde as per Formula 2.1 in Page 65 for x in range(self.num_states): for y in range(self.num_states): P_tilde[x, y] = self.steady_state[0, y] * self.tran_matrix[ y, x] / self.steady_state[0, x] MP = np.dot(self.tran_matrix, P_tilde) eigenval, _ = np.linalg.eig(MP) self.eta = np.sort(eigenval)[-2] # Second largest eigenvalue self.eta = np.sqrt(self.eta) # sqrt for TV norm # Now we need to compute C using chi_0 square in Thm 2.7 # Here we don't have information about current state, # given a steady state distribution, chi_0 is maximized when current state is of the form (1,0,0,0,0,0) curr_state = np.zeros((1, self.num_states)) index = np.argmin(self.steady_state) curr_state[0, index] = 1.0 self.C = 0.25 * sum([(self.steady_state[0, i] - curr_state[0, i])**2 / self.steady_state[0, i] for i in range(self.num_states)]) self.C = np.sqrt(self.C) # sqrt for TV norm if self.rand: self.reward_type = np.random.randint(0, 3) self.set_reward_parameters() else: if self.arm_count == 0: self.rewards = np.array([0, 1]) elif self.arm_count == 1: self.rewards = np.array([.5, .5])
def point(p1_p, p2_p): P = M1(p1_p, p2_p) mc = markovChain(P) mc.computePi('linear') #We can also use 'power', 'krylov' or 'eigen' return mc.pi
def _calculate(matrix): mc = markovChain(matrix) mc.computePi('linear') out = mc.pi return out
# simulation n = 100 state_1 = simulate(1, 0, n) state_2 = simulate(2, n, 2 * n) state_3 = simulate(3, 2 * n, 3 * n) state_4 = simulate(4, 3 * n, 4 * n) # plot n = 100 plt.step(range(n), state_4) plt.xticks(np.arange(0, n, step=10)) plt.ylim(0, 5) plt.show() # steady state distribution mc = markovChain(g) mc.powerMetho.d(tol=1e-10, maxiter=1e16) steady_state = np.round(mc.pi, decimals=4) print(steady_state) # experimentally computed stationary distribution n_max = 500000 n = round(n_max / 4) states = [] states.extend(simulate(1, 0, n)) states.extend(simulate(2, n, 2 * n)) states.extend(simulate(3, 2 * n, 3 * n)) states.extend(simulate(4, 3 * n, 4 * n))