def _update_mean(self, posts): """ Update the current mean. """ if len(posts) != self._nchains: raise ValueError("You must update all %d chains simultaneously." % self._nchains) for i in range(self._nchains): # update based on posterior # update.mu=function(x,use.core,use.sigma,prior){ # X=use.core[x,] X = np.array(posts[i]).flatten() # n=length(X) n = len(X) # mu0=prior$mu mu0 = self._mu # sigma.sq0=prior$sigma^2 sigma_sq0 = self._sigma ** 2 # sigma.sq=use.sigma[x]^2 sigma_sq = self._std[i] ** 2 # num=mu0/sigma.sq0 + sum(X)/sigma.sq num = mu0 / sigma_sq0 + X.sum() / sigma_sq # den=1/sigma.sq0 + n/sigma.sq den = 1.0 / sigma_sq0 + n / sigma_sq # mean=num/den mu = num / den # sigma=den^(-1/2) sigma = den ** (-1 / 2.0) # rnorm(1,mean,sigma) self._mean[i] = normal(mu, sigma).rvs() return self._mean
def _update_mean(self, posts): """ Update the current mean. """ if len(posts) != self._nchains: raise ValueError("You must update all %d chains simultaneously." % self._nchains) for i in range(self._nchains): # update based on posterior # update.mu=function(x,use.core,use.sigma,prior){ # X=use.core[x,] X = np.array(posts[i]).flatten() # n=length(X) n = len(X) # mu0=prior$mu mu0 = self._mu # sigma.sq0=prior$sigma^2 sigma_sq0 = self._sigma**2 # sigma.sq=use.sigma[x]^2 sigma_sq = self._std[i]**2 # num=mu0/sigma.sq0 + sum(X)/sigma.sq num = mu0 / sigma_sq0 + X.sum() / sigma_sq # den=1/sigma.sq0 + n/sigma.sq den = 1. / sigma_sq0 + n / sigma_sq # mean=num/den mu = num / den # sigma=den^(-1/2) sigma = den**(-1 / 2.) # rnorm(1,mean,sigma) self._mean[i] = normal(mu, sigma).rvs() return self._mean
def rvs(self, shape): rvs = np.zeros(shape) for i in range(shape[0]): ind = i % self._nchains rvs[i] = normal(self._mean[ind], self._std[ind]).rvs([1] + list(shape[1:])) return np.array(rvs)
def __init__(self, name='', display_name=None, mu=0.0, sigma=1.0, alpha=1.0, beta=1.0, nchains=1): self.name = name if display_name is None: display_name = self.name self.display_name = display_name # save the priors self._mu = mu self._sigma = sigma self._alpha = alpha self._beta = beta # save number of chains self._nchains = nchains # set initial values self._mean = normal(mu, sigma).rvs(nchains) self._std = np.sqrt(invgamma(alpha, beta).rvs(nchains))
def pdf(self, x): x = np.atleast_1d(x) if len(x) != self._nchains: # raise ValueError("You must request pdf for all %d chains simultaneously."%self._nchains) # warning, should only happen during initialization pass pdf = np.zeros(len(x)) for i in range(len(x)): # wrap around as necessary (might only happen at initialization) ind = i % self._nchains pdf[i] = normal(self._mean[ind], self._std[ind]).pdf(x[i]) return np.array(pdf)
def pdf(self, x): x = np.atleast_1d(x) if len(x) != self._nchains: #raise ValueError("You must request pdf for all %d chains simultaneously."%self._nchains) # warning, should only happen during initialization pass pdf = np.zeros(len(x)) for i in range(len(x)): # wrap around as necessary (might only happen at initialization) ind = i % self._nchains pdf[i] = normal(self._mean[ind], self._std[ind]).pdf(x[i]) return np.array(pdf)
def __init__(self, name="", display_name=None, mu=0.0, sigma=1.0, alpha=1.0, beta=1.0, nchains=1): self.name = name if display_name is None: display_name = self.name self.display_name = display_name # save the priors self._mu = mu self._sigma = sigma self._alpha = alpha self._beta = beta # save number of chains self._nchains = nchains # set initial values self._mean = normal(mu, sigma).rvs(nchains) self._std = np.sqrt(invgamma(alpha, beta).rvs(nchains))
def kdensity( x, extrema=None, kernel="gaussian", binwidth=None, nbins=512, weights=None, #bw="nrd0", adjust=1.0, cut=3, xx=None): """ """ # function (x, bw = "nrd0", adjust = 1, kernel = c("gaussian", # "epanechnikov", "rectangular", "triangular", "biweight", # "cosine", "optcosine"), weights = NULL, window = kernel, # width, give.Rkern = FALSE, n = 512, from, to, cut = 3, na.rm = FALSE, # ...) # { # if (length(list(...))) # warning("non-matched further arguments are disregarded") # if (!missing(window) && missing(kernel)) # kernel <- window # kernel <- match.arg(kernel) # if (give.Rkern) # return(switch(kernel, gaussian = 1/(2 * sqrt(pi)), rectangular = sqrt(3)/6, # triangular = sqrt(6)/9, epanechnikov = 3/(5 * sqrt(5)), # biweight = 5 * sqrt(7)/49, cosine = 3/4 * sqrt(1/3 - # 2/pi^2), optcosine = sqrt(1 - 8/pi^2) * pi^2/16)) # if (!is.numeric(x)) # stop("argument 'x' must be numeric") # name <- deparse(substitute(x)) # x <- as.vector(x) # x.na <- is.na(x) # if (any(x.na)) { # if (na.rm) # x <- x[!x.na] # else stop("'x' contains missing values") # } # N <- nx <- length(x) N = len(x) nx = len(x) # x.finite <- is.finite(x) # if (any(!x.finite)) { # x <- x[x.finite] # nx <- length(x) # } # if (is.null(weights)) { # weights <- rep.int(1/nx, nx) # totMass <- nx/N # } # else { # if (length(weights) != N) # stop("'x' and 'weights' have unequal length") # if (!all(is.finite(weights))) # stop("'weights' must all be finite") # if (any(weights < 0)) # stop("'weights' must not be negative") # wsum <- sum(weights) # if (any(!x.finite)) { # weights <- weights[x.finite] # totMass <- sum(weights)/wsum # } # else totMass <- 1 # if (!isTRUE(all.equal(1, wsum))) # warning("sum(weights) != 1 -- will not get true density") # } if weights is None: weights = np.ones(nx) / float(nx) totMass = nx / float(N) else: totMass = 1.0 # n.user <- n # n <- max(n, 512) # if (n > 512) # n <- 2^ceiling(log2(n)) nbins_user = nbins nbins = max(nbins, 512) if nbins > 512: nbins = int(2**np.ceil(np.log2(nbins))) # if (missing(bw) && !missing(width)) { # if (is.numeric(width)) { # fac <- switch(kernel, gaussian = 4, rectangular = 2 * # sqrt(3), triangular = 2 * sqrt(6), epanechnikov = 2 * # sqrt(5), biweight = 2 * sqrt(7), cosine = 2/sqrt(1/3 - # 2/pi^2), optcosine = 2/sqrt(1 - 8/pi^2)) # bw <- width/fac # } # if (is.character(width)) # bw <- width # } # if (is.character(bw)) { # if (nx < 2) # stop("need at least 2 points to select a bandwidth automatically") # bw <- switch(tolower(bw), nrd0 = bw.nrd0(x), nrd = bw.nrd(x), # ucv = bw.ucv(x), bcv = bw.bcv(x), sj = , `sj-ste` = bw.SJ(x, # method = "ste"), `sj-dpi` = bw.SJ(x, method = "dpi"), # stop("unknown bandwidth rule")) # } bw = nrd0(x) # if (!is.finite(bw)) # stop("non-finite 'bw'") # bw <- adjust * bw bw *= adjust # for some reason I have to multiply bw by 2 #bw *= 2. # if (bw <= 0) # stop("'bw' is not positive.") # if (missing(from)) # from <- min(x) - cut * bw # if (missing(to)) # to <- max(x) + cut * bw if extrema is None: extrema = (np.min(x) - cut * bw, np.max(x) + cut * bw) # if (!is.finite(from)) # stop("non-finite 'from'") # if (!is.finite(to)) # stop("non-finite 'to'") # lo <- from - 4 * bw # up <- to + 4 * bw lo = extrema[0] - 4 * bw up = extrema[1] + 4 * bw #print extrema,lo,up # y <- .Call(C_BinDist, x, weights, lo, up, n) * totMass #y = np.histogram(x, nbins=nbins, weights=weights, range=(lo,up))*totMass xi = x.copy() xi -= lo xi /= (up - lo) / (nbins - 1) xi = np.floor(xi) # Next, make a histogram of x # Avoiding np.histogram2d due to excessive memory usage with many points y = sp.sparse.coo_matrix((weights, np.vstack([xi, np.zeros_like(xi)])), shape=(nbins, 1)).toarray()[:, 0] * totMass # kords <- seq.int(0, 2 * (up - lo), length.out = 2L * n) kords = np.linspace(0, 2 * (up - lo), 2 * nbins) # kords[(n + 2):(2 * n)] <- -kords[n:2] kords[nbins + 1:] = -kords[nbins - 1:0:-1] # kords <- switch(kernel, gaussian = dnorm(kords, sd = bw), # rectangular = { # a <- bw * sqrt(3) # ifelse(abs(kords) < a, 0.5/a, 0) # }, triangular = { # a <- bw * sqrt(6) # ax <- abs(kords) # ifelse(ax < a, (1 - ax/a)/a, 0) # }, epanechnikov = { # a <- bw * sqrt(5) # ax <- abs(kords) # ifelse(ax < a, 3/4 * (1 - (ax/a)^2)/a, 0) # }, biweight = { # a <- bw * sqrt(7) # ax <- abs(kords) # ifelse(ax < a, 15/16 * (1 - (ax/a)^2)^2/a, 0) # }, cosine = { # a <- bw/sqrt(1/3 - 2/pi^2) # ifelse(abs(kords) < a, (1 + cos(pi * kords/a))/(2 * # a), 0) # }, optcosine = { # a <- bw/sqrt(1 - 8/pi^2) # ifelse(abs(kords) < a, pi/4 * cos(pi * kords/(2 * # a))/a, 0) # }) # NOTE: bw is doubled here to match doubled width of kernel bw2 = bw * 2. if kernel == 'gaussian': kords = normal(std=bw2).pdf(kords) elif kernel == 'epanechnikov': a = bw2 * np.sqrt(5) ax = np.abs(kords) ind = ax < a ax[ind] = .75 * (1 - (ax[ind] / a)**2) / a ax[~ind] = 0.0 kords = ax else: raise ValueError("Unknown kernel type.") # kords <- fft(fft(y) * Conj(fft(kords)), inverse = TRUE) kords = np.fft.ifft( np.concatenate([np.fft.fft(y)] * 2) * np.conj(np.fft.fft(kords))) # kords <- pmax.int(0, Re(kords)[1L:n]/length(y)) #kords = (np.real(kords)[:nbins]/float(len(y))).clip(0,np.inf) #kords = (np.real(kords)[::2]/float(len(y))).clip(0,np.inf) #kords = (np.real(kords)/float(len(y))).clip(0,np.inf) #kords = (np.real(kords)).clip(0,np.inf)*2. kords = (np.real(kords)[::2]).clip(0, np.inf) * 2. # xords <- seq.int(lo, up, length.out = n) xords = np.linspace(lo, up, nbins) # x <- seq.int(from, to, length.out = n.user) if xx is None: xx = np.linspace(extrema[0], extrema[1], nbins_user) pdf = np.interp(xx, xords, kords) # structure(list(x = x, y = approx(xords, kords, x)$y, bw = bw, # n = N, call = match.call(), data.name = name, has.na = FALSE), # class = "density") # } return pdf, xx
def kdensity(x, extrema=None, kernel="gaussian", binwidth=None, nbins=512, weights=None, #bw="nrd0", adjust=1.0, cut=3, xx=None): """ """ # function (x, bw = "nrd0", adjust = 1, kernel = c("gaussian", # "epanechnikov", "rectangular", "triangular", "biweight", # "cosine", "optcosine"), weights = NULL, window = kernel, # width, give.Rkern = FALSE, n = 512, from, to, cut = 3, na.rm = FALSE, # ...) # { # if (length(list(...))) # warning("non-matched further arguments are disregarded") # if (!missing(window) && missing(kernel)) # kernel <- window # kernel <- match.arg(kernel) # if (give.Rkern) # return(switch(kernel, gaussian = 1/(2 * sqrt(pi)), rectangular = sqrt(3)/6, # triangular = sqrt(6)/9, epanechnikov = 3/(5 * sqrt(5)), # biweight = 5 * sqrt(7)/49, cosine = 3/4 * sqrt(1/3 - # 2/pi^2), optcosine = sqrt(1 - 8/pi^2) * pi^2/16)) # if (!is.numeric(x)) # stop("argument 'x' must be numeric") # name <- deparse(substitute(x)) # x <- as.vector(x) # x.na <- is.na(x) # if (any(x.na)) { # if (na.rm) # x <- x[!x.na] # else stop("'x' contains missing values") # } # N <- nx <- length(x) N = len(x) nx = len(x) # x.finite <- is.finite(x) # if (any(!x.finite)) { # x <- x[x.finite] # nx <- length(x) # } # if (is.null(weights)) { # weights <- rep.int(1/nx, nx) # totMass <- nx/N # } # else { # if (length(weights) != N) # stop("'x' and 'weights' have unequal length") # if (!all(is.finite(weights))) # stop("'weights' must all be finite") # if (any(weights < 0)) # stop("'weights' must not be negative") # wsum <- sum(weights) # if (any(!x.finite)) { # weights <- weights[x.finite] # totMass <- sum(weights)/wsum # } # else totMass <- 1 # if (!isTRUE(all.equal(1, wsum))) # warning("sum(weights) != 1 -- will not get true density") # } if weights is None: weights = np.ones(nx)/float(nx) totMass = nx/float(N) else: totMass = 1.0 # n.user <- n # n <- max(n, 512) # if (n > 512) # n <- 2^ceiling(log2(n)) nbins_user = nbins nbins = max(nbins,512) if nbins > 512: nbins = int(2**np.ceil(np.log2(nbins))) # if (missing(bw) && !missing(width)) { # if (is.numeric(width)) { # fac <- switch(kernel, gaussian = 4, rectangular = 2 * # sqrt(3), triangular = 2 * sqrt(6), epanechnikov = 2 * # sqrt(5), biweight = 2 * sqrt(7), cosine = 2/sqrt(1/3 - # 2/pi^2), optcosine = 2/sqrt(1 - 8/pi^2)) # bw <- width/fac # } # if (is.character(width)) # bw <- width # } # if (is.character(bw)) { # if (nx < 2) # stop("need at least 2 points to select a bandwidth automatically") # bw <- switch(tolower(bw), nrd0 = bw.nrd0(x), nrd = bw.nrd(x), # ucv = bw.ucv(x), bcv = bw.bcv(x), sj = , `sj-ste` = bw.SJ(x, # method = "ste"), `sj-dpi` = bw.SJ(x, method = "dpi"), # stop("unknown bandwidth rule")) # } bw = nrd0(x) # if (!is.finite(bw)) # stop("non-finite 'bw'") # bw <- adjust * bw bw *= adjust # for some reason I have to multiply bw by 2 #bw *= 2. # if (bw <= 0) # stop("'bw' is not positive.") # if (missing(from)) # from <- min(x) - cut * bw # if (missing(to)) # to <- max(x) + cut * bw if extrema is None: extrema = (np.min(x) - cut*bw, np.max(x) + cut*bw) # if (!is.finite(from)) # stop("non-finite 'from'") # if (!is.finite(to)) # stop("non-finite 'to'") # lo <- from - 4 * bw # up <- to + 4 * bw lo = extrema[0] - 4*bw up = extrema[1] + 4*bw #print extrema,lo,up # y <- .Call(C_BinDist, x, weights, lo, up, n) * totMass #y = np.histogram(x, nbins=nbins, weights=weights, range=(lo,up))*totMass xi = x.copy() xi -= lo xi /= (up - lo) / (nbins - 1) xi = np.floor(xi) # Next, make a histogram of x # Avoiding np.histogram2d due to excessive memory usage with many points y = sp.sparse.coo_matrix((weights, np.vstack([xi,np.zeros_like(xi)])), shape=(nbins,1)).toarray()[:,0] * totMass # kords <- seq.int(0, 2 * (up - lo), length.out = 2L * n) kords = np.linspace(0,2*(up-lo),2*nbins) # kords[(n + 2):(2 * n)] <- -kords[n:2] kords[nbins+1:] = -kords[nbins-1:0:-1] # kords <- switch(kernel, gaussian = dnorm(kords, sd = bw), # rectangular = { # a <- bw * sqrt(3) # ifelse(abs(kords) < a, 0.5/a, 0) # }, triangular = { # a <- bw * sqrt(6) # ax <- abs(kords) # ifelse(ax < a, (1 - ax/a)/a, 0) # }, epanechnikov = { # a <- bw * sqrt(5) # ax <- abs(kords) # ifelse(ax < a, 3/4 * (1 - (ax/a)^2)/a, 0) # }, biweight = { # a <- bw * sqrt(7) # ax <- abs(kords) # ifelse(ax < a, 15/16 * (1 - (ax/a)^2)^2/a, 0) # }, cosine = { # a <- bw/sqrt(1/3 - 2/pi^2) # ifelse(abs(kords) < a, (1 + cos(pi * kords/a))/(2 * # a), 0) # }, optcosine = { # a <- bw/sqrt(1 - 8/pi^2) # ifelse(abs(kords) < a, pi/4 * cos(pi * kords/(2 * # a))/a, 0) # }) # NOTE: bw is doubled here to match doubled width of kernel bw2 = bw*2. if kernel == 'gaussian': kords = normal(std=bw2).pdf(kords) elif kernel == 'epanechnikov': a = bw2 * np.sqrt(5) ax = np.abs(kords) ind = ax<a ax[ind] = .75 * (1-(ax[ind]/a)**2)/a ax[~ind] = 0.0 kords = ax else: raise ValueError("Unknown kernel type.") # kords <- fft(fft(y) * Conj(fft(kords)), inverse = TRUE) kords = np.fft.ifft(np.concatenate([np.fft.fft(y)]*2)*np.conj(np.fft.fft(kords))) # kords <- pmax.int(0, Re(kords)[1L:n]/length(y)) #kords = (np.real(kords)[:nbins]/float(len(y))).clip(0,np.inf) #kords = (np.real(kords)[::2]/float(len(y))).clip(0,np.inf) #kords = (np.real(kords)/float(len(y))).clip(0,np.inf) #kords = (np.real(kords)).clip(0,np.inf)*2. kords = (np.real(kords)[::2]).clip(0,np.inf)*2. # xords <- seq.int(lo, up, length.out = n) xords = np.linspace(lo,up,nbins) # x <- seq.int(from, to, length.out = n.user) if xx is None: xx = np.linspace(extrema[0],extrema[1],nbins_user) pdf = np.interp(xx,xords,kords) # structure(list(x = x, y = approx(xords, kords, x)$y, bw = bw, # n = N, call = match.call(), data.name = name, has.na = FALSE), # class = "density") # } return pdf,xx