









































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!










































Artificial Intelligence (AI) is a technique which allows machines to act like humans by replicating their behaviour and nature.
- Machine Learning algorithms are designed in such a way that the machines tries to learn by itself without being explicitly programmed on each and every instruction. How does it work? The machine learn and make predictions based on its experience (data). **Statistics vs Machine Learning
- Machine learning is a brand of AI and consists in computational algorithms (performed by computers) that learn from data. -> data driven approach. Discover automatically meaningful patterns and trends in complex data mainly for predictive purposes. When do we need machine learning? - Complex tasks - Tasks that change over time
There are several variable types as:
- Quantitative : they take on numerical values (e.g. income, stock price…)
- Qualitative (categorical): they take on values in one of K different classes (e.g. increase/decrease of a stock..) - Independent variables : usually denoted by X. - Dependent variables : usually denoted by Y. And there are 2 types of problems: - Regression problems : the response variable is quantitative; - Classification problems : the response variable is qualitative or categorical; Supervised learning vs unsupervised learning Supervised Learning For each observation of the predictor measurements Xi there is an associated response measurement Yi {(x1, y1), (x2,y2), … , (xn, yn)} Where each Xi is a vector of length p and Yi is a scalar. We wish to fit a model that links the predictors to the response with the aim of predicting the response for unseen observations or better understanding the relationship between the response and the predictors. Methods: linear regression, logistic regression, GAM, regression trees… Unsupervised Learning For each observation of the predictor measurements Xi there is NO response measurement Yi. We lack a response variable that can supervise our analysis. We aim to find groups of similar items (clustering) or features (PCA-Principal Component Analysis). What is the objective of Statistical learning? Suppose we observe a quantitative response Y and p predictors. X = (X1, X2, … , Xp). We assume the following relationship: Y = f(X) + e Where: - F is a fixed but unknown function of X - E is a random error term, independent of X and with mean equal to zero and variance equal to sigma^2. (irreducible error) The goal is to predict Y with Y^ = f^(X) where f^ is the estimate of f. We want the prediction to be as accurate as possible, to minimise E(Y-Y^)^2. The accuracy of Y^ depends on the reducible and irreducible error. So, the goal is to estimate f to understand the relationship between X and Y. In order to estimate f, we apply the statistical learning method to the training data to estimate f. In particular, we find f^ such that Y~ f^(X).
Example 1: training and test MSE Is not guaranteed that the method with the lowest training MSE will also have the lowest test MSE! Usually, the test MSE is much larger than the train MSE. When flexibility (order of d) increases we observe a first decrease of the test MSE which at some point levels off and starts increasing (U-shape). When we observe for a given complexity a small training MSE and a large test MSE it may be a problem of OVERFITTING.
Expected test MSE Y = f(X) + e For each X0 there is typically a distribution of possible Y values (given by the fact that we have the error e). Goal: compute the expected test MSE for a given value X E(Y(x0) – Y^(x0))^2 = E(Y(xo) – f^(X0))^
Note that the expectation (E) is computed over all the possible training data sets. It averages the randomness in the training data. 2 simulated training data sets: linear model
- Red dots: different training data sets (used to estimate the linear model – grey line) - Blue dot denotes a generic point X0 for which we compute Y^ = f^(X0) using the estimated linear model 100 simulated training data sets: linear model
Which depends on the model flexibility. Decomposition of the expected test MSE The expected test MSE for a given X0 can be decomposed as follows: E(Y(Xo) – Y^(Xo))^2 = E(f(Xo) + e – f^(Xo))^2 = Var(f^(Xo) + Bias^2(f^(Xo)) + Var(e)
- Var(f^(Xo) represents the variability of f^ over different training set; - Bias^2(f^(Xo )) is the error of estimating f using f^ - Var(e) = sigma^2 is the irreducible error The expected test MSE: summary The expected test MSE for a given value Xo is given by: - The reducible components of MSE change according to the complexity/flexibility of the model: with more flexible methods, the variance will increase and the bias decrease. - The relative change of quantities determines the U-shape of the test MSE. - The overall expected test MSE is the mean computed over all Xo in the test set. Training error rate: is defined as the proportion of misclassified observations as Where Y^i=f^(Xi) is the predicted class; Test error rate is defined as the proportion of misclassified observations: We want to find the classifier that has the smallest test error rate. The test error rate is minimised on average by the Bayes Classifier which assigns each observation to the most likely class. The ideal classifier: Bayes classifier
The response variable has just 2 categories (j=0 or 1) and we have only 1 regressor (p=1). The grey function in the plot represents the TRUE (unknown) conditional probability function f defined as: Pr(Y=1 | X = x), the classification is not deterministic. The Bayes classifier assigns each observation to the most likely class given the predictor values: With 2 classes (0 and 1) this corresponds to the class J such that Pr(Y = j | X =x) > 0. So, 0.5 is the Bayes decision boundary. For a given x the error rate is: 1 – max(j) Pr(Y = j | X = x). The overall Bayes error rate is given by E(1- max(j) Pr(Y = j | X = x)). The Bayes error rate is the lowest possible error rate for any classifier of a random outcome and is analogous to the irreducible error.
In real case we do not know the conditional probability of Y given X and so it is not possible to apply the Bayes classifier which represents only a gold standard. Which is the predicted class Y^0? KNN classifier
- The points represent the training data set; the black and purple lines are the KNN and Bayes decision boundary. - The background grid represents how a class is assigned for a new test observation using the KNN method. - As K grows the method become less flexible (low variance but high-bias classifier). - 1/K is a measure of flexibility; - Train error rate (blue line) consistently declines as the flexibility increases and it is equal to 0 when K=1. - The test error rate (orange line) shows the characteristic U-shape (when it is too flexible – small K – we have overfitting). - The dashed horizontal line is the Bayes error rate (irreducible error). Curse of dimensionality When p is large it can be difficult to find a fairly large neighbourhood of observations close to any X0. This would result in a deterioration in the performance of the local KNN approach. The sampling density is proportional to n^1/p.
Resampling methods
- The training error can be always computed but it is usually an underestimate of the test error rate; - The test error can be easily calculated if a large designated set is available; - As an alternative the test error can be estimated using the available training data by means of resampling methods. Resampling methods: create different subsets of the training data to be used for fitting the same statistical method. Two of the most famous resampling methods are:
1- Cross-Validation : can be used to estimate the test error for model assessment or model selection; 2- Bootstrap : can be used to estimate the accuracy of a parameter estimate; The validation set approach
- The available set of observations is randomly divided into 2 parts: a training set and a validation or hold-out set. - The model is fit on the training set and the fitted model is used to predict the responses for the observations in the validation set. - The validation error provides an estimate of the test error. Drawbacks : - The validation estimate of the test error can be highly variable depending on which observations are included in the training set and which in the validation set. - Only a subset of the n available observations are used to fit/train the model. - This suggests that the validation error may tend to overestimate the test error for the model fit on the entire data set. The Leave-One-Out Cross-Validation (LOOCV) The LOOCV uses n validation sets of size 1. The LOOCV estimate of the test MSE is the average of the n validation errors: The LOOCV: pros and cons - The LOOCV approach tends not to overestimate (low bias) the test error as much as the validation set approach does. This is due to the fact that LOOCV uses almost the complete dataset for the training. - If we perform LOOCV multiple times we will get always the same results (there is no randomness). - Is computationally expensive to implement, as the model has to be fit n times. - LOOCV does not shake up the data enough (average of n fitted models can have high variance). K-Fold Cross-Validation
The estimate of sigma is known as residual standard error (RSE) and is given by: The RSE (sigma^) is an absolute measure of the lack of fit of the model. It represents the average amount that the response will deviate from the actual regression line. A relative measure of the goodness of fit is given by the R^2 statistics: Where TSS is the total variability of the response variable, and r is the correlation coefficient. Multiple linear regression Is a quantitative response Y with p predictor variables X1, … , Xp. Its model has this equation: Betaj represents the average effect on Y of a one unit increase Xj holding all the other predictors fixed. The unknown parameters are estimated using the least squares approach: Multiple linear regression: ANOVA
If any of the p-values for the individual variable is small, do we need to check the overall F- statistic? Yes, especially when p is large (but still lower than n). In the high dimensional setting when p>n we have more coefficients to estimate than observations to use for the estimation. This is similar to an underdetermined linear system of equations: Which has infinitely many solutions. This can be solved by imposing constraints on the parameters. Multiple linear regression: variable selection How to choose the best subset of regressors among p predictors? To decide which predictors are associated with the response, we need to try out a lot of models, each containing a different subset of the predictors, and compare them using model fit index. Given p predictors we have: models containing k predictors. With p predictors, there are in total 2^p possible models that contain subsets of p the available variables. As a solution it is possible to use stepwise methods which explore a restricted set of models sequentially:
- Forward selection: start with the null model and at each step add the variables that gives the greatest additional improvement to the fit. Continue until when a stopping rule is satisfied. - Backward selection: start with the full model and at each step remove the least useful predictor (the one with the highest p-value). Continue until when a stopping rule is satisfied. It can’t be used if p>n. - Mixed selection: combined forward and stepwise approach. Goodness of fit index : R^2 always increases when new variables are added to the model even if they are weakly associated with the response. AIC = deviance + penalty = c + nlog(RSS) + 2(1+p)
Where beta0 is the average credit card among African Americans, beta1(beta2) is the difference in the average balance between Asians and African Americans. So, African Americans is the baseline.
- Additive assumption: the affect of changes in a predictor XJ on the response is independent of the values of the other predictors. - interaction term (between quantitative variables) in the model equation: Problem #1: Non-linearity of the data The linear regression model assumes our linear relationship between the response and predictors. if a true relationship is far from linear all the conclusions can be trusted and the prediction accuracy can be significantly reduced. How to identify and only linearity? With plot of the residuals If you see a non-linear pattern it is possible to use a non-linear transformation of the predictors. Problem #2: Correlation of error terms An assumption of the linear regression model is that the error term e1, …, en are uncorrelated. If this assumption does not hold, standards errors are underestimated and confidence/prediction intervals are narrower than they should be. How to identifiy correlation? Plot the autocorrelation function (ACF) of the residuals. Problem #3: non-constant variance of error terms An assumption of the linear regression model is that Var(ei) = sigma^2 for each i. - heteroscedasticity can be identified in the residual plot. What to do? Transform the response variable using a concave function such as logY or radY.
Problem #4:High leverage points A high leverage point is a point for which the value of the predictor xi is unusual. Quite easy to identify high leverage points with simple regression. More difficult with multiple linear regression (central plot) The leverage (denoted by Hii) is given by the i-th element in the diagonal of the following HAT matrix: Where X is the regressor matrix. Problem #5: Outliers An outlier is a point for which Yi is far from the value predicted by the model. The corresponding residual ei = Yi – Yi^ is anomalous. The outlier has little effect on the least squares fit. But RSE is 1.09 when the outlier is included in the regression, and 0.77 when it is removed.
The model equation of the multiple linear regression is: The unknown parameters beta are estimated using the least squares approach, with the least squares solution that is: Assuming that the true model is linear (or approximately), we obtain that:
- Least squares regression is unbiased: E(f^(x0)) = f(x0) - The variance Var(f^(x0)) is nearly sigma^2e p/n (prediction accuracy) P&N Assume that the true model is linear. With N = 20 and p from 1 to 20. When p is nearly n the least square regression fit is too flexible and hence overfits the data. we need to find, for high-dimensional settings, a less flexible fitting approach that least squares: - Subset selection : identify a subset of p regressors that we think to be related to the response. - Shrinkage : we fit a model with all p regressors but the estimated coefficients are shrunken towards 0. - Dimension reduction : we project the p predictors into a M-dimensional subspace (M < p) through linear combination. Norms Given a vector beta = (beta1, … , beta p), - The l1 norm is calculated as the sum of the absolute values of the vector elements: And it measures the Manhattan distance of beta from zero.
- The l2 norm that is calculated as the square foot of the sum of the squared vector values: And it measures the Euclidean distance from beta to zero. Best subset selection The best model among M0, … Mp will be selected using cross-validated prediction error, AIC, BIC or adjusted R^2. Forward Stepwise Selection At each step the variable that gives the greater additional improvement to the fit is added to the model. The best model among M0, … , Mp will be selected using cross-validated prediction error, AIC, BIC or adjusted R^2. The total number of fitted models is 1+p(p+1)/2. When, p=20, we have 211 models. But, is not guaranteed to find with this selection the best possible model out of all 2^p models. It can be applied also in high-dimensional setting where n<p.