Language as a Stochastic Process 1 Basic Statistics for...

5

Click here to load reader

Transcript of Language as a Stochastic Process 1 Basic Statistics for...

Page 1: Language as a Stochastic Process 1 Basic Statistics for NLPpages.cs.wisc.edu/~jerryzhu/cs769/langstat.pdf · Pick an arbitrary letter x at random from any English ... Language as

CS769 Spring 2010 Advanced Natural Language Processing

Language as a Stochastic Process

Lecturer: Xiaojin Zhu [email protected]

1 Basic Statistics for NLP

Pick an arbitrary letter x at random from any English text ever written. What is x?

Random variable X: the random outcome of an experiment.

Probability distribution P (X = x) = θx, for x ∈ {1, . . . , 27} if our alphabet consists of {a, b, c, . . . , z,t}.Often use the probability vector θ = (θ1, . . . , θ27)>.

Basic properties: 0 ≤ θx ≤ 1,∑

x θx = 1.

Useful analogy of θ: a k-sided die. Here k = 27. If k = 2, a coin. A fair die/coin is when θ1 = . . . =θk = 1/k, otherwise it is biased.

Sampling a letter x from the distribution θ (x ∼ θ):

1. create intervals of lengths θ1, . . . , θk.

2. generate a uniform random number r ∈ [0, 1] (most programming languages have a random() functionfor this).

3. find the interval r falls into, output the interval index.

The Multinomial distribution for k-sided die with probability vector θ, N throws, outcome countsn1, . . . , nk:

P (n1, . . . , nk|θ) =(

Nn1 . . . nk

) k∏i=1

θnii . (1)

Going back to language, if we ignore letter order, we can model the letters as draws from a multinomialdistribution. The MLE of the parameter is the frequency of letters in the English language (not computable).

A corpus (pl. corpora) is a large, representative collection of text. See the Linguistic Data Consortium(LDC) for some examples.

The likelihood function is P (Data|θ). Here Data=(n1, . . . , nk), and the likelihood takes the formof (1). Likelihood is a function of θ, and in general is not normalized:

∫P (Data|θ)dθ 6= 1.

Conditional probability P (X = x|H = h). Given the latest letter is h, what is the next letter x?How do we estimate conditional probabilities? Given a corpus with multiple sentences:

i am a studenti like this class...Let’s add a special symbol “start-of-sentence” 〈s〉 to each sentence:〈s〉 i am a student

1

Page 2: Language as a Stochastic Process 1 Basic Statistics for NLPpages.cs.wisc.edu/~jerryzhu/cs769/langstat.pdf · Pick an arbitrary letter x at random from any English ... Language as

Language as a Stochastic Process 2

〈s〉 i like this class〈s〉...and break each sentence into pairs of letters. Note we don’t cross sentence boundaries, e.g., no (t 〈s〉):(〈s〉 i) (i t) (t a) (a m) . . . (n t)(〈s〉 i) (i t) (t l) (l i) . . . (s s)...Now our random variables x, h take value in {〈s〉, a, . . . , z,t}. Let chx be the count of the pair (hx) in thecorpus. We want to estimate the parameters θ = {θhx = P (x|h)}. Note this θ is much bigger! We canarrange it in a transition matrix with rows for h and columns for x.

Intuition: P (x|h) = chx/∑

x′ chx′ . It turns out to be the MLE of θ. Since a sentence always starts with〈s〉, the likelihood function P (Data|θ) is

P (i|〈s〉)P (t|i) . . . P (t|n)P (i|〈s〉) . . . (2)= θ〈s〉iθit . . . (3)

=∏h,x

θchx

hx , (4)

with the constraints 0 ≤ θhx ≤ 1,∑

x θhx = 1,∀h. One can solve the MLE with Lagrange multiplier as before.

The joint probability P (x, h) = P (h, x) is the (much rarer event) that the two events both happening.The MLE is P (x, h) = chx/

∑h,x chx.

The marginal probability is obtained by summing over some random variables. For example, P (h) =∑x P (h, x). Relation:

P (x, h) = P (x|h)P (h). (5)

Now, h and x do not have to be the same type of random variables. Consider this: randomly pick aword, and randomly pick a letter from the word. Let h be the length (number of letters) of the word, and xbe the identity of the letter. It is perfectly fine to ask for P (x = a|h = 2). What if you are told the letter is‘a’, and you have to guess the length of the word?

The Bayes rule allows you to flip the conditional probability around:

P (x|h) =P (x, h)P (h)

=P (h|x)P (x)∑x′ P (h|x′)P (x′)

=P (h|x)P (x)

P (h). (6)

2 Parameter Estimation (Statistics) = Learning a Model (Ma-chine Learning)

Now given N draws from an unknown θ, and we observe the count histogram n1, . . . , nk, can we estimateθ? In addition, how do we predict the next draw?

Example 1 If in N = 10 coin flips, we observe n1 = 4 heads, and n2 = 6 tails, what is θ?

Intuition says θ = (0.4, 0.6). But in fact pretty much any θ could have generated the observed counts. Howdo we pick one? Should we pick one?

Parameter estimation and future prediction are two central problems of statistics, and machine learning.

