Docsity

Applied Statistics Notes, Appunti di Statistica

Anteprima parziale del testo

Scarica Applied Statistics Notes e più Appunti in PDF di Statistica solo su Docsity!

Applied Statistic

Notes

Author: Mattia Longobardo

Professor: Piercesare Secchi

Accademic Year: 2023/

List of Figures I

  • 1 Linear Models
    • 1.1 Homogeneous Linear Model
      • 1.1.1 Introduction
      • 1.1.2 Structured (or Parametric) Models:
      • 1.1.3 Model Errors Typologies
      • 1.1.4 Classical Linear Models
      • 1.1.5 Parameters Estimations
      • 1.1.6 Model Diagnostic
      • 1.1.7 Inference
      • 1.1.8 Model Selection
    • 1.2 Heterogeneous Linear Model
      • 1.2.1 Model Specification
      • 1.2.2 Known Variance Weights
      • 1.2.3 Variance Function
      • 1.2.4 Group 1 Known Weights λ(·) = λ( v )
      • 1.2.5 Group 2 λ(·) = λ(δ)
      • 1.2.6 Group 3 λ(·) = λ(μ)
      • 1.2.7 Group 4 λ(·) = λ(δ, μ)
    • 1.3 Linear Model with Correlated Errors
      • 1.3.1 Correlation Structure
      • 1.3.2 Heterogeneous variance and correlation
    • 1.4 Linear Mixed Model
  • 2 Spatial Statistics
    • 2.1 Introduction and GeoStat Data
      • 2.1.1 Lattice Data
      • 2.1.2 Contiguity
      • 2.1.3 Spatial Autocorrelation
    • 2.2 Variogram
      • 2.2.1 Geostat Data
      • 2.2.2 Intrinsic Stationarity
      • 2.2.3 Semi-Variogram
      • 2.2.4 Parametric Models for Semi-Variogram
      • 2.2.5 Estimating the Semi-Variogram
    • 2.3 Kriging Problem
      • 2.3.1 Prediction in Spatial Statistics - Kriging
      • 2.3.2 Simple Kriging - Known Mean ms 0 for Zs 0 :
      • 2.3.3 Ordinary Kriging - Same but Unknown Mean for Zs
        • Changes) 2.3.4 Universal Kriging - Known Covariates (Useful in Non-Stationary Processes, Mean
      • 2.3.5 Kriging Problem - Drift Estimation
      • 2.3.6 Summary
  • 3 Markov’s Chain
    • 3.1 Introduction
    • 3.2 Long Run Behaviour
      • 3.2.1 Definitions:
    • 3.3 Markov Decision Process
      • 3.3.1 Maximize Utility
    • 3.4 Hidden Markov’s Model
  • 4 Kalman Filter
    • 4.1 State Space Models and Kalman Filters
    • 4.2 Examples
  • 5 Multiple Choice Question
    • January
    • February
    • June
  • 6 Exams
    • January
    • February
    • June
    • July
    • September
    • January
    • February
    • June
    • July

1.1. HOMOGENEOUS LINEAR MODEL CHAPTER 1. LINEAR MODELS

L

Y

g(x)

(y − E[y|x])

E[y|x] − g(x))^2

Figure 1.1: Error in Euclidean Distance

Names:

  • X ∈ (X 1 ,... , Xp) features, predictors, regressors, inputs.
  • Y ∈ R response, target, output.
  • Data: n realizations of X and Y on n units [not necessarily independent].
  • ϵ error, residual (what is left over after given the best explanation of Y in terms of X).
  • use data to find best f : fitting the model to the data.
  • fθˆ fitted model or fˆ (what you know after fitting the model).

1.1.2 Structured (or Parametric) Models:

Example: y = β 0 + β 1 X 1 + · · · + βpXp + ϵ = fθ(x) + ϵ θ = (β 0 , β 1 ,... , βp) ∈ Rp+^

So, taking in consideration Equation 1.1.2, we can proceed with fitting the model. To fit the model we need to find:

β^ ˆ = argminβ∈Rp+

∑^ n

i=

