F# - Ordinary Least Squares (OLS)

So we're finally ready to run a regression.

Let's first make up some data.

-----------------------------------------------------------

#light

open System
open System.Collections.Generic
open System.IO
open Xunit
open MathMod
open Microsoft.FSharp.Math


//---------------------------------------------------------
//Regression
//---------------------------------------------------------

let ni = 10
let k = 4
let x0 = Array2D.zeroCreate<float> ni k
let y = Array2D.zeroCreate<float> ni 1

y.[0,0] <- 290.68
y.[1,0] <- 236.44
y.[2,0] <- 397.78
y.[3,0] <- 209.13
y.[4,0] <- 266.98
y.[5,0] <- 359.83
y.[6,0] <- 56.08
y.[7,0] <- 299.26
y.[8,0] <- 254.52
y.[9,0] <- 496.51

x0.[0,0] <- 1.0
x0.[1,0] <- 1.0
x0.[2,0] <- 1.0
x0.[3,0] <- 1.0
x0.[4,0] <- 1.0
x0.[5,0] <- 1.0
x0.[6,0] <- 1.0
x0.[7,0] <- 1.0
x0.[8,0] <- 1.0
x0.[9,0] <- 1.0

x0.[0,1] <- 22.0
x0.[1,1] <- 4.0
x0.[2,1] <- 13.0
x0.[3,1] <- 3.0
x0.[4,1] <- 9.0
x0.[5,1] <- -13.0
x0.[6,1] <- -12.0
x0.[7,1] <-  7.0
x0.[8,1] <- 19.0
x0.[9,1] <- 21.0

x0.[0,2] <- 12.0
x0.[1,2] <- 14.0
x0.[2,2] <- 19.0
x0.[3,2] <- 9.0
x0.[4,2] <- 13.0
x0.[5,2] <- 23.0
x0.[6,2] <- 7.0
x0.[7,2] <- 8.0
x0.[8,2] <- 3.0
x0.[9,2] <- 15.0

x0.[0,3] <- 5.0
x0.[1,3] <- 7.0
x0.[2,3] <- 9.0
x0.[3,3] <- 11.0
x0.[4,3] <- 7.0
x0.[5,3] <- 17.0
x0.[6,3] <- 2.0
x0.[7,3] <- 14.0
x0.[8,3] <- 5.0
x0.[9,3] <- 22.0

I have created a small data set of 10 observations and 4 coefficients.

In scaler notation, these observations represent the following relationship:

Yi = a + B1Xi1 + B2Xi2 + B3Xi3 + B4Xi4 + ei

In matrix notation, this equation is represented as:

y = XB + e

where Y is an n x 1 vector, X is n x k matrix, B is a k x 1 vector, and e is an n x 1 vector.

The OLS problem is to solve for (estimate) B-hat using the following equation:

