Showing posts with label Lecture Notes. Show all posts
Showing posts with label Lecture Notes. Show all posts

Saturday, 23 November 2019

Machine Learning: Lecture Notes (Week 9)

Gaussian Distribution (Normal Distribution) is used for anomaly detection. I'm quite familiar with this so I won't discuss this in my blog.

The Parameter Distribution Problem
Given a data set, estimate which distribution gave this data set using the Maximum Likelihood method:

  • Estimate that the average (mu) to be the same as the average.
  • Estimate that the variance (sigma) to be the same as the standard error squared.

Anomaly Detection Algorithm
Given a training set of m samples, each with n features, where the samples follow the normal distribution, p(x) can be calculated as:

  • Choose features that might indicate anomalous examples.
  • Fit parameters for the different features such as mean values and variance.
  • Calculate p(x). If p(x) is smaller than a threshold, an anomaly is detected.


Put simply, the likelihood for one example is calculated. If that likelihood is too small, we probably have an anomaly.

This is the last lecture note for this course, since I've completed the course. The next blog post will discuss a simple logistic regression example for apartment prices.

Saturday, 16 November 2019

Machine Learning: Lecture Notes (Week 8)

Unsupervised Learning
Unsupervised learning is training on a training set X without labels y.

Clustering algorithms finds clusters of similar features.

K-means
K-means is the most popular clustering algorithm.

It takes two random points as cluster centroids.
  • Ignore the bias feature.
  • Repeat:
    • For each sample in the training set, it assigns it to the closest centroid.
    • Each centroid is moved to the average location of its assigned training samples.


Optimization Objective
K-means can get stuck in a local optimum. Use random initialization several time to overcome this.

Elbow method for selecting number of clusters.

Create a curve of the cost function versus the number of clusters. The "elbow" will indicate the suitable number of clusters.

It can be necessary to reduce the number of features. Sometimes, features can be seen as redundant.

This can be done by projecting data from a set of higher dimensions to a plane of lower dimensions.

Principal Component Analysis
PCA tries to reduce the error between the data points and its projections.

PCA should not be confused with liner regression

Start with mean normalization (subtracting average from each value). The scale of features must be on a comparable scale.

Calculate coviarance matrix SIGMA = 1/m x^i {x^i}^T
Compute Eigenvectors U, S, V of SIGMA using singular value decomposition (SVD)

This gives U, a matrix of n column vectors. The first k column vectors are the new planes for the PCA.

U_reduce = U(1:k,:)

z=U_reduce^T x

Restoring:
X_approx = U_reduce x



Anomaly Detection - when to use
Use AD when there are few positive examples
Many different and unpredictable types of anomalies.
What features to use?
Use histograms to see whether it is a gaussian feature or not. If it is gaussian, it is suitable for anomaly detection. If it isn't, transform it to a gaussian distribution.

Recommender Systems
One approach can be to use a version of a regression analysis.

Collaborative Filtering
In this case, we don't have any information about the movies, such as romance/action etc. The users has specified which movies they like (theta values).

Alternate optimizing for indata and model parameters to get lower errors.

1. Initialize x and theta to smal random values.
2. Minimize J(x, theta) using gradient descent

Saturday, 9 November 2019

Machine Learning: Lecture Notes (Week 7)

Support Vector Machine (SVM)
The cost function will be modified compared to the sigmod function. It will consist of a constant slope session and a constant session.

In SVM, the parameters CA+B shall be minimized. A is the cost term, B is the regularization term and C controls the weight between them.

When training the hypothesis function, the hypothesis (theta multiplied by input data) shall be

  • Cost1: Bigger than 1, if y = 1
  • Cost0: Smaller than -1, if y = 1

Support Vector Machine
Define f as a similarity function that calculates the proximity to landmarks (combinations of feature values):
The function is based on a Gaussian kernel. Sigma affects the pointyness of the curve - a small sigma means a pointier curve.

Big C (small lambda): Low bias, high variance. Optimizing training set.
Small C (big lambda): High bias, low variance. Optimizing to reduce overfitting.

Saturday, 28 September 2019

Machine Learning: Lecture Notes (1)

Here are some lecture notes from the second week of the online Stanford course in Machine Learning. I take the notes for myself as a form of rubberducking.

Terminology from week 1 (single feature linear regression):

  • m is the number of training examples. 
  • n is the number of features.   
  • y is the output from the training set 
  • x are the input features in the training set
  • x(i) are the input features of the ith training set. 
  • hθ =>θ0+θ1x1 + ... + θnxn is the hypothesis function that will be modified to fit the training set.
  • θj are the hypothesis values for each feature.
  • J is the cost function. A common cost function is the mean square error function
  • α is the learning rate


Multiple features:
It is common to add a constant feature to the model: θ0, where x0 = 1
Using matrices, hθ (x) = θTx
Gradient Descent Algorithm
For each θj: 
    Add the derivative of the cost function for that feature
    Add α times the average of the (hypothesis minus the actual observation) multiplied by that feature value.
Gradient Descent - Feature Scaling
The gradient descent algorithm can converge quicker if the feature range are scaled to the same interval.
Feature scaling is dividing the input variables by the range of the input variables. The new range will be 1, but the values can still be higher.

Mean normalization means subtracting the mean from each value. If you combine those methods, you get:
xi := (xi - mean(x))/range(x)

Normal Equation
This is a way to minimize J without iterations.
Gradient Descent or Normal Equation
GD: Need to chose a learning rate. NE: No need to choose a learning rate.
GD: Needs many iterations. NE: Needs no iterations
GD: O(kn^2). NE: O(n^3). NE is heavy for large feature sets!


The next step is a programming assignment where I'll implement linear regression to a simple data set. I won't publish that on my blog, however.

Saturday, 19 April 2014

Lecture Notes: Financial Markets (Swedish notes)

Here are the lecture notes from my second course in National Economics: Financial Markets.

The notes are unfortunately in Swedish (I had no thoughts about blogging initially) and are meant as an inspiration. I recommend any student to write his/her own lecture notes him/her self.

The PDF file can be found at:
https://dl.dropboxusercontent.com/u/3587964/FinansHansBystrom.pdf

As before, I can guarantee that there are logical and factual mistakes, missing pieces and other flaws. However, I passed the exam with a good grade.