(yi − β 0 − β 1 Xi 1 − · · · − βpXip)^2 (1.2)

So we are looking for the parameters β that minimize the error.

1.1.3 Model Errors Typologies

Until now we have just created our model, now we can asses it real capabilities. We created our model with the idea of reducing the error of the training data , but we still not consider how will act in the real world. So to do that take in consideration this:

  • Model: y = f (x) + ϵ.
  • New Data: y 0 , x 0.

So after had fit the model on the trading data we can proceed with:

CHAPTER 1. LINEAR MODELS 1.1. HOMOGENEOUS LINEAR MODEL

  1. Prediction: So we predict with our model the result of x 0 that we will call yˆ 0
  2. Explain the role of features x 1 ,... , xp in generating variability of y :

=⇒ Evaluate : E|x 0 [(y 0 − yˆ 0 )^2 ] = (f (x 0 ) − fˆ (x 0 ))^2 + Var(ϵ 0 )

  • The error can be reduced if we reduce the difference between f (x 0 ) and fˆ (x 0 ).
  • Remember : Model y 0 = f (x 0 ) + ϵ 0
  • ϵ 0 cannot be captured, always > 0 If there is variability, it cannot be eliminated.

So now we can compare the training error (Equation 1.3a) with the generalization error(Equation 1.3b):

Training Error =

n

∑^ n

i=

(yi − fˆ (xi))^2 (1.3a)

General Error = E|data[(y 0 − fˆ (x 0 ))^2 ] = E[(y 0 − fˆ (x 0 ))^2 ] (1.3b)

Complexity of the Model

Error

Training Error Generalization Error

Figure 1.2: Training Error vs. Generalization Error

Training error represents how well the model fits the training data and tends to decrease as the model becomes more complex because the model is optimizing its performance specifically for the training data. Generalization error measures how well the model performs on new, unseen data and is typically higher than training error because the model is being evaluated on data it hasn’t seen before, which may have different characteristics.In summary, Generalization error is a more reliable indicator of a model’s performance in practice because it assesses how well the model can generalize its learned patterns to new situations.

Proposition: E[(y 0 − fˆ (x 0 ))^2 ] = (f (x 0 ) − E[ fˆ (x 0 )])^2 + Var( fˆ (x 0 )) + Var(ϵ 0 )

Where the bias is (f (x 0 ) − E[ fˆ (x 0 )])^2 , variance is Var( fˆ (x 0 )) and the error is the Var(ϵ 0 ). Now in order to reduce the error we need to reduce the variance but doing that we probably will become bias, this means that our generalization error will increase. So we need to find a point of balance between bias and variance this problem is call Bias-Variance trade-off.

A high bias means the model is making simplistic assumptions about the underlying data and may

CHAPTER 1. LINEAR MODELS 1.1. HOMOGENEOUS LINEAR MODEL

that errors have the same variance means that is homogeneous across all observation. But there’s also a more compact way to write this model as:

y = X β + ϵ (1.5)

Where X is know as n × p Design Matrix:

X =

x 11 x 12... x 1 p xn 1 xn 2... xnp

= (x 1 , x 2 ,... , xp)

and

X ⊤^ =

x⊤ 1 .. . x⊤ n

x 11 x 12... x 1 n x 21 x 22... x 2 n .. .

xp 1 xp 2... xpn

For this chapter we will assumed X FULL RANK p, meaning that all the columns are linearly indepen- dent.

Linearly Independent means that none of the variables can be explained by another variable or a combination of more variables. So we don’t have Multicollinearity.

We can also introduce a known parameter Xi 0 for all i:

y = X 0 + Xβ + ϵ

yi : Xi 0 + Xi 1 β 1 + · · · + Xipβp + ϵi

This new feature is call offset (absorbed in the design matrix, becomes just column + mean) when Xi 0 is assumed to be known. Corresponding parameter β 0 assumed to be =1 =⇒

y = X 0 · 1 + Xβ + ϵ

We can also to remove the offset Xi 0 by defining a new target variable as y˜i = yi − Xi 0 :

y ˜i = xi 1 β 1 + · · · + xipβp + ϵi with y˜i as the dependent variable

