- 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
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.
# define invlogit function def invlogit(x): return pymc.exp(x) / (1 + pymc.exp(x))
# observed data n = 5 * np.ones(4) x = np.array([-0.896, -0.296, -0.053, 0.727]) y_obs = np.array([0, 1, 3, 5]) # define priors alpha = pymc.Normal('alpha', mu=0, tau=1.0/5**2) beta = pymc.Normal('beta', mu=0, tau=1.0/10**2) # define likelihood p = pymc.InvLogit('p', alpha + beta*x) y = pymc.Binomial('y_obs', n=n, p=p, value=y_obs, observed=True) # inference m = pymc.Model([alpha, beta, y]) mc = pymc.MCMC(m) mc.sample(iter=11000, burn=10000)
[-----------------100%-----------------] 11000 of 11000 complete in 6.9 sec
beta.stats()
{'95% HPD interval': array([ 3.1131, 23.0992]), 'mc error': 0.2998, 'mean': 12.1401, 'n': 1000, 'quantiles': {2.5000: 3.5785, 25: 7.5365, 50: 11.3823, 75: 15.9492, 97.5000: 25.4258}, 'standard deviation': 5.8260}
xp = np.linspace(-1, 1, 100) a = alpha.stats()['mean'] b = beta.stats()['mean'] plt.plot(xp, invlogit(a + b*xp).value) plt.scatter(x, y_obs/5, s=50); plt.xlabel('Log does of drug') plt.ylabel('Risk of death');
pymc.Matplot.plot(mc)
Plotting alpha Plotting beta
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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