-
-
Save ariddell/4631160 to your computer and use it in GitHub Desktop.
Revisions
-
Chris Fonnesbeck revised this gist
May 11, 2012 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,10 +16,8 @@ cdef extern from "math.h": double sqrt(double) cdef extern from "gsl/gsl_rng.h": ctypedef struct gsl_rng_type ctypedef struct gsl_rng gsl_rng_type *gsl_rng_mt19937 gsl_rng *gsl_rng_alloc(gsl_rng_type * T) nogil -
Chris Fonnesbeck revised this gist
May 11, 2012 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,9 +14,6 @@ from numpy cimport * cdef extern from "math.h": double sqrt(double) cdef extern from "gsl/gsl_rng.h": ctypedef struct gsl_rng_type: -
Chris Fonnesbeck revised this gist
May 11, 2012 . 1 changed file with 13 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,30 +8,32 @@ using conditional distributions: x|y \sim Gamma(3, y^2 +4) y|x \sim Normal(\frac{1}{1+x}, \frac{1}{2(1+x)}) ''' cimport cython import numpy as np from numpy cimport * cdef extern from "math.h": double sqrt(double) #cdef double Sqrt(double n): # return sqrt(n) cdef extern from "gsl/gsl_rng.h": ctypedef struct gsl_rng_type: pass ctypedef struct gsl_rng: pass gsl_rng_type *gsl_rng_mt19937 gsl_rng *gsl_rng_alloc(gsl_rng_type * T) nogil cdef extern from "gsl/gsl_randist.h": double gamma "gsl_ran_gamma"(gsl_rng * r,double,double) double gaussian "gsl_ran_gaussian"(gsl_rng * r,double) cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937) @cython.boundscheck(False) def gibbs(int N=20000,int thin=500): cdef: double x=0 @@ -43,6 +45,7 @@ def gibbs(int N=20000,int thin=500): for i from 0 <= i < N: for j from 0 <= j < thin: x = gamma(r,3,1.0/(y*y+4)) y = gaussian(r,1.0/sqrt(x+1)) samples[i,0] = x samples[i,1] = y return samples -
Chris Fonnesbeck renamed this gist
May 11, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Chris Fonnesbeck created this gist
May 11, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ ''' Gibbs sampler for function: f(x,y) = x x^2 \exp(-xy^2 - y^2 + 2y - 4x) using conditional distributions: x|y \sim Gamma(3, y^2 +4) y|x \sim Normal(\frac{1}{1+x}, \frac{1}{2(1+x)}) ''' cimport cpython import numpy as np from numpy cimport * cdef extern from "math.h": double sqrt(double) cdef double Sqrt(double n): return sqrt(n) cdef extern from "gsl/gsl_rng.h": ctypedef struct gsl_rng_type: pass ctypedef struct gsl_rng: pass gsl_rng_type *gsl_rng_mt19937 gsl_rng *gsl_rng_alloc(gsl_rng_type * T) cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937) cdef extern from "gsl/gsl_randist.h": double gamma "gsl_ran_gamma"(gsl_rng * r,double,double) double gaussian "gsl_ran_gaussian"(gsl_rng * r,double) def gibbs(int N=20000,int thin=500): cdef: double x=0 double y=0 int i, j ndarray[float64_t, ndim=2] samples samples = np.empty((N,thin)) for i from 0 <= i < N: for j from 0 <= j < thin: x = gamma(r,3,1.0/(y*y+4)) y = gaussian(r,1.0/Sqrt(x+1)) samples[i] = x,y return samples