But in this case β 0 is going to be β 1 , and we have to recover (X′) by setting Xi 1 = 1 for all i. In this way we are centring the model.

y → y − ¯y xi → xi − ¯xi β 0 = ¯y − β 1 x¯ 1 − · · · − βp x¯p

1.1.5 Parameters Estimations

OLS

There are different methods to estimate the parameters the first we are going to talk about is the Ordinary Least Square or OLS, where the objective is the minimization of the residual sum of squares with respect to β(OLS is less suitable for LMM.). Find β that minimizes the distance between what I observed (y) and what I would observe if there were no errors (Xβ):

β^ ˆOLS = arg min β ∥yi − x⊤ i β∥^2 = arg min β

∑n

i=

(yi − x⊤ i β)^2 (1.6)

1.1. HOMOGENEOUS LINEAR MODEL CHAPTER 1. LINEAR MODELS

where x⊤ i β is the i-th row of vector xi, i.e., x⊤ i β = β 1 xi 1 + · · · + βpxip Column Perspective:

Xβ = β 1 x(1)^ + · · · + βpx(p)

X =

x(1),... , x(p)

where x(j)^ =

x 1 j x 2 j .. . xnj

, j = 1,... , p

Find among all the possible combinations of the columns of the design matrix the one that is closest to the target. L(X) = span

x(1),... , x(p)

=⇒ linear space created by matrix X.If y ∈ L(X), you would be able to express y as a linear combination of the regressors with no errors. In this case, y ∈/ L(X): find among all vectors in L(X), the one closest to y in terms of Euclidean distance.

Πy ⊥ L(X) ∈ L(X) Πy ⊥ L(X) = ˆy = βˆ 1 x(1)^ + · · · + βˆpx(p)^ = X βˆ

β^ ˆ is the vector of coefficients of Πy ⊥ L(X).

y = X βˆ + ˆϵ

y = ˆy + ˆϵ where yˆ is the fitted values and ˆϵ is the residuals. Assumptions:

  • Columns are iid
  • x(1),... , x(p)^ are linearly independent

dim(L(X)) = p dim(L⊥(X)) = n − p (less degrees of freedom)

This means that in the context of estimating parameters in a statistical model, each additional variable or parameter being estimated reduces the available degrees of freedom. For example, in simple linear regression with one predictor variable, there are n-2 degrees of freedom, where n is the number of ob- servations and 2 represents the intercept and slope being estimated. Adding more predictor variables further reduces the degrees of freedom available for estimating each parameter. Calculation The formula β = (X′X)−^1 X′y represents the calculation of coefficients in a linear regression model. Let’s break down the elements of this formula:

  • β: This is a vector of coefficients in the linear regression model. For each predictor variable in your model, there is a corresponding β coefficient that represents the strength and direction of its effect on the target variable.
  • X: The design matrix, as discussed earlier, contains the predictor variables. X is typically an n × p matrix, where n is the number of observations, and p is the number of predictors.
  • X′: The transpose of the design matrix X. It is a p × n matrix.
  • y: The target variable (or dependent variable) you are trying to predict in your linear regression model. It is a vector of length n (number of observations).
  • (X′X)−^1 : This is the matrix inverse of the product of the transpose of the design matrix X and X. (X′X) is a p × p matrix, and (X′X)−^1 is its inverse. It’s used to estimate the coefficients.

Now, let’s explain the steps involved in this formula:

1.1. HOMOGENEOUS LINEAR MODEL CHAPTER 1. LINEAR MODELS

MLE for β, σ^2 :

( βˆM LE , σˆ^2 ) = arg max β,σ^2

L(β, σ^2 |y)

Usually, maximize log L (log L = log likelihood):

( βˆM LE , σˆ^2 ) = arg max β,σ^2

L(β, σ^2 |y)

Proposition: Since X is full rank

β^ ˆM LE = (X′X)−^1 X′y ⇒ E[ βˆM LE ] = β (= βˆOLS )

ˆσ^2 M LE =

n

i

ˆε^2 i ⇒ E[ˆσ^2 M LE ] = n − p n

