def test_hessian_2(): n = 3 seqs = [[ 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 3, 3 ]] model = ContinuousTimeMSM().fit(seqs) print(model.timescales_) print(model.uncertainty_timescales()) theta = model.theta_ C = model.countsmat_ print(C) C_flat = (C + C.T)[np.triu_indices_from(C, k=1)] print(C_flat) print('theta', theta, '\n') inds = np.where(theta != 0)[0] hessian1 = _ratematrix.hessian(theta, C, inds=inds) hessian2 = nd.Jacobian(lambda x: _ratematrix.loglikelihood(x, C)[1])(theta) hessian3 = nd.Hessian(lambda x: _ratematrix.loglikelihood(x, C)[0])(theta) np.set_printoptions(precision=3) # H1 = hessian1[np.ix_(active, active)] # H2 = hessian2[np.ix_(active, active)] # H3 = hessian2[np.ix_(active, active)] print(hessian1, '\n') print(hessian2, '\n') # print(hessian3) print('\n') info1 = np.zeros((len(theta), len(theta))) info2 = np.zeros((len(theta), len(theta))) info1[np.ix_(inds, inds)] = scipy.linalg.pinv(-hessian1) info2[np.ix_(inds, inds)] = scipy.linalg.pinv(-hessian2[np.ix_(inds, inds)]) print('Inverse Hessian') print(info1) print(info2) # print(scipy.linalg.pinv(hessian2)) # print(scipy.linalg.pinv(hessian1)[np.ix_(last, last)]) # print(scipy.linalg.pinv(hessian2)[np.ix_(last, last)]) print(_ratematrix.sigma_pi(info1, theta, n)) print(_ratematrix.sigma_pi(info2, theta, n))
def test_hessian_1(): n = 5 grid = NDGrid(n_bins_per_feature=n, min=-np.pi, max=np.pi) seqs = grid.fit_transform(load_doublewell(random_state=0)['trajectories']) model = ContinuousTimeMSM(use_sparse=False).fit(seqs) theta = model.theta_ C = model.countsmat_ hessian1 = _ratematrix.hessian(theta, C, n) Hfun = nd.Jacobian(lambda x: _ratematrix.loglikelihood(x, C, n)[1]) hessian2 = Hfun(theta) # not sure what the cutoff here should be (see plot_test_hessian) assert np.linalg.norm(hessian1-hessian2) < 1
def test_hessian_1(): n = 3 seqs = [[1,1,1,1,1,2,2,2,2,1,1,1,1,3,3,3,3,3,2,2,2,2,2,2,1,1,1,1,2,3,3,3,3]] model = ContinuousTimeMSM().fit(seqs) theta = model.theta_ C = model.countsmat_ hessian1 = _ratematrix.hessian(theta, C) Hfun = nd.Jacobian(lambda x: _ratematrix.loglikelihood(x, C)[1]) hessian2 = Hfun(theta) # not sure what the cutoff here should be (see plot_test_hessian) assert np.linalg.norm(hessian1-hessian2) < 1e-6 print(_ratematrix.sigma_pi(-scipy.linalg.pinv(hessian1), theta, n))
def test_hessian_2(): n = 3 seqs = [ [1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 3, 3]] model = ContinuousTimeMSM().fit(seqs) print(model.timescales_) print(model.uncertainty_timescales()) theta = model.theta_ C = model.countsmat_ print(C) C_flat = (C + C.T)[np.triu_indices_from(C, k=1)] print(C_flat) print('theta', theta, '\n') inds = np.where(theta != 0)[0] hessian1 = _ratematrix.hessian(theta, C, inds=inds) hessian2 = nd.Jacobian(lambda x: _ratematrix.loglikelihood(x, C)[1])(theta) hessian3 = nd.Hessian(lambda x: _ratematrix.loglikelihood(x, C)[0])(theta) np.set_printoptions(precision=3) # H1 = hessian1[np.ix_(active, active)] # H2 = hessian2[np.ix_(active, active)] # H3 = hessian2[np.ix_(active, active)] print(hessian1, '\n') print(hessian2, '\n') # print(hessian3) print('\n') info1 = np.zeros((len(theta), len(theta))) info2 = np.zeros((len(theta), len(theta))) info1[np.ix_(inds, inds)] = scipy.linalg.pinv(-hessian1) info2[np.ix_(inds, inds)] = scipy.linalg.pinv(-hessian2[np.ix_(inds, inds)]) print('Inverse Hessian') print(info1) print(info2) # print(scipy.linalg.pinv(hessian2)) # print(scipy.linalg.pinv(hessian1)[np.ix_(last, last)]) # print(scipy.linalg.pinv(hessian2)[np.ix_(last, last)]) print(_ratematrix.sigma_pi(info1, theta, n)) print(_ratematrix.sigma_pi(info2, theta, n))
def test_hessian_1(): n = 3 seqs = [[ 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 3, 3 ]] model = ContinuousTimeMSM().fit(seqs) theta = model.theta_ C = model.countsmat_ hessian1 = _ratematrix.hessian(theta, C) Hfun = nd.Jacobian(lambda x: _ratematrix.loglikelihood(x, C)[1]) hessian2 = Hfun(theta) # not sure what the cutoff here should be (see plot_test_hessian) assert np.linalg.norm(hessian1 - hessian2) < 1e-6 print(_ratematrix.sigma_pi(-scipy.linalg.pinv(hessian1), theta, n))
def _plot_test_hessian(): # plot the difference between the numerical hessian and the analytic # approximate hessian (opens Matplotlib window) n = 5 grid = NDGrid(n_bins_per_feature=n, min=-np.pi, max=np.pi) seqs = grid.fit_transform(load_doublewell(random_state=0)['trajectories']) model = ContinuousTimeMSM(use_sparse=False).fit(seqs) theta = model.theta_ C = model.countsmat_ hessian1 = _ratematrix.hessian(theta, C, n) Hfun = nd.Jacobian(lambda x: _ratematrix.loglikelihood(x, C, n)[1]) hessian2 = Hfun(theta) import matplotlib.pyplot as pp pp.scatter(hessian1.flat, hessian2.flat, marker='x') pp.plot(pp.xlim(), pp.xlim(), 'k') print('Plotting...', file=sys.stderr) pp.show()