- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
Simple Logistic model
We have observations of height and weight and want to use a logistic model to guess the sex.
# observed data df = pd.read_csv('HtWt.csv') df.head()
data { int N; // number of obs (pregnancies) int M; // number of groups (women) int K; // number of predictors int y[N]; // outcome row_vector[K] x[N]; // predictors int g[N]; // map obs to groups (pregnancies to women) } parameters { real alpha; real a[M]; vector[K] beta; real sigma; } model { sigma ~ uniform(0, 20); a ~ normal(0, sigma); b ~ normal(0,sigma); c ~ normal(0, sigma) for(n in 1:N) { y[n] ~ bernoulli(inv_logit( alpha + a[g[n]] + x[n]*beta)); } }'
log_reg_code = """ data { int<lower=0> n; int male[n]; real weight[n]; real height[n]; } transformed data {} parameters { real a; real b; real c; } transformed parameters {} model { a ~ normal(0, 10); b ~ normal(0, 10); c ~ normal(0, 10); for(i in 1:n) { male[i] ~ bernoulli(inv_logit(a*weight[i] + b*height[i] + c)); } } generated quantities {} """ log_reg_dat = { 'n': len(df), 'male': df.male, 'height': df.height, 'weight': df.weight } fit = pystan.stan(model_code=log_reg_code, data=log_reg_dat, iter=2000, chains=1)
print fit
df_trace = pd.DataFrame(fit.extract(['c', 'b', 'a'])) pd.scatter_matrix(df_trace[:], diagonal='kde');
Estimating parameters of a logistic model
Gelman’s book has an example where the dose of a drug may be affected to the number of rat deaths in an experiment.
Dose (log g/ml) | # Rats | # Deaths |
---|---|---|
-0.896 | 5 | 0 |
-0.296 | 5 | 1 |
-0.053 | 5 | 3 |
0.727 | 5 | 5 |
We will model the number of deaths as a random sample from a binomial distribution, where \(n\) is the number of rats and \(p\) the probabbility of a rat dying. We are given \(n = 5\), but we believve that \(p\) may be related to the drug dose \(x\). As \(x\) increases the number of rats dying seems to increase, and since \(p\) is a probability, we use the following model:
\[\begin{split}y \sim \text{Bin}(n, p) \\ \text{logit}(p) = \alpha + \beta x \\ \alpha \sim \mathcal{N}(0, 5) \\ \beta \sim \mathcal{N}(0, 10)\end{split}\]
where we set vague priors for \(\alpha\) and \(\beta\), the parameters for the logistic model.
Original PyMC3 code
n = 5 * np.ones(4) x = np.array([-0.896, -0.296, -0.053, 0.727]) y = np.array([0, 1, 3, 5]) def invlogit(x): return pm.exp(x) / (1 + pm.exp(x)) with pm.Model() as model: # define priors alpha = pm.Normal('alpha', mu=0, sd=5) beta = pm.Flat('beta') # define likelihood p = invlogit(alpha + beta*x) y_obs = pm.Binomial('y_obs', n=n, p=p, observed=y) # inference start = pm.find_MAP() step = pm.NUTS() trace = pm.sample(niter, step, start, random_seed=123, progressbar=True)
Exercise - convert to PyStan version
Using a hierarchcical model
This uses the Gelman radon data set and is based off this IPython notebook . Radon levels were measured in houses from all counties in several states. Here we want to know if the preence of a basement affects the level of radon, and if this is affected by which county the house is located in.
The data set provided is just for the state of Minnesota, which has 85 counties with 2 to 116 measurements per county. We only need 3 columns for this example county
, log_radon
, floor
, where floor=0
indicates that there is a basement.
We will perfrom simple linear regression on log_radon as a function of county and floor.
radon = pd.read_csv('radon.csv')[['county', 'floor', 'log_radon']] radon.head()
Hiearchical model
With a hierarchical model, there is an \(a_c\) and a \(b_c\) for each county \(c\) just as in the individual couty model, but they are no longer indepnedent but assumed to come from a common group distribution
\[\begin{split}a_c \sim \mathcal{N}(\mu_a, \sigma_a^2) \\ b_c \sim \mathcal{N}(\mu_b, \sigma_b^2)\end{split}\]
we furhter assume that the hyperparameters come from the following distributions
\[\begin{split}\mu_a \sim \mathcal{N}(0, 100^2) \\ \sigma_a \sim \mathcal{U}(0, 100) \\ \mu_b \sim \mathcal{N}(0, 100^2) \\ \sigma_b \sim \mathcal{U}(0, 100)\end{split}\]
Original PyMC3 code
county = pd.Categorical(radon['county']).codes with pm.Model() as hm: # County hyperpriors mu_a = pm.Normal('mu_a', mu=0, tau=1.0/100**2) sigma_a = pm.Uniform('sigma_a', lower=0, upper=100) mu_b = pm.Normal('mu_b', mu=0, tau=1.0/100**2) sigma_b = pm.Uniform('sigma_b', lower=0, upper=100) # County slopes and intercepts a = pm.Normal('slope', mu=mu_a, sd=sigma_a, shape=len(set(county))) b = pm.Normal('intercept', mu=mu_b, tau=1.0/sigma_b**2, shape=len(set(county))) # Houseehold errors sigma = pm.Gamma("sigma", alpha=10, beta=1) # Model prediction of radon level mu = a[county] + b[county] * radon.floor.values # Data likelihood y = pm.Normal('y', mu=mu, sd=sigma, observed=radon.log_radon)
Exercise - convert to PyStan version
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论