← Natural Language Processing vault · formula sheet · NLP1–NLP7 (mid-sem)

Natural Language Processing — formula sheet, learn these by heart.

natural-language-processing formula-sheet mid-sem probability

Every equation needed for mid-sem NLP revision, grouped by concept. Each card gives the display equation and defines the symbols immediately after it in plain words. Use this as the formula-first companion to the cheatsheet.

Symbol key

SymbolMeaning
\(w_i\)the word at position \(i\) in a sequence
\(t_i\)the tag at position \(i\), such as a POS tag
\(h\)a history or context, often previous words or tags
\(V\)the vocabulary; \(|V|\) is the number of word types
\(C(\cdot)\)a count from data
\(x,w,b,z,a\)input vector, weight vector, bias, pre-activation and activation

Similarity & weighting

Dot product
\[a\cdot b=\sum_{i=1}^{d}a_i b_i\]

\(a,b\) are vectors with \(d\) dimensions. \(a_i,b_i\) are values in dimension \(i\). A larger dot product means stronger shared weighted evidence, but it also grows with vector length.

Cosine similarity
\[\cos\theta=\frac{a\cdot b}{\|a\|\|b\|}=\frac{\sum_{i=1}^{d}a_i b_i}{\sqrt{\sum_{i=1}^{d}a_i^2}\sqrt{\sum_{i=1}^{d}b_i^2}}\]

\(\theta\) is the angle between vectors. \(\|a\|\) and \(\|b\|\) are vector lengths. Cosine compares direction, so it is useful when document length should not dominate similarity.

Raw and log term frequency
\[\mathrm{tf}_{\mathrm{raw}}(t,d)=C(t,d),\qquad \mathrm{tf}(t,d)=\begin{cases}1+\log C(t,d),&C(t,d)>0\\0,&C(t,d)=0\end{cases}\]

\(t\) is a term. \(d\) is a document. \(C(t,d)\) is the count of the term in the document. Log TF keeps repeated words useful without letting them grow linearly forever.

Document frequency and inverse document frequency
\[\mathrm{df}(t)=|\{d\in D:t\in d\}|,\qquad \mathrm{idf}(t)=\log\frac{N}{\mathrm{df}(t)}\]

\(D\) is the document collection. \(N=|D|\) is number of documents. \(\mathrm{df}(t)\) counts documents containing term \(t\). IDF is high for rare informative words and low for common words.

TF-IDF
\[\mathrm{tfidf}(t,d)=\mathrm{tf}(t,d)\times\mathrm{idf}(t)\]

\(\mathrm{tf}(t,d)\) measures importance inside one document. \(\mathrm{idf}(t)\) measures rarity across documents. The product rewards terms frequent here but not everywhere.

Joint, marginal and conditional estimates for co-occurrence
\[P(w,c)=\frac{C(w,c)}{N},\qquad P(w)=\frac{C(w)}{N},\qquad P(c)=\frac{C(c)}{N}\]

\(w\) is a target word. \(c\) is a context. \(C(w,c)\) is their co-occurrence count. \(N\) is the total co-occurrence count used for normalization.

PMI and PPMI
\[\mathrm{PMI}(w,c)=\log_2\frac{P(w,c)}{P(w)P(c)},\qquad \mathrm{PPMI}(w,c)=\max(\mathrm{PMI}(w,c),0)\]

\(P(w,c)\) is observed joint probability. \(P(w)P(c)\) is chance co-occurrence under independence. PMI is positive when association is stronger than chance; PPMI clips negative unreliable values to zero.

Word2Vec & embeddings

Skip-gram softmax
\[P(w_o\mid w_i)=\frac{\exp(v'_{w_o}\cdot v_{w_i})}{\sum_{w=1}^{|V|}\exp(v'_w\cdot v_{w_i})}\]