σ^2

ˆσ^2 M LE ’s bias is relevant for small n → use σˆ^2 RM LE

RMLE

RMLE stand for restricted maximum likelihood and is use to obtain unbiased estimate of σ^2 Notice: y ∼ Nn(Xβ, σ^2 I)

βˆ = (X′X)−^1 X′y ∼ Np^ (β, σ^2 (X′X)−^1 )^ if collinearity, determinant = 0 → do not trust ε ˆ = ( I − H )y ∼ Nn(0, σ^2 ( I − H ))

Recall Linear transformation If W ∼ Nn(μ, Σ) and A q × n matrix what is the distribution of AW? AW ∼ Nq(Aμ, AΣA′)

Find ˆσ^2 RM LE = arg maxσ 2 L(σ^2 |εˆ) = (^) n^1 −p

∑n i=1 εˆ 2 i ⇒^ unbiased estimator of^ σ 2 Inference: With RMLE we can also estimate Var( βˆ) = Cov( βˆ) Notice Var( βˆ) = σ^2 (X′X)−^1 where σ^2 depends on estimation model used (OLS, RMLE, etc. → where σˆ OLS^2 = ˆσ^2 RM LE ) Estimate βˆRMLE of β = βˆMLE true for LM (independent observations with homogeneous variance). β^ ˆOLS and σˆ OLS^2 = βˆRMLE and ˆσ^2 RMLE true for LM (independent, homoscedastic residuals).

Var( βˆ) = σ^2 (X′X)−^1 estimated by Varˆ ( βˆ) = ˆσ^2 (X′X)−^1 where σ^2 = ˆσ^2 OLS = σˆ MLE^2 = σˆ^2 RMLE. Notice: Var-Cov matrices:

  • OLS = RMLE (unbiased).
  • MLE ̸= RMLE < σˆ^2 MLE ̸= ˆσ RMLE^2.

1.1.6 Model Diagnostic

Before making any inference, check model assumptions. For a homogeneous linear model the key assumptions are:

  1. Residual errors, εi, are independent and homoscedastic.

CHAPTER 1. LINEAR MODELS 1.1. HOMOGENEOUS LINEAR MODEL

  1. Effect of covariates can be written as a linear function of their values and the corresponding parameters.
  2. Normality Assumption important for ML estimation, but not for OLS.

The are different tools to check this assumptions.

  • Residual Analysis
  • Influential Cases (same logic as F-statistic: distance between observed and expected). Check whether model fit is sensitive to inclusion or exclusion of some observations.

Residuals

Residual Diagnostics:

  1. Raw residuals εˆi against fitted values μˆi → look for random patterns and constant variability + outliers.
  2. Scatterplot of ˆεi against values of the covariates → look for random patterns.
  3. Q-Q plot of ˆεi against corresponding values for standard normal distribution.

Raw Residuals: ε ˆi = yi − μˆi, where μˆi = x′ i βˆ (fitted value) Scaled Residuals: Raw εˆi divided by their estimated ˆσ (true σ rarely known).

  • Internally studentized residuals: Estimate of σ based on all observations (ˆσ).
  • Externally studentized residuals: i-th observation is excluded σˆ(−i).(These allow outliers to stand out.)

But residual scaling does not address the variances of residuals εˆi differ even if variances of true errors εi are equal. This is connected to the Hat Matrix :

H = X(X′X)−^1 X′

n × n matrix H: projection that maps vector y on the subspace spanned by columns of X. Vector of predicted values: y ˆ = Hy, εˆ = y − ˆy = (In − H)y

where In is the n × n identity matrix.If H is not proportional to In, raw residuals are potentially het- eroscedastic and/or correlated.

Var(ˆε) = σ^2 (In − H) (issue of heteroscedasticity)

Scaling addresses the issue of heteroscedasticity but introduces the issue of correlation between scaled residuals , which is addressed with Error Recovery Methods.

Error Recovery