B-hat = (X'X)-1X'y

This is the standard estimator for the OLS found in every econometrics text book.

Solving this equation is simple given all the F# functions we have written up until now.

First off, I want to refine my transpose and matrix multiplication functions so that I don't have to pass in an empty array.  These are below.  They just do the work that I was already doing in code, and make life a little bit easier.

//
//NEWLY UPDATED 8/12/2009
//
let rec transposeACol(x : float [,] ) (xp : float [,] ) (i : int) (j : int) =
    match j with
    | -1 -> xp
    | _ -> xp.[j, i] <- x.[i, j]
           //printfn "i: %i j: %i x: %10.5f" i j (if i >=0 && j >=0 then x.[i, j] else -1.0)
           //let userresp = Console.ReadLine()
           let xp = transposeACol x xp i (j-1)
           xp
          
let rec transpose (x : float [,] ) (xp : float [,] ) (i : int) (j : int) =
    match i with
    | -1 -> xp
    | _  -> let xp = transposeACol x xp i j 
            let xp = transpose x xp (i-1) j
            xp

let rec transpose2 (x : float [,] ) =
    let n = x.GetUpperBound 0
    let k = x.GetUpperBound 1
    let xp = Array2D.zeroCreate<float> (k+1) (n+1)
    transpose x xp n k

let rec matMult2 (xp : float [,] ) (x : float [,] ) =
    let n = xp.GetUpperBound 0
    let k = xp.GetUpperBound 1
    let m = x.GetUpperBound 1
    let xpx = Array2D.zeroCreate<float> (n+1) (m+1)
    matMult xp x xpx

Next my main program takes my data array x0 and creates its transpose in xp.  I then multiply xp and x0 to create xpx.  This result is a square k x k matrix that I want to invert.

let xp = transpose2 x0
let xpx = matMult2 xp x0

I then assign xpx to X for no good reason other than to simplify my subsequent code.  I then pass this matrix to my matInverse function, returning XI as the inverse

let X = xpx
let XI, ret = matInverse X

Below is my matInverse function that was derived in my prior post:

----------------------------------------------

let matInverse (X : float [,]) =

    let ret, L, U = liDecomp X

    let k = X.GetUpperBound 0

    let zeroX = Array2D.zeroCreate<float> (k+1) (k+1)
    let LU = matMult L U zeroX

    //--------------------------------
    //Now Invert L
    //--------------------------------
    let LI, ret_LI = invertL L

    //-----------------------------------------
    //Now Invert U
    //First Transpose U - UP is U Transpose
    //-----------------------------------------
    let Uzero = Array2D.zeroCreate<float> (k+1) (k+1)
    let UP = transpose U Uzero k k

    //-----------------------------------------
    //Now Invert UP
    //Return UPI as Inverse of UP
    //-----------------------------------------
    let UPI, ret_UI = invertL UP

    //-----------------------------------------
    //Now Transpose UPI
    //UI is now U inverse
    //-----------------------------------------
    let UI = transpose UPI Uzero k k

    //-----------------------------------------
    //Multiply UI and LI to calculate XI
    //XI is now X inverse
    //-----------------------------------------
    let XI = Array2D.zeroCreate<float> (k+1) (k+1)
    let XI = matMult UI LI XI

    //-----------------------------------------
    //return the inverse
    //-----------------------------------------
    XI, ret

----------------------------------------------

So now calculating B-hat is simply one line of code:

    let b = matMult2 (matMult2 XI xp) y

    for i = 0 to k do
         printfn "Beta Hat: %i %10.5f" i b.[i,0]

The stuff in the parentheses:  (matMult2 XI xp) is the product  (X'X)-1X.  This result is then multiplied by y, yielding the vector of B-Hat coefficient estimates.

Piece of cake.

Now, let's work on the Standard Errors.

    //---------------------------------------
    //  Calc Std Errors
    //---------------------------------------

    // Calc Residual Vector = Y - Yhat
    // First Calc Y Hat = X BetaHat
   
    let YHat = matMult2 x0 b
   
    let e = Array2D.zeroCreate<float> ni 1
    for i = 0 to (ni-1) do
        e.[i,0] <- YHat.[i,0] - y.[i,0]
       
    for i = 0 to (ni-1) do
         printfn "Y, Hat, e: %i %10.5f %10.5f %10.5f " i y.[i,0] YHat.[i,0] e.[i,0]

    //--------------------------------------------------
    //Calc SSE -> Sum of Squared Errors e'e
    //--------------------------------------------------
    let ssV = matMult2 (transpose2 e) e
   
    let sse = ssV.[0,0]
    printfn "SSE: %10.2f" sse
   
    let sigma2 = sse / float (ni - (k+1))
    printfn "SE: %10.2f" sigma2
    
    //--------------------------------------------------
    //Calc Variance/Covariance Matrix of B Hat
    //--------------------------------------------------
    //---------------------------------------
    // Calc T-Stats
    //---------------------------------------
   
    let bVar = Array2D.zeroCreate<float> (k+1) 1
    let bSE = Array2D.zeroCreate<float> (k+1) 1
    let TStat = Array2D.zeroCreate<float> (k+1) 1

    for i = 0 to k do
        bVar.[i,0] <- sigma2 * XI.[i,i]
        bSE.[i,0] <- Math.Sqrt bVar.[i,0]
        TStat.[i,0] <- b.[i,0] / bSE.[i,0]
        printfn "B, Var, SE, TStat: %10.5f %10.5f %10.5f %10.5f" b.[i,0] bVar.[i,0] bSE.[i,0] TStat.[i,0]
I first calculate the estimated values for Y: YHat.  YHat equals is simply the product of X & b:  YHat = Xb.

----------------------------------------------

 

The vector of residuals is just YHat - Y.  The vector e stores the residuals.

The product e'e is the sum of squared errors.  The result of this product is a 1 x 1 matrix.  I assign this value to the variable sse.

The variance of the regression (sigma2) is the sum of squared errors divided by n - k.

Now to calculate the standard errors for each of the regression coefficients, we first need to calculate the variance of each estimate:

Var(BHat) = sigma2*(X'X)-1.  That is, the variance of BHat is just the variance of the regression multiplied by (X'X)-1.  The standard error for BHat (bSE) is just the square root of the variance.

And, of course, a coefficient's T-Statistic (TStat) is just the ratio of the coefficient to it's standard error.

See my next post for the calculation of the R2 and a printout of the results.

 

 

 

 

 

Print | posted @ Thursday, August 06, 2009 9:28 AM

Comments on this entry:

Gravatar # Your content is really useful
by dress at 7/15/2010 2:23 AM

Prom dresses
wedding dresses
on best wedding dresses for
discount Prom dresses
discount wedding dresses
a series of discount Wedding Dresses, Including Wedding gowns, Evening gowns, Wedding Dress, Bridal gowns and Bridal Dress
wedding dresses
  
Gravatar # re: F# - Ordinary Least Squares (OLS)
by deexu at 7/16/2010 1:46 AM

great range of Ed Hardy products. Ed Hardy Women's Ellerise Lowrise Sneaker · Ed Hardy Women's
ed hardy jeans, ed hardy hoody, ed hardy shirt, ed hardy clothing, ed hardy cap, ed glasses, ed belts,
women fashion shoes, men's clothes. helping .perhaps you will like
Ed Hardy
Ed Hardy shoes
Ed Hardy shirts
Ed Hardy clothes
Ed Hardy clothing
Ed Hardy shoes
Don Ed Hardy is an American tattoo collector raised in Southern California
Ed Hardy Clothing,Christian Audigier,Ed Hardy Shoes,Ed Hardy Swimwear,Ed Hardy Hat,
ED Hardy Caps
Ed Hardy Sunglasses
Ed Hardy Wallets
EdHardy
Gucci outlet store online, numerous cheap Gucci bags, handbags, wallets, purses, totes, shoes on sale,
cheap prices and authentic qualities
gucci handbags
gucci jewelryRGRTGHBJOIJIJ
  
Gravatar # re: F# - Ordinary Least Squares (OLS)
by replica lv handbags at 7/17/2010 5:53 AM

Here is some introduce about Louis Vuitton Taiga Leather Bags TAIGA LEATHTHER: created in 1993, replica louis vuitton wallets designed specifically for men, careful pressure striae, simple and elegant design, there are two colors: Epicea Acajuo dark brown dark green and purple, features: solid lock, excellent corners. Louis Vuitton Wallets Taiga Leather bags are popular for its amazing design and quality,cheap louis vuitton wallets anyone who to own it can order on this site!Taiga Leather is a world leading brand name in the fashion industry. louis vuitton wallets for men A Taiga Leather is not only a sign of taste and fashion; it is also a symbol of social status and recognition. At our website the quality of handbag has reached the highest level of perfection and it comes at a very attractive price. Louis Vuitton Handbags are inspired by louis vuitton mens wallets the highly popular designs of their original counterparts.
  
Gravatar # re: F# - Ordinary Least Squares (OLS)
by lv handbags at 7/24/2010 5:52 AM

These louis vuitton agendas have a cute and fun appeal, and are unique to exemplify the fusion of fashion and function in this seasons handbag collections. leather agendasThe original designs of Louis Vuitton Agendas has captured the fashion of the season as well as the lifestyle of one-of-a-kind artworks by focusing on handbags that blend art and function in our louis vuitton agendas online shop. replica Louis Vuitton Agendas Collection is perfect for the woman who appreciates high quality handbags and seeks to incorporate beauty with function.
  
Gravatar # re: F# - Ordinary Least Squares (OLS)
by Men’s belts at 7/28/2010 11:43 PM

Men’s belts, LV men’s belts, Fashionable Gucci men’s belts, Attractive style Hermes men’s belts.

  
Gravatar # re: F# - Ordinary Least Squares (OLS)
by Vibram Five Fingers at 8/13/2010 12:54 AM


This is such a great resource you are providing. I enjoy seeing sites that understand the value of providing a prime resource for free. I really loved reading your post. Thanks!
http://www.vibramfivefinger.info Running Barefoot
http://www.vibramfivefinger.info vibram bikila
http://www.vibramfivefinger.info Discount Vibram Five Fingers
www.vibramfivefinger.info/...gers-flow-c-12_4.html Vibram Five Fingers Flow
www.vibramfivefinger.info/...gers-flow-c-12_4.html Vibram Five Fingers KSO Trek
http://www.nikedunkssbshoes.com nike dunk shoes
http://www.nikedunkssbshoes.com nike sb sale
http://www.nikedunkssbshoes.com nike skateboarding
  
Gravatar # Chanel Handbags
by Chanel Handbags at 9/1/2010 6:38 PM

If theres one thing Ive learned working on a bag blog for the past three years, then it is that women take a lot of pride in their designer handbags collections...
  
Gravatar # re: F# - Ordinary Least Squares (OLS)
by nannan at 9/3/2010 12:45 AM

NYTGRTHRF HFRGTCF In this case I use the start date of 01/10/1990, the end date is today ( the implicit function DateTime.Today returns today). Again, note that I have to enclose my Convert.ToDateTime function in parentheses because it is a function callTiffany jewellery
Tiffany
Tiffany & Co
Tiffany Co Bracelets
Tiffany Co Charms
Tiffany Co Earrings
Tiffany sale
Tiffany Co Necklaces
Tiffany uk
Tiffany Co Rings
tiffany jewelry
tiffany co
  
Gravatar # Lic
by womens ugg boots at 9/8/2010 7:39 PM

In short, professional design cheap supra shoes for your feet more comfortable.


Have you heard of air max shop? it is good for you.


ugg boots Shoes now,we have many colors for you,ugg boots and so on ,free shipping and nontax,only need 1 week to your door!Enjoying shopping now!


it represent a symbol of fashion,taste.And classic tall uggs shoes can give you these feeling which you want to find.


uggs classic short have different designs.


nike air max shoes are on hot sale.


  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 7 and type the answer here: