#s1.sample(1e3, temperature=1) #print("Gaussian Sampler error: %f" % c.approx_error_data(s1.db, norm_test, labels)) #gavg = c.calc_gavg(s1.db, grid, 50).reshape(-1,n) #myplot(p.subplot(2,3,2),gavg) # MPM Model n,gext,grid = get_grid_data(np.vstack(( data0, data1 )), positive=True) dist0 = MPMDist(data0,kmax=1) dist1 = MPMDist(data1,kmax=1) mpm = MPMCls(dist0, dist1) #s2 = samc.SAMCRun(mpm, burn=0, stepscale=1000, refden=1, thin=10, #lim_iters=100, low_margin=0.2, high_margin=-0.5) #s2.sample(2e5, temperature=2) mh = mh.MHRun(mpm, burn=100, thin=20) mh.sample(5e3,verbose=False) print("MPM Sampler error: %f" % mpm.approx_error_data(mh.db, test, labels)) numlam = 200 gavg = mpm.calc_gavg(mh.db, grid, numlam=numlam).reshape(-1,n) #g = mpm.calc_curr_g(grid).reshape(-1,n) ga1 = mpm.dist0.calc_db_g(mh.db, mh.db.root.object.dist0, grid, numlam=numlam).reshape(-1,n) ga2 = mpm.dist1.calc_db_g(mh.db, mh.db.root.object.dist1, grid, numlam=numlam).reshape(-1,n) myplot(p.subplot(2,3,2),gavg,data0,data1) myplot(p.subplot(2,3,3),ga1,data0,data1) myplot(p.subplot(2,3,4),ga2,data0,data1) myplot(p.subplot(2,3,5),gavg,test[:500,:],test[500:,:]) p.subplot(2,3,6) p.plot(test[500:,0], test[500:,1],'m.',alpha=0.5)
labels = np.hstack((np.zeros(N / 2), np.ones(N / 2))) bayes0 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D) * 3, norm_trn_data0) bayes1 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D) * 3, norm_trn_data1) # Gaussian Analytic gc = GaussianCls(bayes0, bayes1) errors['gauss'] = gc.approx_error_data(norm_tst_data, labels) print("Gaussian Analytic error: %f" % errors['gauss']) # MPM Model numlam = 100 dist0 = MPMDist(trn_data0, kmax=1, priorkappa=80, lammove=0.02, mumove=0.1) dist1 = MPMDist(trn_data1, kmax=1, priorkappa=80, lammove=0.02, mumove=0.1) mpm = MPMCls(dist0, dist1) mh = mh.MHRun(mpm, burn=100, thin=20) mh.sample(2e3, verbose=False) errors['mpm'] = mpm.approx_error_data(mh.db, tst_data, labels, numlam=numlam) print("MPM Sampler error: %f" % errors['mpm']) output['acceptance'] = float(mh.accept_loc) / mh.total_loc output['seed'] = seed p.figure() def myplot(ax, g, data0, data1, gext): ax.plot(data0[:, 0], data0[:, 1], 'g.', label='0', alpha=0.3) ax.plot(data1[:, 0], data1[:, 1], 'r.', label='1', alpha=0.3) ax.legend(fontsize=8, loc='best') im = ax.imshow(g, extent=gext, aspect='equal', origin='lower')
import pandas as pa import samcnet.mh as mh from samcnet.mixturepoisson import * trn_data0 = pa.read_csv('tests/ex_data_0.csv', header=None) trn_data1 = pa.read_csv('tests/ex_data_1.csv', header=None) predict_samples = pa.read_csv('tests/ex_data_predict.csv', header=None) dist0 = MPMDist(trn_data0) dist1 = MPMDist(trn_data1) mpm = MPMCls(dist0, dist1) mh = mh.MHRun(mpm, burn=1000, thin=50, verbose=True) mh.sample(1e4) print(mpm.predict(mh.db, predict_samples)) mh.db.close()
bayes0 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D) * 3, norm_trn_data0) bayes1 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D) * 3, norm_trn_data1) gc = GaussianCls(bayes0, bayes1) output['gausserr'] = gc.approx_error_data(norm_tst_data, tst_labels) print("Gaussian Analytic error: %f" % output['gausserr']) # MPM Model dist0 = MPMDist(trn_data0, kmax=1, priorkappa=120, mumove=0.1, lammove=0.05) dist1 = MPMDist(trn_data1, kmax=1, priorkappa=120, mumove=0.1, lammove=0.05) mpm = MPMCls(dist0, dist1) mh = mh.MHRun(mpm, burn=100, thin=20) t1 = time() iters = 2e3 numlam = 50 mh.sample(iters, verbose=False) output['mpmerr'] = mpm.approx_error_data(mh.db, tst_data, tst_labels, numlam=numlam) print("MPM Sampler error: %f" % output['mpmerr']) print "Whole run with %d iters and %d numlam took %f seconds" % (iters, numlam, time() - t1) p.figure() def myplot(ax, g, data0, data1, gext): ax.plot(data0[:, 0], data0[:, 1], 'g.', label='0', alpha=0.3) ax.plot(data1[:, 0], data1[:, 1], 'r.', label='1', alpha=0.3) ax.legend(fontsize=8, loc='best')
print("skSVM error: %f" % output['svmerr']) # Gaussian Analytic bayes0 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D)*3, norm_trn_data0) bayes1 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D)*3, norm_trn_data1) gc = GaussianCls(bayes0, bayes1) output['gausserr'] = gc.approx_error_data(norm_tst_data, tst_labels) print("Gaussian Analytic error: %f" % output['gausserr']) # MPM Model dist0 = MPMDist(trn_data0,kmax=1) dist1 = MPMDist(trn_data1,kmax=1) mpm = MPMCls(dist0, dist1) mh = mh.MHRun(mpm, burn=1, thin=2) mh.sample(40,verbose=False) output['mpmerr'] = mpm.approx_error_data(mh.db, tst_data, tst_labels,numlam=200) print("MPM Sampler error: %f" % output['mpmerr']) if 'WORKHASH' in os.environ: import zmq,time,zlib import simplejson as js ctx = zmq.Context() socket = ctx.socket(zmq.REQ) socket.connect('tcp://'+server+':7000') #data = mh.read_db() data = zlib.compress(js.dumps(output)) socket.send(os.environ['WORKHASH'], zmq.SNDMORE) socket.send(data) socket.recv()
bayes0 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D)*3, norm_trn_data0) bayes1 = GaussianBayes(np.zeros(D), 1, 8, np.eye(D)*3, norm_trn_data1) gc = GaussianCls(bayes0, bayes1) output['gausserr'] = gc.approx_error_data(norm_tst_data, tst_labels) print("Gaussian Analytic error: %f" % output['gausserr']) # MPM Model dist0 = MPMDist(trn_data0,kmax=1,priorkappa=120,mumove=0.1,lammove=0.05) dist1 = MPMDist(trn_data1,kmax=1,priorkappa=120,mumove=0.1,lammove=0.05) mpm = MPMCls(dist0, dist1) mh = mh.MHRun(mpm, burn=100, thin=20) t1=time() iters = 2e3 numlam = 50 mh.sample(iters,verbose=False) output['mpmerr'] = mpm.approx_error_data(mh.db, tst_data, tst_labels,numlam=numlam) print("MPM Sampler error: %f" % output['mpmerr']) print "Whole run with %d iters and %d numlam took %f seconds" % (iters, numlam, time()-t1) p.figure() def myplot(ax,g,data0,data1,gext): ax.plot(data0[:,0], data0[:,1], 'g.',label='0', alpha=0.3) ax.plot(data1[:,0], data1[:,1], 'r.',label='1', alpha=0.3) ax.legend(fontsize=8, loc='best') im = ax.imshow(g, extent=gext, aspect='equal', origin='lower') p.colorbar(im,ax=ax) ax.contour(g, [0.0], extent=gext, aspect=1, origin='lower', cmap = p.cm.gray) def jit(x): return x+np.random.randn(*x.shape)/4.0