Chapter 7 - Ulysses’ Compass

The chapter began with the problem of overfitting, a universal phenomenon by which models with more parameters fit a sample better, even when the additional parameters are meaningless. Two common tools were introduced to address overfitting: regularizing priors and estimates of out-of-sample accuracy (WAIC and PSIS). Regularizing priors reduce overfitting during estimation, and WAIC and PSIS help estimate the degree of overfitting. Practical functions compare in the rethinking package were introduced to help analyze collections of models fit to the same data. If you are after causal estimates, then these tools will mislead you. So models must be designed through some other method, not selected on the basis of out-of-sample predictive accuracy. But any causal estimate will still overfit the sample. So you always have to worry about overfitting, measuring it with WAIC/PSIS and reducing it with regularization.

Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them. Problems are labeled Easy (E), Medium (M), and Hard(H).

Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points.

Questions

7E1. State the three motivating criteria that define information entropy. Try to express each in your own words.

#1) should be measured with a continuous scale which the spacing between adjacent values is consistent
#2) capture the size of the possibility space is the value scales with the number of possible outcomes
#3) be flexible to add independent events such that it does not matter how the events are divided

7E2. Suppose a coin is weighted such that, when it is tossed and lands on a table, it comes up heads 70% of the time. What is the entropy of this coin?

p <- c(0.7, 1 - 0.7)
(H <- -sum(p * log(p)))
## [1] 0.6108643
#The entropy of this coin is 0.61.

7E3. Suppose a four-sided die is loaded such that, when tossed onto a table, it shows “1” 20%, “2” 25%, “3” 25%, and “4” 30% of the time. What is the entropy of this die?

p <- c(0.20,0.25,0.25,0.30)
entropy <- -sum(p * log(p))
entropy
## [1] 1.376227

7E4. Suppose another four-sided die is loaded such that it never shows “4”. The other three sides show equally often. What is the entropy of this die?

p <- c(0.333,0.3333,0.3333,0.00001)
entropy <- -sum(p*log(p))
entropy
## [1] 1.098688

7M1. Write down and compare the definitions of AIC and WAIC. Which of these criteria is most general? Which assumptions are required to transform the more general criterion into a less general one?

#AIC = Dtrain + 2p . Dtrain is training deviance. p is the number of parameters estimated by

#WAIC = −2(lppd−pWAIC)=−2(∑Ni=1logPr(yi)−∑Ni=1V(yi)) Pr(yi) is the average likelihood of observation i in the training sample.

#WAIC is the most general to move from that to AIC we must first go to DIC by assuming posterior distribution is multivariate gaussian. From DIC to AIC we must assume that priors are flat or overwhelmed by the likelihood.

7M2. Explain the difference between model selection and model comparison. What information is lost under model selection?

# Selection - choose one best model for prediction. We lose information about differences among models. 
#   For example, if m1 and m2 were very close to each other compared on a performance metric but different in structure. 
#   Then selecting only one of them discard part of the evidence.
# Averaging - include uncertainty about 'correctness' of the models into predictions. 
#   Technically speaking, information criteria is used as a weight of certainty of the model and predictions of different models 
#   are mixed using the weights. 
#   Averaging discard information about model differences.

7M3. When comparing models with an information criterion, why must all models be fit to exactly the same observations? What would happen to the information criterion values, if the models were fit to different numbers of observations? Perform some experiments, if you are not sure.

# All information criteria use sum over all samples as part of the calculation(is an additive measure of an error)
# Different number of examples (or different examples in the sample) will cause differences in the value of the criteria that influenced. Models fitted on a smaller sample will have better results (value of the criteria is lower).

7M4. What happens to the effective number of parameters, as measured by PSIS or WAIC, as a prior becomes more concentrated? Why? Perform some experiments, if you are not sure.

# In short, effective number of parameters should reduce, as models become less 'flexible' and more rigid. Regularizing priors constrain a model's flexibility. Since the effective num of parameters measures how flexible the model is, as a prior becomes more concentrated, it redueces the effective num of parameters.

7M5. Provide an informal explanation of why informative priors reduce overfitting.

# Informative priors reduce overfitting because more data is required to shift params from their initial most probable values. It means that if a pattern is not frequent in data, it will be hard to move params from the initial state, as data has a small influence.  
# Another possible explanation - regularised(more concentrated) priors are equal to adding a big set of initial data concentrated around parameters with priors.
# Excerpt from the book:
#   One way to prevent a model from getting too excited by the training sample is to give it a skeptical prior. By “skeptical,” I mean a prior that slows the rate of learning from the sample. The most common skeptical prior is a regularizing prior, which is applied to a beta-coefficient, a “slope” in a linear model. Such a prior, when tuned properly, reduces overfitting while still allowing the model to learn the regular features of a sample. If the prior is too skeptical, however, then regular features will be missed, resulting in underfitting. So the problem is really one of tuning. But as you’ll see, even mild skepticism can help a model do better, and doing better is all we can really hope for in the large world, where no model nor prior is optimal

7M6. Provide an informal explanation of why overly informative priors result in underfitting.

# Overly informative priors result in underfitting because effective number of parameters reduces and model have less freedom to fit the data. If regularisation is too strong fitting requires more and more data, and model underfits on the small samples.
# In other words, a model becomes overconfident in parameters values and it's hard to move it from that point. It requires more evidence(data) for even tiny changes of parameters.