def _generate_matrix(self): """Generate A matrix if required""" k = self.k n = self.n m = self.m dim = self.dim self._calculate_orders() self.A = sparse.dok_matrix((self.order_idx[k],dim)) self.row_counter = 0 for ordi in xrange(k): self.nterms = m**(n - (ordi+1)) self.terms = dec2base(np.c_[0:self.nterms,], m, n-(ordi+1)) self._recloop((ordi+1), 1, [], [], n, m) print "Order " + str(ordi+1) + " complete. Time: " + time.ctime() # save matrix to file self.A = self.A.tocsc() savedict = {'A':self.A, 'order_idx':self.order_idx} sio.savemat(self.filename, savedict)
def order1direct(p,a): """Compute first order solution directly for testing""" if p.size != a.fdim: raise ValueError, "Probability vector doesn't match a.fdim" # 1st order marginals marg = a.eta_from_p(p)[:a.order_idx[1]] # output p1 = np.zeros(a.fdim) the1pos = lambda x,v: ((v-1)*a.n)+x # loop over all probabilities (not p(0)) for i in range(1,a.fdim): Pword = dec2base(np.atleast_2d(i).T,a.m,a.n) # loop over each variable for j in range(a.n): # this value x = Pword[0][j] if x!=0: # this is a normal non-zero marginal factor = marg[the1pos(j,x)] else: # this is a zero-value marginal factor = 1 - marg[the1pos(j,np.r_[1:a.m])].sum() if p1[i]==0: # first entry p1[i] = factor else: p1[i] *= factor # normalise p1[0] = 1.0 - p1.sum() return p1
def _sample(self, method='naive'): """Sample probabilities of system. Parameters ---------- method : {'naive', 'beta:x', 'kt'}, optional Sampling method to use. 'naive' is the standard histrogram method. 'beta:x' is for an add-constant beta estimator, with beta value following the colon eg 'beta:0.01' [1]_. 'kt' is for the Krichevsky-Trofimov estimator [2]_, which is equivalent to 'beta:0.5'. References ---------- .. [1] T. Schurmann and P. Grassberger, "Entropy estimation of symbol sequences," Chaos,vol. 6, no. 3, pp. 414--427, 1996. .. [2] R. Krichevsky and V. Trofimov, "The performance of universal encoding," IEEE Trans. Information Theory, vol. 27, no. 2, pp. 199--207, Mar. 1981. """ calc = self.calc # decimalise if any([c in calc for c in ['HXY', 'HX']]): if self.X_n > 1: d_X = decimalise(self.X, self.X_n, self.X_m) else: # make 1D d_X = self.X.reshape(self.X.size) # unconditional probabilities if ('HX' in calc) or ('ChiX' in calc): self.PX = prob(d_X, self.X_dim, method=method) if any([c in calc for c in ['HXY', 'HiX', 'HiXY', 'HY']]): self.PY = _probcount(self.Ny, self.N, method) if 'SiHXi' in calc: for i in xrange(self.X_n): self.PXi[:, i] = prob(self.X[i, :], self.X_m, method=method) # conditional probabilities if any([c in calc for c in ['HiXY', 'HXY', 'HshXY']]): sstart = 0 for i in xrange(self.Y_dim): send = sstart + self.Ny[i] indx = slice(sstart, send) sstart = send if 'HXY' in calc: # output conditional ensemble oce = d_X[indx] if oce.size == 0: print 'Warning: Null output conditional ensemble for ' + \ 'output : ' + str(i) else: self.PXY[:, i] = prob(oce, self.X_dim, method=method) if any([c in calc for c in ['HiX', 'HiXY', 'HshXY']]): for j in xrange(self.X_n): # output conditional ensemble for a single variable oce = self.X[j, indx] if oce.size == 0: print 'Warning: Null independent output conditional ensemble for ' + \ 'output : ' + str(i) + ', variable : ' + str(j) else: self.PXiY[:, j, i] = prob(oce, self.X_m, method=method) if 'HshXY' in calc: # shuffle #np.random.shuffle(oce) shfoce = np.random.permutation(oce) self.Xsh[j, indx] = shfoce # Pind(X) = <Pind(X|Y)>_y if ('HiX' in calc) or ('ChiX' in calc): # construct joint distribution words = dec2base( np.atleast_2d(np.r_[0:self.X_dim]).T, self.X_m, self.X_n) PiXY = np.zeros((self.X_dim, self.Y_dim)) PiXY = self.PXiY[words, np.r_[0:self.X_n]].prod(axis=1) # average over Y self.PiX = np.dot(PiXY, self.PY) self.sampled = True
def _sample(self, method='naive'): """Sample probabilities of system. Parameters ---------- method : {'naive', 'beta:x', 'kt'}, optional Sampling method to use. 'naive' is the standard histrogram method. 'beta:x' is for an add-constant beta estimator, with beta value following the colon eg 'beta:0.01' [1]_. 'kt' is for the Krichevsky-Trofimov estimator [2]_, which is equivalent to 'beta:0.5'. References ---------- .. [1] T. Schurmann and P. Grassberger, "Entropy estimation of symbol sequences," Chaos,vol. 6, no. 3, pp. 414--427, 1996. .. [2] R. Krichevsky and V. Trofimov, "The performance of universal encoding," IEEE Trans. Information Theory, vol. 27, no. 2, pp. 199--207, Mar. 1981. """ calc = self.calc # decimalise if any([c in calc for c in ['HXY','HX']]): if self.X_n > 1: d_X = decimalise(self.X, self.X_n, self.X_m) else: # make 1D d_X = self.X.reshape(self.X.size) # unconditional probabilities if ('HX' in calc) or ('ChiX' in calc): self.PX = prob(d_X, self.X_dim, method=method) if any([c in calc for c in ['HXY','HiX','HiXY','HY']]): self.PY = _probcount(self.Ny,self.N,method) if 'SiHXi' in calc: for i in xrange(self.X_n): self.PXi[:,i] = prob(self.X[i,:], self.X_m, method=method) # conditional probabilities if any([c in calc for c in ['HiXY','HXY','HshXY']]): sstart=0 for i in xrange(self.Y_dim): send = sstart+self.Ny[i] indx = slice(sstart,send) sstart = send if 'HXY' in calc: # output conditional ensemble oce = d_X[indx] if oce.size == 0: print 'Warning: Null output conditional ensemble for ' + \ 'output : ' + str(i) else: self.PXY[:,i] = prob(oce, self.X_dim, method=method) if any([c in calc for c in ['HiX','HiXY','HshXY']]): for j in xrange(self.X_n): # output conditional ensemble for a single variable oce = self.X[j,indx] if oce.size == 0: print 'Warning: Null independent output conditional ensemble for ' + \ 'output : ' + str(i) + ', variable : ' + str(j) else: self.PXiY[:,j,i] = prob(oce, self.X_m, method=method) if 'HshXY' in calc: # shuffle #np.random.shuffle(oce) shfoce = np.random.permutation(oce) self.Xsh[j,indx] = shfoce # Pind(X) = <Pind(X|Y)>_y if ('HiX' in calc) or ('ChiX' in calc): # construct joint distribution words = dec2base(np.atleast_2d(np.r_[0:self.X_dim]).T,self.X_m,self.X_n) PiXY = np.zeros((self.X_dim, self.Y_dim)) PiXY = self.PXiY[words,np.r_[0:self.X_n]].prod(axis=1) # average over Y self.PiX = np.dot(PiXY,self.PY) self.sampled = True