- 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
Norms and Distance of Vectors
Recall that the ‘norm’ of a vector \(v\), denoted \(||v||\) is simply its length. For a vector with components
\[v = \left(v_1,...,v_n\right)\]
the norm of \(v\) is given by:
\[||v|| = \sqrt{v_1^2+...+v_n^2}\]
The distance between two vectors is the length of their difference:
\[d(v,w) = ||v-w||\]
import numpy as np from scipy import linalg # norm of a vector v = np.array([1,2]) linalg.norm(v)
2.2361
# distance between two vectors w = np.array([1,1]) linalg.norm(v-w)
1.0000
Inner Products
Inner products are closely related to norms and distance. The (standard) inner product of two \(n\) dimensional vectors \(v\) and \(w\) is given by:
\[\begin{split}<v,w> = v_1w_1+...+v_nw_n\end{split}\]
I.e. the inner product is just the sum of the product of the components. Certain ‘special’ matrices also define inner products, and we will see some of those later.
Any inner product determines a norm via:
\[\begin{split}||v|| = <v,v>^{\frac12}\end{split}\]
There is a more abstract formulation of an inner product, that is useful when considering more general vector spaces, especially function vector spaces:
An inner product on a vector space \(V\) is a symmetric, positive definite, bilinear form.
There is also a more abstract definition of a norm - a norm is function from a vector space to the real numbers, that is positive definite, absolutely scalable and satisfies the triangle inequality.
What is important here is not to memorize these definitions - just to realize that ‘norm’ and ‘inner product’ can be defined for things that are not tuples in \(\mathbb{R}^n\). (In particular, they can be defined on vector spaces of functions).
Example
v.dot(w)
3
Outer Products
Note that the inner product is just matrix multiplication of a \(1\times n\) vector with an \(n\times 1\) vector. In fact, we may write:
\[\begin{split}<v,w> = v^tw\end{split}\]
The outer product of two vectors is just the opposite. It is given by:
\[v\otimes w = vw^t\]
Note that I am considering \(v\) and \(w\) as column vectors. The result of the inner product is a scalar. The result of the outer product is a matrix.
Example
np.outer(v,w)
array([[1, 1], [2, 2]])
Extended example : the covariance matrix is an outer proudct.
import numpy as np # We have n observations of p variables n, p = 10, 4 v = np.random.random((p,n))
# The covariance matrix is a p by p matrix np.cov(v)
array([[ 0.1055, -0.0437, 0.0352, -0.0152], [-0.0437, 0.055 , -0.0126, 0.0324], [ 0.0352, -0.0126, 0.1016, 0.0552], [-0.0152, 0.0324, 0.0552, 0.1224]])
# From the definition, the covariance matrix # is just the outer product of the normalized # matrix where every variable has zero mean # divided by the number of degrees of freedom w = v - v.mean(1)[:, np.newaxis] w.dot(w.T)/(n - 1)
array([[ 0.1055, -0.0437, 0.0352, -0.0152], [-0.0437, 0.055 , -0.0126, 0.0324], [ 0.0352, -0.0126, 0.1016, 0.0552], [-0.0152, 0.0324, 0.0552, 0.1224]])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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