from numpy import zeros, random, sqrt gamma = random.gamma normal = random.normal def pygibbs(N=20000, thin=200): mat = zeros((N,2)) x,y = mat[0] for i in range(N): for j in range(thin): x = gamma(3, y**2 + 4) y = normal(1./(x+1), 1./sqrt(2*(x+1))) mat[i] = x,y return mat y = 5 y = 'foo' x = y = [1, 2, 3] y[0] = -9 x '5' + 6 # Calculate second difference matrix import numpy as np I2 = -2*np.eye(8) E = np.diag(np.ones(7), k=-1) I2 + E + E.T from scipy import linalg def lse(A, b, B, d, cond=None): """ Equality-contrained least squares. The following algorithm minimizes ||Ax - b|| subject to the constrain Bx = d. """ A, b, B, d = map(np.asanyarray, (A, b, B, d)) p = B.shape[0] # QR decomposition of constraint matrix B Q, R = linalg.qr(B.T) # Solve Ax = b, assuming A is triangular y = linalg.solve_triangular(R[:p, :p], d, trans='T', lower=False) A = np.dot(A, Q) # Least squares solution to Ax = b z = linalg.lstsq(A[:, p:], b - np.dot(A[:, :p], y), cond=cond)[0].ravel() return np.dot(Q[:, :p], y) + np.dot(Q[:, p:], z) A, b = [[0, 2, 3], [1, 3, 4.5]], [1, 1] B, d = [[1, 1, 0]], [1] lse(A, b, B, d) import matplotlib.pyplot as plt fig = plt.figure() x = np.linspace(0,2*np.pi,100) y = 2*np.sin(x) ax = fig.add_subplot(1,2,1) ax.plot(x,y) y2 = y + 0.1*np.random.normal( size=x.shape ) ax = fig.add_subplot(1,2,2) ax.plot(x,y2,'bo') from pymc.examples import gelman_bioassay from pymc import MCMC, Matplot M = MCMC(gelman_bioassay) M.sample(1000,verbose=0) Matplot.plot(M.alpha) import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Polygon # Generate some data from five different probability distributions, # each with different characteristics. We want to play with how an IID # bootstrap resample of the data preserves the distributional # properties of the original sample, and a boxplot is one visual tool # to make this assessment numDists = 5 randomDists = ['Normal(1,1)',' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)', 'Triangular(2,9,11)'] N = 500 norm = np.random.normal(1,1, N) logn = np.random.lognormal(1,1, N) expo = np.random.exponential(1, N) gumb = np.random.gumbel(6, 4, N) tria = np.random.triangular(2, 9, 11, N) # Generate some random indices that we'll use to resample the original data # arrays. For code brevity, just use the same random indices for each array bootstrapIndices = np.random.random_integers(0, N-1, N) normBoot = norm[bootstrapIndices] expoBoot = expo[bootstrapIndices] gumbBoot = gumb[bootstrapIndices] lognBoot = logn[bootstrapIndices] triaBoot = tria[bootstrapIndices] data = [norm, normBoot, logn, lognBoot, expo, expoBoot, gumb, gumbBoot, tria, triaBoot] fig = plt.figure(figsize=(10,6)) fig.canvas.set_window_title('A Boxplot Example') ax1 = fig.add_subplot(111) plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25) bp = plt.boxplot(data, notch=0, sym='+', vert=1, whis=1.5) plt.setp(bp['boxes'], color='black') plt.setp(bp['whiskers'], color='black') plt.setp(bp['fliers'], color='red', marker='+') # Add a horizontal grid to the plot, but make it very light in color # so we can use it for reading data values but not be distracting ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) # Hide these grid behind plot objects ax1.set_axisbelow(True) ax1.set_title('Comparison of IID Bootstrap Resampling Across Five Distributions') ax1.set_xlabel('Distribution') ax1.set_ylabel('Value') # Now fill the boxes with desired colors boxColors = ['darkkhaki','royalblue'] numBoxes = numDists*2 medians = range(numBoxes) for i in range(numBoxes): box = bp['boxes'][i] boxX = [] boxY = [] for j in range(5): boxX.append(box.get_xdata()[j]) boxY.append(box.get_ydata()[j]) boxCoords = zip(boxX,boxY) # Alternate between Dark Khaki and Royal Blue k = i % 2 boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) ax1.add_patch(boxPolygon) # Now draw the median lines back over what we just filled in med = bp['medians'][i] medianX = [] medianY = [] for j in range(2): medianX.append(med.get_xdata()[j]) medianY.append(med.get_ydata()[j]) plt.plot(medianX, medianY, 'k') medians[i] = medianY[0] # Finally, overplot the sample averages, with horixzontal alignment # in the center of each box plt.plot([np.average(med.get_xdata())], [np.average(data[i])], color='w', marker='*', markeredgecolor='k') # Set the axes ranges and axes labels ax1.set_xlim(0.5, numBoxes+0.5) top = 40 bottom = -5 ax1.set_ylim(bottom, top) xtickNames = plt.setp(ax1, xticklabels=np.repeat(randomDists, 2)) plt.setp(xtickNames, rotation=45, fontsize=8) # Due to the Y-axis scale being different across samples, it can be # hard to compare differences in medians across the samples. Add upper # X-axis tick labels with the sample medians to aid in comparison # (just use two decimal places of precision) pos = np.arange(numBoxes)+1 upperLabels = [str(np.round(s, 2)) for s in medians] weights = ['bold', 'semibold'] for tick,label in zip(range(numBoxes),ax1.get_xticklabels()): k = tick % 2 ax1.text(pos[tick], top-(top*0.05), upperLabels[tick], horizontalalignment='center', size='x-small', weight=weights[k], color=boxColors[k]) # Finally, add a basic legend plt.figtext(0.80, 0.08, str(N) + ' Random Numbers' , backgroundcolor=boxColors[0], color='black', weight='roman', size='x-small') plt.figtext(0.80, 0.045, 'IID Bootstrap Resample', backgroundcolor=boxColors[1], color='white', weight='roman', size='x-small') plt.figtext(0.80, 0.015, '*', color='white', backgroundcolor='silver', weight='roman', size='medium') plt.figtext(0.815, 0.013, ' Average Value', color='black', weight='roman', size='x-small') plt.show() %load_ext rmagic x,y = arange(10), random.normal(size=10) %%R -i x,y -o XYcoef lm.fit <- lm(y~x) par(mfrow=c(2,2)) plot(lm.fit) XYcoef <- coef(lm.fit) XYcoef %%R library(compiler) library(rbenchmark) library(inline) library(RcppGSL) Rgibbs <- function(N,thin) { mat <- matrix(0,ncol=2,nrow=N) x <- 0 y <- 0 for (i in 1:N) { for (j in 1:thin) { x <- rgamma(1,3,y*y+4) y <- rnorm(1,1/(x+1),1/sqrt(2*(x+1))) } mat[i,] <- c(x,y) } mat } RCgibbs <- cmpfun(Rgibbs) gibbscode <- ' // n and thin are SEXPs which the Rcpp::as function maps to C++ vars int N = as(n); int thn = as(thin); int i,j; NumericMatrix mat(N, 2); RNGScope scope; // Initialize Random number generator // The rest of the code follows the R version double x=0, y=0; for (i=0; i