2.1 The MLE Estimate

The Maximum Likelihood Estimate (MLE) is

θMLE = argmaxθP (Data|θ). (7)

Page 3: Language as a Stochastic Process 1 Basic Statistics for NLPpages.cs.wisc.edu/~jerryzhu/cs769/langstat.pdf · Pick an arbitrary letter x at random from any English ... Language as

Language as a Stochastic Process 3

Deriving the MLE of a multinomial (V is the same as k, c is the same as m):

θML = arg maxθ∈V−simplex

P (c1:V |θ) (8)

= arg maxθ

V∏w=1

θcww multinomial definition (9)

= arg maxθ

V∑w=1

cw log θw log() monotonic (10)

We are faced with the constrained optimization problem of finding θ1:V :

maxθ1:V

∑Vw=1 cw log θw (11)

subject to∑V

w=1 θw = 1. (12)

The general procedure to solve equality constrained optimization problems is the following: We introducea scalar β called a Lagrange multiplier (one for each constraint), rewrite the equality constraint as E(x) = 0,and define a new Lagrangian function of the form G(x, β) = F (x) − βE(x), where F (x) is the originalobjective. Solve for the unconstrained optimization problem on G.

In our case, the Lagrangian is

V∑w=1

cw log θw − β

(V∑

w=1

θw − 1

)(13)

After verifying that this is a concave function, we set the gradient (w.r.t. θ1:V and β) to zero:

∂θw=

cw

θw− β = 0 (14)

∂β=

V∑w=1

θw − 1 = 0, (15)

which givesθML

w =cw∑V

w=1 cw

=cw

|C|, (16)

where |C| =∑V

w=1 cw is the length of the corpus. It can be seen that the purpose of β is normalization.Therefore, in this case the MLE is simply the frequency estimate!

2.2 The MAP Estimate

The Maximum A Posterior (MAP) is

θMAP = argmaxθP (θ|Data) = argmaxθP (θ)P (Data|θ) (17)

We need a prior distribution P (θ), which is usually taken to be the Dirichlet distribution since it is conjugateto multinomial:

P (θ) = Dir(θ|α1, . . . , αk) =Γ(∑k

i=1 αi)∏ki=1 Γ(αi)

k∏i=1

θαi−1i (18)

The posterior is again a Dirichlet

P (θ|Data) = Dir(θ|α1 + n1, . . . , αk + nk) =Γ(∑k

i=1 αi + ni)∏ki=1 Γ(αi + ni)

k∏i=1

θαi+ni−1i (19)

Page 4: Language as a Stochastic Process 1 Basic Statistics for NLPpages.cs.wisc.edu/~jerryzhu/cs769/langstat.pdf · Pick an arbitrary letter x at random from any English ... Language as

Language as a Stochastic Process 4

The mode of the posterior (i.e., the MAP estimate) is

θi =ni + αi − 1

N +∑k

j=1 αj − k(20)

2.3 The Bayesian Approach

Keep the posterior distribution P (θ|Data).

3 Predictive Distribution (Statistics) = Inference (Machine Learn-ing)

For MLE and MAP, use the point estimate of θ to compute the multinomial.For Bayesian, integrate out θ w.r.t. the posterior. This gives a multivariate Polya distribution, also

known as the Dirichlet compound multinomial distribution. Let βi = αi + ni.

P (m1 . . .mk|β) =∫

θ

P (m1 . . .mk|θ)p(θ|β)dθ (21)

=m!∏

i(mi!)Γ(∑

i βi)Γ(m +

∑i βi)

∏i

Γ(mi + βi)Γ(βi)

(22)

where m =∑

i mi.

4 Bag of Word, tf.idf, and Cosine Similarity

A document d can be represented by a word count vector. The length of the vector is the vocabulary size.The ith element is the number of tokens for the ith word type. This is known as the Bag of word (BOW)representation, as it ignores word order. A variant is a vector of binary indicators for the presence of eachword type.

A document d can also be represented by a tf · idf vector. tf is the normalize term frequency, i.e. wordcount vector scaled, so that the most frequent word type in this document has a value of 1:

tfw =c(w, d)

maxv c(v, d),

where c(w, d) is the count of word w in d. idf is inverse document frequency. Let N be the number ofdocuments in the collection. Let nw be the number of documents in which word type w appears.

idfw = logN

nw.

idf is an attempt to handle stopwords or near-stopwords: if a word (like “the”) appears in most documents,it is probably not very interesting. Since nw ≈ N , its idf will be close to 0. Finally

tf · idfw = tfw × idfw.

Given two documents d, q in feature vector representation, one way to define how similar they are is thecosine similarity: q and d form an angle θ in the feature space, and

sim(d, q) = cos(θ) (23)

=d>q

‖d‖ · ‖q‖(24)

=d>q√

d>d√

q>q, (25)

Page 5: Language as a Stochastic Process 1 Basic Statistics for NLPpages.cs.wisc.edu/~jerryzhu/cs769/langstat.pdf · Pick an arbitrary letter x at random from any English ... Language as

Language as a Stochastic Process 5

where the dot product

u>v =V∑

i=1

uivi.