Methods to remove both heteroscedasticity and correlation of raw residuals εˆi, by transforming resid- uals so that they have μ = 0, constant variance, and are uncorrelated. n × n matrix P = In − H is not of full rank: if n > p, then rank of P ≤ n − p. Thus, we may have at most n − p transformed, uncorrelated residuals. Residuals of error recovery may represent more than one observation. → Difficult!

CHAPTER 1. LINEAR MODELS 1.1. HOMOGENEOUS LINEAR MODEL

1.1.7 Inference

Linear Testing

Testing: H 0 : Lβ = C 0 where L is a q × p matrix known. H 1 : Lβ ̸= C 0 C 0 = [ 0 · · · 0 ] Examples: Ex. 1)

L =

[

]

(1 in position k)

Lβ = βk ⇒ H 0 : βk = 0 H 1 : βk ̸= 0

Ex. 2)

L =

[

]

Lβ =

[

β 1 β 2

]

⇒ H 0 : β 1 = 0 and β 2 = 0

H 1 : β 1 ̸= 0 or β 2 ̸= 0

Ex. 3)

L =

[

]

Lβ =

[

β 1 3 β 3 + β 4

]

⇒ H 0 : β 1 = 1 and 3 β 3 + β 4 = 25

H 1 : β 1 ̸= 1 or 3 β 3 + β 4 ̸= 25

WithC 0 =

[

]

F-Test Statistics to Compare Variances

Null Hypothesis (H 0 ): L βˆ = C 0 Alternative Hypothesis (H 1 ): L βˆ ̸= C 0

F =

(L βˆ − C 0 )⊤(LVar( βˆ)L⊤)−^1 (L βˆ − C 0 ) q

Where q is the number of combinations.

If H 0 is true, F ∼ F(q,n−p)

Reject H 0 if F > Fα,q,n−p

L βˆ estimated by L βˆ : L βˆ ∼ N (Lβ, σ^2 L(X⊤X)−^1 L⊤) Remember: Var( βˆ) = σ^2 (X⊤X)−^1

1.1. HOMOGENEOUS LINEAR MODEL CHAPTER 1. LINEAR MODELS

Reject H 0

Figure 1.4: F-distribution with 10 numerator and 10 denominator degrees of freedom.

Wald Test

L q × p matrix Inference: LP

F : ( βˆ′L′Var( βˆ)−^1 L βˆ) ∼ F (q, n − p) Degrees of Freedom (DoF) of LP: q (the number of rows of L matrix). For testing: H 0 : Lβ = C 0 (known matrix or vector) vs. H 1 : Lβ ̸= C 0 Example: H 0 : βi = 0 vs. H 1 : βi ̸= 0, i ∈ { 1... p}

L =

[

]

only 1 row, q = 1 (all 0 except 1 in position i).

F =

( βˆi − C 0 )^2 Var( βˆi)

if H 0 : βi = C 0 , F = ( βˆi−C 0 )^2 Var( βˆi) Reject if: ( βˆi − C 0 )^2 Var( βˆi)

F 1 −α(q, n − p), α ∈ (0, 1)

⇒ equivalent to: | βˆi − C 0 | √ Var( βˆi)

t 1 − α 2 ,(n−p)

Remember: βˆi − C 0 √ Var( βˆi)

∼ t(n − p)

Reject H 0 if: | βˆi − C 0 | > t 1 − α 2 ,(n−p)

Var( βˆi)

Thus:

β^ ˆi ± t 1 − α 2 ,(n−p)

Var( βˆi) = C 1 −α/2( βˆi)

Note: The distribution is independent from the parameter you want to build your model on.

1.2. HETEROGENEOUS LINEAR MODEL CHAPTER 1. LINEAR MODELS

BIC - Bayesian Information Criterion

The BIC also taking into account the number of observations penalizes more The models with many observations, which however at the same time are The models where you tend to have higher accuracy. So if I take two models with the same variables and but the second one with twice as many observations as the other one will have very close AICs with the second one having a lower AIC tends to be,but I had computational waste to account for the additional observations. So maybe I gain 1% in accuracy to lose 100% in computational efficiency which the BIC takes into account in part.

BIC( β,ˆ ˆσ^2 ) = (log n) · p − 2 ℓ( β,ˆ σˆ^2 )