\(w_i\) is the input center word. \(w_o\) is an observed outside/context word. \(v_{w_i}\) is the input embedding. \(v'_w\) is an output embedding. \(|V|\) is vocabulary size. The denominator normalizes over all words.

Skip-gram objective over context window
\[J=\sum_{t=1}^{T}\sum_{-m\le j\le m,\ j\ne 0}\log P(w_{t+j}\mid w_t)\]

\(T\) is number of tokens. \(m\) is window radius. \(w_t\) is center word. \(w_{t+j}\) is a neighbor. The model maximizes log probability of true context words.

Negative sampling loss for one positive pair
\[\ell=-\log\sigma(v'_o\cdot v_i)-\sum_{k=1}^{K}\log\sigma(-v'_{n_k}\cdot v_i)\]

\(v_i\) is the center-word vector. \(v'_o\) is the true outside-word vector. \(n_k\) is a sampled noise word. \(K\) is number of negative samples. The first term rewards real pairs; the second punishes random pairs.

Sigmoid used in negative sampling
\[\sigma(x)=\frac{1}{1+e^{-x}}\]

\(x\) is a score such as a dot product. The sigmoid maps any real score to a number between zero and one, interpretable as binary probability.

CBOW context vector
\[\bar{v}=\frac{1}{2m}\sum_{-m\le j\le m,\ j\ne 0}v_{w_{t+j}},\qquad P(w_t\mid \mathrm{context})=\operatorname{softmax}(U\bar{v})_{w_t}\]

\(\bar{v}\) is the averaged context embedding. \(m\) is window radius. \(U\) is an output weight matrix. CBOW predicts the center word from surrounding words.

GloVe weighted least-squares objective
\[J=\sum_{i=1}^{|V|}\sum_{j=1}^{|V|}f(X_{ij})\left(w_i^T\tilde{w}_j+b_i+\tilde{b}_j-\log X_{ij}\right)^2\]

\(X_{ij}\) is word-context co-occurrence count. \(w_i\) and \(\tilde{w}_j\) are word and context vectors. \(b_i,\tilde{b}_j\) are biases. \(f\) weights counts so rare and extremely frequent pairs do not dominate.

Language models

Chain rule
\[P(w_1,w_2,\ldots,w_n)=\prod_{i=1}^{n}P(w_i\mid w_1,w_2,\ldots,w_{i-1})\]

\(w_1,\ldots,w_n\) are words in order. The equation is exact: each word is predicted from all previous words.

Bigram approximation
\[P(w_1^n)\approx\prod_{i=1}^{n}P(w_i\mid w_{i-1})\]

\(w_1^n\) means the whole sequence. Bigram models keep only the previous word as history, trading linguistic depth for count reliability.

N-gram approximation
\[P(w_i\mid w_1^{i-1})\approx P(w_i\mid w_{i-n+1}^{i-1})\]

\(n\) is n-gram order. \(w_{i-n+1}^{i-1}\) is the previous \(n-1\) words. The Markov assumption makes estimation possible.

Bigram MLE
\[P_{\mathrm{MLE}}(w_i\mid w_{i-1})=\frac{C(w_{i-1},w_i)}{C(w_{i-1})}\]

\(C(w_{i-1},w_i)\) counts the bigram. \(C(w_{i-1})\) counts the history word. MLE uses observed relative frequency.

N-gram MLE
\[P_{\mathrm{MLE}}(w_i\mid w_{i-n+1}^{i-1})=\frac{C(w_{i-n+1}^{i})}{C(w_{i-n+1}^{i-1})}\]

\(C(w_{i-n+1}^{i})\) counts the full n-gram ending at \(w_i\). \(C(w_{i-n+1}^{i-1})\) counts the context. Divide full event count by context count.

Log probability
\[\log P(w_1^n)=\sum_{i=1}^{n}\log P(w_i\mid h_i)\]

\(h_i\) is the chosen history for word \(w_i\). Logs turn products into sums and prevent numerical underflow.

Add-one smoothing
\[P_{\mathrm{add1}}(w\mid h)=\frac{C(h,w)+1}{C(h)+|V|}\]

\(h\) is history. \(w\) is a candidate next word. \(|V|\) is vocabulary size. Adding one gives nonzero probability to every word but can over-smooth.

Add-k smoothing
\[P_{\mathrm{add}k}(w\mid h)=\frac{C(h,w)+k}{C(h)+k|V|}\]

\(k\) is a small positive constant. Smaller \(k\) redistributes less probability mass than add-one smoothing.

Linear interpolation
\[P(w_i\mid w_{i-2},w_{i-1})=\lambda_3P_3(w_i\mid w_{i-2},w_{i-1})+\lambda_2P_2(w_i\mid w_{i-1})+\lambda_1P_1(w_i)\]

\(P_3,P_2,P_1\) are trigram, bigram and unigram models. \(\lambda_1,\lambda_2,\lambda_3\) are mixture weights with \(\lambda_1+\lambda_2+\lambda_3=1\). Every order contributes.

Kneser-Ney continuation intuition
\[P_{\mathrm{cont}}(w)=\frac{|\{h:C(h,w)>0\}|}{|\{(h,w'):C(h,w')>0\}|}\]

\(P_{\mathrm{cont}}(w)\) is based on how many distinct histories precede \(w\). It prefers words that complete many contexts, not only words with high raw frequency.

Perplexity
\[\mathrm{PP}(W)=P(w_1^N)^{-1/N}=\sqrt[N]{\frac{1}{\prod_{i=1}^{N}P(w_i\mid h_i)}}\]

\(W\) is the test sequence. \(N\) is number of predicted tokens. Lower perplexity means the model assigns higher average probability to the observed text.

Cross-entropy and perplexity
\[H(W)=-\frac{1}{N}\sum_{i=1}^{N}\log_2P(w_i\mid h_i),\qquad \mathrm{PP}(W)=2^{H(W)}\]

\(H(W)\) is average negative log probability in bits. Perplexity is two raised to that average uncertainty.

Neural equations

Weighted sum and activation
\[z=w\cdot x+b=\sum_{i=1}^{d}w_i x_i+b, \qquad a=f(z)\]

\(x\) is input vector. \(w\) is weight vector. \(b\) is bias. \(z\) is pre-activation. \(a\) is output activation. \(f\) is a non-linear activation function.

Sigmoid, tanh and ReLU
\[\sigma(z)=\frac{1}{1+e^{-z}},\qquad \tanh z=\frac{e^z-e^{-z}}{e^z+e^{-z}},\qquad \mathrm{ReLU}(z)=\max(0,z)\]

\(z\) is a scalar score. Sigmoid maps to zero-one, tanh maps to minus-one-one, and ReLU keeps positive values while zeroing negatives.

Layer computation
\[h^{(l)}=f\left(W^{(l)}h^{(l-1)}+b^{(l)}\right)\]

\(h^{(l)}\) is hidden representation at layer \(l\). \(W^{(l)}\) is weight matrix. \(b^{(l)}\) is bias vector. \(f\) is applied elementwise.

Softmax
\[P(y=k\mid x)=\frac{e^{s_k}}{\sum_{j=1}^{K}e^{s_j}}\]

\(s_k\) is score for class \(k\). \(K\) is number of classes. Softmax converts scores into probabilities that sum to one.

Cross-entropy loss
\[L=-\sum_{k=1}^{K}y_k\log \hat{y}_k\]

\(y_k\) is the true one-hot label value. \(\hat{y}_k\) is predicted probability. The loss is small when the true class gets high probability.

Gradient-descent update
\[\theta \leftarrow \theta-\eta\nabla_{\theta}L(\theta)\]

\(\theta\) is any trainable parameter. \(\eta\) is learning rate. \(\nabla_{\theta}L\) is the gradient of loss with respect to the parameter. Move opposite the gradient to reduce loss.

Sequence labeling

Local POS decision
\[\hat{t}=\arg\max_t P(w\mid t)P(t)\]

\(\hat{t}\) is the chosen tag. \(w\) is word. \(P(w\mid t)\) is emission likelihood. \(P(t)\) is tag prior. This is a local simplification, not full sequence decoding.

HMM joint probability
\[P(w_1^n,t_1^n)=\prod_{i=1}^{n}P(w_i\mid t_i)P(t_i\mid t_{i-1})\]

\(w_1^n\) is the word sequence. \(t_1^n\) is the tag sequence. \(P(w_i\mid t_i)\) is emission probability. \(P(t_i\mid t_{i-1})\) is transition probability.

Transition probability estimate
\[a_{ij}=P(t_j\mid t_i)=\frac{C(t_i,t_j)}{C(t_i)}\]

\(a_{ij}\) is probability of moving from tag \(t_i\) to tag \(t_j\). Counts are estimated from tagged data.

Emission probability estimate
\[b_j(o)=P(o\mid t_j)=\frac{C(t_j,o)}{C(t_j)}\]

\(b_j(o)\) is probability that tag \(t_j\) emits observation word \(o\). \(C(t_j,o)\) counts tag-word pairs.

Forward probability
\[\alpha_t(j)=P(o_1,\ldots,o_t,q_t=j)=b_j(o_t)\sum_i\alpha_{t-1}(i)a_{ij}\]

\(\alpha_t(j)\) is probability of observing first \(t\) words and ending in state \(j\). \(o_t\) is current observation. \(a_{ij}\) is transition. \(b_j(o_t)\) is emission.

Viterbi recurrence
\[v_t(j)=\max_i v_{t-1}(i)a_{ij}b_j(o_t)\]

\(v_t(j)\) is best path score ending in tag/state \(j\) at time \(t\). \(i\) ranges over previous states. Store the maximizing \(i\) as a backpointer.

MEMM conditional tag model
\[P(t_i\mid t_{i-1},x)=\frac{\exp\left(\sum_k\lambda_k f_k(t_i,t_{i-1},x,i)\right)}{\sum_{t'}\exp\left(\sum_k\lambda_k f_k(t',t_{i-1},x,i)\right)}\]

\(x\) is the observed sentence. \(f_k\) are feature functions. \(\lambda_k\) are learned weights. MEMMs use rich features but normalize locally.

Linear-chain CRF path score
\[s(x,y)=\sum_{i=1}^{n}\left(A_{y_{i-1},y_i}+P_{i,y_i}\right)\]

\(x\) is input sentence. \(y\) is a label path. \(A_{y_{i-1},y_i}\) is transition score between labels. \(P_{i,y_i}\) is emission score for label \(y_i\) at position \(i\).

CRF conditional probability
\[P(y\mid x)=\frac{e^{s(x,y)}}{\sum_{y'\in\mathcal{Y}(x)}e^{s(x,y')}}\]

\(\mathcal{Y}(x)\) is the set of possible label sequences for input \(x\). The denominator normalizes over complete paths, giving globally normalized sequence probabilities.

Memorize by families. Geometry formulas compare vectors; language-model formulas multiply or sum probabilities over sequences; neural formulas transform vectors and minimize loss; tagging formulas combine emissions, transitions and path search.
← Natural Language Processing vault
© cvam — written in plaintext, served warm