# Maximum likelihood: Bernoulli

I would really appreciate if anyone you can explain how it went from step 1 to the answer provided below. This is from the book Doing Data Science by Cathy O'Neil and Rachel Schutt pages 101 to 102.

I have provided my answers with the steps I took to get there.

$$p(x|c) = \Pi_j\theta_{jc}^{x_j}(1-\theta_{jc})^{(1-x_j)}$$

Take ln both sides:

$$ln(p(x|c)) = ln(\Pi_j\theta_{jc}^{x_j}(1-\theta_{jc})^{(1-x_j)})$$

Known:

$$\Pi_j \theta_{jc}^{x_j} = \theta_{jc}^{(\sum_{j} x)}$$

$$\Pi_j(1-\theta_{jc})^{(1-x_j)} = (1-\theta_{jc})^{n - \sum_j x}$$

Substitute the known into the equations:

$$ln(p(x|c)) = ln(\theta_{jc}^{(\sum_{j} x)} * (1-\theta_{jc})^{n - \sum_j x})$$

Expand the muliplication:

$$ln(p(x|c)) = ln(\theta_{jc}^{(\sum_{j} x)}) + ln((1-\theta_{jc})^{n - \sum_j x})$$

Simply:

$$ln(p(x|c)) = (\sum_{j} x)ln(\theta_{jc}) + ({n - \sum_j x})ln(1-\theta_{jc})$$

$$ln(p(x|c)) = (\sum_{j} x)ln(\theta_{jc}) - (\sum_j x)ln(1-\theta_{jc}) + (n)ln(1-\theta_{jc})$$

$$ln(p(x|c)) = (\sum_{j} x)ln(\frac{\theta_{jc}}{1-\theta_{jc}}) + (n)ln(1-\theta_{jc})$$

$$ln(p(x|c)) = (\sum_j x_j) ln(\frac{\theta_j}{1-\theta_j}) + \sum_j log(1-\theta_j)$$

I don't have access to the book, but I think you've some typos in its answer. The parentheses should cover both $$x_j$$ and the $$\ln$$ term because outside we have no $$j$$ index. Also, we have $$\theta_{jc}$$, not $$\theta_j$$.
If we start from taking log of both sides, \begin{align}p(x|c)&=\sum_{j}\ln(\theta_{jc}^{x_j}(1-\theta_{jc})^{1-x_j})\\&=\sum_jx_j(\ln\theta_{jc}+(1-x_j)\ln(1-\theta_{jc})) \\&=\sum_j x_j\ln \left(\frac{\theta_{jc}}{1-\theta_{jc}}\right)-\sum_j \ln(1-\theta_{jc}) \end{align}
Your mistakes start from the "Known" section, e.g. $$\prod_j\theta_{jc}^{x_j}\neq\theta_{jc}^{\sum_j x_j}$$ because $$\theta_{jc}$$ is dependent on $$j$$.