Penalize (log n) · p − 2 ℓ(β, σ^2 ). If n is large enough, logn > 2 (as in AIC) then the penalty term of the BIC (logn ∗ p > 2 p) will be higher than the penalty term of AIC (2p). Quantitative methods BUT remember some variables are relevant.

ANOVA

Sequential Approach: Add complexity to the model, starting from a simple model with just the intercept and possibly to stop early (interim results). Tests: Test H 0 H 1 x 4 1 1 + x 4 x 2 1 + x 4 1 + x 4 + x 2 x 3 1 + x 4 + x 2 1 + x 4 + x 2 + x 3 Marginal Approach (e.g., F-test): Data are collected before the testing and tests are applied to all data. Tests: Test H 0 H 1 x 4 1 + x 2 + x 3 1 + x 4 + x 2 + x 3 x 2 1 + x 4 + x 3 1 + x 4 + x 2 + x 3 x 3 1 + x 4 + x 2 1 + x 4 + x 2 + x 3

1.2 Heterogeneous Linear Model

Now we relax the homoscedasticity assumption and allow for the observations to be heteroscedastic, i.e., to have different variances, while retaining the assumption that the observations are independent and normally distributed.We refer to this new class of models as LMs with heterogeneous variance.

1.2.1 Model Specification

Starting from the classical LM with homogeneous variance the variance Var(yi) of observation yi of the dependent variable is equal to σ^2. We now relax the constant variance assumption and assume that

Var(yi) = σ i^2. (1.11)

Therefore, we formulate an LM with heterogeneous variance (each observation can have different variance) by assuming that

yi = x(1) i β 1 + · · · + x( i p)βp + ϵi = x′ iβ + ϵi (1.12)

and

ϵi ∼ N (0, σ i^2 ), (1.13)

CHAPTER 1. LINEAR MODELS 1.2. HETEROGENEOUS LINEAR MODEL

where ϵi are independent, i.e., ϵi is independent of ϵi′ for i ̸= i′. The fixed part of the LM is exactly the same as that of the classical LM with homogeneous variance. Similarly to the model with heterogeneous variance implies that

E(yi) ≡ μi = x′ iβ. (1.14)

It is important to note that the model contains in total n + p parameters, including n parameters σi and p parameters β. This is more than n, the number of observations. Therefore, the model is not identifiable. It may become identifiable, however, if we impose additional constraints on the residual variances σ^21 ,... , σ n^2. One simple way to impose such constraints is to assume known variance weights. We can now rewrite the model equations with a more compact form that will count also the new vari- ance structure. Toward this end, we define a diagonal matrix R :

R ≡ ΛΛ, (1.15)

where Λ = diag(λ 1 ,... , λn) is a diagonal matrix. We can specify the model as follows:

y = X β + ϵ, (1.16)

where ϵ ∼ N (0, R), R = σ^2 R , (1.17)

1.2.2 Known Variance Weights

The simplest way to introduce heteroscedasticity and, at the same time, to reduce the number of vari- ance parameters in model is to assume that the variance of ϵi is equal to a known proportion of one (unknown) parameter σ^2. More specifically, we may associate with every observation i a known con- stant wi > 0 and assume that Var(ϵi) = Var(yi) = σ^2 /wi.

ϵi ∼ N (0, σ^2 /wi). (1.18)

Constants wi are called true weights. The higher the weight for a given observation, the lower the variance, i.e., the more precisely recorded the value of yi.

1.2.3 Variance Function

A more general and flexible way to introduce variance heterogeneity is by means of a variance func- tion.

λ(δ, μ, v ), (1.19)

which assumes positive values and is continuous and differentiable with respect to δ for all legitimate values of δ. Note that μ is a scalar and δ and v can be vectors. We can then assume that the variance of the residual errors, i.e., Var(ϵi), is expressed as follows:

Var(ϵi) = σ^2 λ^2 (δ, μi; v i), (1.20)

where:

  • μi is defined as μi = x′ iβ.
  • σ is a scale parameter
  • v i is a vector of (known) covariates defining the variance function for observation i