def main(): M1 = Mvn.rand(2) M2 = Mvn.rand(2) data1 = M1.sample(100) data2 = M2.sample(100) M1 = Mvn.fromData(data1) M2 = Mvn.fromData(data2) M3 = Mvn.fromData([M1, M2]) data3 = Matrix.stack([[data1], [data2]]) assert M3 == Mvn.fromData(data3) A = pylab.gca() M3.plot(A, facecolor='m', minalpha=0.1, zorder=-1) M1.plot(A, facecolor='b', minalpha=0.1, zorder=0) M2.plot(A, facecolor='r', minalpha=0.1, zorder=1) pylab.scatter(data1[:, 0], data1[:, 1], facecolor='b', zorder=2) pylab.scatter(data2[:, 0], data2[:, 1], facecolor='r', zorder=3) pylab.show()
def main(): M1=Mvn.rand(2) M2=Mvn.rand(2) data1 = M1.sample(100) data2 = M2.sample(100) M1 = Mvn.fromData(data1) M2 = Mvn.fromData(data2) M3 = Mvn.fromData([M1,M2]) data3 = Matrix.stack([[data1],[data2]]) assert M3 == Mvn.fromData(data3) A=pylab.gca() M3.plot(A,facecolor='m',minalpha=0.1,zorder = -1) M1.plot(A,facecolor='b',minalpha = 0.1,zorder = 0) M2.plot(A,facecolor='r',minalpha = 0.1,zorder = 1) pylab.scatter(data1[:,0],data1[:,1],facecolor='b',zorder = 2) pylab.scatter(data2[:,0],data2[:,1],facecolor='r',zorder = 3) pylab.show()
def main(): #generate data N = 75 red = Mvn.rand(2) blue = Matrix(red.sample(N)) red = Mvn.fromData(blue) #create figure fig = pylab.figure(1, figsize=(7, 7)) fig.suptitle('Mahalabois Distance') ax = setupAxes(red.transform(-1)) # scatter plot of the origional data scatter(ax[0, 0], red, blue) # scatter plot of the normalized data scatter(ax[1, 0], red / red, blue / red) # draw the cumulative distribution cumulative(ax[0, 1], red, blue) # draw the histogram hist(red.mah().pdf, red.mah(blue), ax[1, 1]) ax[0, 1].set_xlim([0, None]) ax[1, 1].set_ylim([0, None]) pylab.show()
def main(): #generate data N=75 red = Mvn.rand(2) blue = Matrix(red.sample(N)) red = Mvn.fromData(blue) #create figure fig = pylab.figure(1, figsize=(7, 7)) fig.suptitle('Mahalabois Distance') ax = setupAxes(red.transform(-1)) # scatter plot of the origional data scatter(ax[0,0],red,blue) # scatter plot of the normalized data scatter(ax[1,0],red/red,blue/red) # draw the cumulative distribution cumulative(ax[0,1],red,blue) # draw the histogram hist(red.mah().pdf,red.mah(blue),ax[1,1]) ax[0,1].set_xlim([0,None]) ax[1,1].set_ylim([0,None]) pylab.show()
pylab.gcf().clear() pi1 = sum(W1) pi2 = sum(W2) (pi1, pi2) = [pi1 / (pi1 + pi2), pi2 / (pi1 + pi2)] d1 = R1.density(data) * pi1 d2 = R2.density(data) * pi2 (W1, W2) = [ d1 / (d1 + d2), d2 / (d1 + d2), ] R1 = Mvn.fromData(data=data, weights=W1, bias=True) R2 = Mvn.fromData(data=data, weights=W2, bias=True) #print 'W1=%s' % sum(W1) #print 'W2=%s' % sum(W2) pylab.scatter(data[:, 0], data[:, 1], c='r', alpha=0.5, zorder=3) R1.plot(zorder=2) R2.plot(zorder=1) # pylab.gca().add_artist(R1.patch()) # pylab.gca().add_artist(R2.patch()) pylab.draw() p = sum(pi1 * W1 * pi2 * W2) print 'p=%s' % p # if abs(p-old_p) <0.0000001:
def main(): N=75#numpy.round(10.0**(1.5+1*numpy.random.rand())) #generate data red = Mvn.rand(2) blue = red.sample(N) red = Mvn.fromData(blue) #create figure fig = pylab.figure(1, figsize=(7, 7)) fig.suptitle('Mahalabois Distance') axgrid = GridSpec(2, 2) ax = numpy.empty([2, 2],dtype = object) #get axes ax[0, 0] = subplot(axgrid[0, 0]) ax[0, 0].axis('equal') ax[0, 0].grid('on') ax[0, 0].set_title('red') @curry def forewardTransform(center, M, x, y): center = center.squeeze() x = Matrix(x) y = Matrix(y) xy = numpy.hstack([x.T, y.T]) xy = xy - center[None, :] xy = numpy.array(xy*M) mags = (numpy.array(xy)**2).sum(1)[:, None]**0.5 dirs = xy/mags xy = dirs*mags**2 return xy[:, 0].squeeze(), xy[:, 1].squeeze() @curry def reverseTransform(center, M, x, y): center = center.squeeze() x = Matrix(x) y = Matrix(y) xy = numpy.hstack([x.T, y.T]) xy = xy - center[None, :] xy = numpy.array(xy*M) mags = (numpy.array(xy)**2).sum(1)[:, None]**0.5 dirs = xy/mags xy = dirs*mags**0.5 + center return xy[:, 0].squeeze(), xy[:, 1].squeeze() foreward = forewardTransform(red.mean, red.transform(-1)) reverse = reverseTransform(red.mean, red.transform(1)) grid_helper = GridHelperCurveLinear([ foreward, reverse ]) ax[1, 0] = subplot(axgrid[1, 0], projection = 'custom', transform = grid_helper ) ax[1, 0].axis('equal') ax[1, 0].grid('on') ax[1, 0].set_title('red/red') ax[0, 1] = subplot(axgrid[0, 1]) ax[0, 1].grid('on') ax[0, 1].set_title('red.mah2().cdf()') ax[0, 1].set_ylim([0, 1]) ax[1, 1] = pylab.subplot(axgrid[1, 1], sharex=ax[0, 1])#,sharey=ax[0,1]) ax[1, 1].grid('on') ax[1,1].set_title('red.mah2().pdf()') scatter(ax[0, 0], red, blue) # blue0 = Matrix(blue)/red # blue0 = blue0.array() red0 = red/red blue0 = foreward(blue[:, 0], blue[:, 1]) blue0 = numpy.hstack([blue0[0][:, None], blue0[1][:, None]]) scatter(ax[1, 0], red0, blue0) mah2 = red.mah2(blue) mah2.sort() ax[0, 1].hlines(numpy.arange(mah2.size, dtype = float)/mah2.size, 0,mah2, color = 'b', alpha = alpha) ax[0, 1].scatter(mah2, numpy.arange(mah2.size, dtype = float)/mah2.size, color = 'b', alpha = alpha) x = numpy.linspace(*ax[0, 1].get_xlim(), num=500) ax[0, 1].plot(x, red.mah2().cdf(x), color = 'r', linewidth = 2, alpha = alpha) hist(red.mah2().pdf, red.mah2(blue), ax[1, 1]) ax[0, 1].set_xlim([0, None]) ax[1, 1].set_ylim([0, None]) pylab.show()
pi2 = sum(W2) (pi1,pi2) = [ pi1/(pi1+pi2), pi2/(pi1+pi2) ] d1 = R1.density(data)*pi1 d2 = R2.density(data)*pi2 (W1,W2) = [ d1/(d1+d2), d2/(d1+d2), ] R1 = Mvn.fromData(data = data,weights = W1,bias=True) R2 = Mvn.fromData(data = data,weights = W2,bias=True) #print 'W1=%s' % sum(W1) #print 'W2=%s' % sum(W2) pylab.scatter(data[:,0],data[:,1],c='r',alpha=0.5, zorder = 3) R1.plot(zorder = 2) R2.plot(zorder = 1) # pylab.gca().add_artist(R1.patch()) # pylab.gca().add_artist(R2.patch()) pylab.draw() p=sum(pi1*W1*pi2*W2) print 'p=%s' % p # if abs(p-old_p) <0.0000001:
def main(): N = 75 #numpy.round(10.0**(1.5+1*numpy.random.rand())) #generate data red = Mvn.rand(2) blue = red.sample(N) red = Mvn.fromData(blue) #create figure fig = pylab.figure(1, figsize=(7, 7)) fig.suptitle('Mahalabois Distance') axgrid = GridSpec(2, 2) ax = numpy.empty([2, 2], dtype=object) #get axes ax[0, 0] = subplot(axgrid[0, 0]) ax[0, 0].axis('equal') ax[0, 0].grid('on') ax[0, 0].set_title('red') @curry def forewardTransform(center, M, x, y): center = center.squeeze() x = Matrix(x) y = Matrix(y) xy = numpy.hstack([x.T, y.T]) xy = xy - center[None, :] xy = numpy.array(xy * M) mags = (numpy.array(xy)**2).sum(1)[:, None]**0.5 dirs = xy / mags xy = dirs * mags**2 return xy[:, 0].squeeze(), xy[:, 1].squeeze() @curry def reverseTransform(center, M, x, y): center = center.squeeze() x = Matrix(x) y = Matrix(y) xy = numpy.hstack([x.T, y.T]) xy = xy - center[None, :] xy = numpy.array(xy * M) mags = (numpy.array(xy)**2).sum(1)[:, None]**0.5 dirs = xy / mags xy = dirs * mags**0.5 + center return xy[:, 0].squeeze(), xy[:, 1].squeeze() foreward = forewardTransform(red.mean, red.transform(-1)) reverse = reverseTransform(red.mean, red.transform(1)) grid_helper = GridHelperCurveLinear([foreward, reverse]) ax[1, 0] = subplot(axgrid[1, 0], projection='custom', transform=grid_helper) ax[1, 0].axis('equal') ax[1, 0].grid('on') ax[1, 0].set_title('red/red') ax[0, 1] = subplot(axgrid[0, 1]) ax[0, 1].grid('on') ax[0, 1].set_title('red.mah2().cdf()') ax[0, 1].set_ylim([0, 1]) ax[1, 1] = pylab.subplot(axgrid[1, 1], sharex=ax[0, 1]) #,sharey=ax[0,1]) ax[1, 1].grid('on') ax[1, 1].set_title('red.mah2().pdf()') scatter(ax[0, 0], red, blue) # blue0 = Matrix(blue)/red # blue0 = blue0.array() red0 = red / red blue0 = foreward(blue[:, 0], blue[:, 1]) blue0 = numpy.hstack([blue0[0][:, None], blue0[1][:, None]]) scatter(ax[1, 0], red0, blue0) mah2 = red.mah2(blue) mah2.sort() ax[0, 1].hlines(numpy.arange(mah2.size, dtype=float) / mah2.size, 0, mah2, color='b', alpha=alpha) ax[0, 1].scatter(mah2, numpy.arange(mah2.size, dtype=float) / mah2.size, color='b', alpha=alpha) x = numpy.linspace(*ax[0, 1].get_xlim(), num=500) ax[0, 1].plot(x, red.mah2().cdf(x), color='r', linewidth=2, alpha=alpha) hist(red.mah2().pdf, red.mah2(blue), ax[1, 1]) ax[0, 1].set_xlim([0, None]) ax[1, 1].set_ylim([0, None]) pylab.show()