## Biased Monte-Carlo estimator

## Table of Contents

## Introduction

In statistic the bias of an estimator is the difference between the true value of a certain quantity of interest and the calculated expected value from the data. A bias may occur either if one missunderstoods the true nature of the quantity or uses a worng mathematical calculation. In this notebook you will see an example for the second case.

## Requirements

### Knowledge

To complete this exercise notebook you should possess knowledge about the following topics:

- Monte Carlo estimator
- Jensen's inequality, see notebook 'exercise-jensen-inequality'

## Python Modules

```
import numpy as np
import scipy.stats
from matplotlib import pyplot as plt
%matplotlib inline
```

## Exercise

Assume the data generating distribution$ p(x) $, which is a mixture of two Gaussians$ \mathcal N(10,0.6) $ and$ \mathcal N(15,1.4) $ .

```
# data generating distribution is a mixture of two Gaussians with parameters
loc1=10
loc2=15
scale1=0.6
scale2=1.4
# samples from p(x)
def samples(n=10000, p=0.3, loc1=loc1, loc2=loc2, scale1=scale1, scale2=scale2):
z = np.random.binomial(n,p=p)
x1 = np.random.normal(loc1,scale1,z)
x2 = np.random.normal(loc2,scale2,n-z)
x = np.concatenate((x1,x2))
np.random.shuffle(x)
return x
x = samples()
_ = plt.hist(x, bins=int(np.sqrt(len(x))))
```

### Task 1

Assume you need an approximation for$ K = \mathbb E_{p(x)}[\log(x)] $.

What's wrong with the following Monte Carlo estimator

$ \hat K = \log \left [\frac{1}{M}\sum_{i=1}^M x_i \right] $ with$ x_i \sim p(x) $ ?

Do you expect that$ K = \hat K $ ?

#### Hint

Read about Jensen's inequality, and argue for the specific case of$ \log $.

### Task 2

Give the formula for an unbiased estimator$ \tilde K $ of$ K $ !

Implement both estimators and compare their results for the mixture of Gaussian's samples.

#### Hint

For better results sample 100 samples and take the average of the calculated means.

## Licenses

### Notebook License (CC-BY-SA 4.0)

*The following license applies to the complete notebook, including code cells. It does however not apply to any referenced external media (e.g., images).*

exercise-biased-monte-carlo-estimator

by Christian Herta

is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Based on a work at https://gitlab.com/deep.TEACHING.

### Code License (MIT)

*The following license only applies to code cells of the notebook.*

Copyright 2018 Christian Herta

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.