Docsity

Project 3: Two-dimensional Arrays and Multilinear Regression, Slides of Computer Science

Toggle sidebar
bg1
Project 3 – Two-dimensional arrays May 9, 2006
1
Third Programming Project
Third Programming Project
–
–Two
Two-
-Dimensional Arrays
Dimensional Arrays
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
May 9, 2006
2
Outline
• Quiz three on Thursday for full lab
period
– See sample quiz on home page of course
web site
• Project three
– Multilinear regression
– Equations to be solved
– Data structures to be used
– Library program use in separate code file
3
Files You Can Download
• Project instructions on home page and
projects page
• Other files on projects page only
– Data file for project
– Library program for solving simultaneous
linear equations
– Excel file with answers
• This lecture on home page and
laboratory presentations page
4
Exercise 8 Linear Regression
• Fit linear relation-
ship to measured
data pairs (xi, yi)
• y = a + bx
• Have equations to
determine a and b
• Get goodness-of-
fit measures
Fitted Line
y
x
indicates data points
y
i
x
i
i
y
ˆ
• Fitted value at
xiis ii bx+a=y
ˆ
5
Project Three Regression
• Have a result that depends on more
than one variable
– Example is emissions from diesel engine
that depends on fuel properties
– emissions = b0+ b1(cetane) +
b2(aromatics) + b3(density)
• Use measured data on emissions,
cetane, aromatics to find b0, b1, b2, b3
6
General Regression
• Use notation so that we can write code
for any number of predictive variables
• Call predictive variables x1, x2, x3, etc.
• Call response variable y
– In previous example, x1= cetane, x2=
aromatics, x3= density, and y = emissions
– For three variables the equation is y = b0+
b1 x1+ b2 x2+ b3 x3
pf3
pf4
pf5

Partial preview of the text

Download Project 3: Two-dimensional Arrays and Multilinear Regression and more Slides Computer Science in PDF only on Docsity!

Third Programming Project Third Programming Project

  • Two–Two--Dimensional ArraysDimensional Arrays

Larry Caretto Computer Science 106

Computing in Engineering

and Science

May 9, 2006

2

Outline

  • Quiz three on Thursday for full lab

period

  • See sample quiz on home page of course web site
  • Project three
  • Multilinear regression
  • Equations to be solved
  • Data structures to be used
  • Library program use in separate code file

3

Files You Can Download

  • Project instructions on home page and

projects page

  • Other files on projects page only
    • Data file for project
    • Library program for solving simultaneous linear equations
    • Excel file with answers
  • This lecture on home page and

laboratory presentations page

4

Exercise 8 Linear Regression

  • Fit linear relation-

ship to measured

data pairs (xi, yi)

  • y = a + bx
  • Have equations to

determine a and b

  • Get goodness-of-

fit measures

Fitted Line

y

x indicates data points

y (^) i x (^) i

y ˆ i

  • Fitted value at xi is yˆi =a+bxi

5

Project Three Regression

  • Have a result that depends on more

than one variable

  • Example is emissions from diesel engine that depends on fuel properties
  • emissions = b 0 + b 1 (cetane) + b 2 (aromatics) + b 3 (density)
  • Use measured data on emissions,

cetane, aromatics to find b 0 , b 1 , b 2 , b 3

6

General Regression

  • Use notation so that we can write code

for any number of predictive variables

  • Call predictive variables x 1 , x 2 , x 3 , etc.
  • Call response variable y
    • In previous example, x 1 = cetane, x 2 = aromatics, x 3 = density, and y = emissions
    • For three variables the equation is y = b 0 + b 1 x 1 + b 2 x 2 + b 3 x 3

7

General Equation and Data

  • In general we can have K predictive

variables, x 1 to x k

  • General model equation:
  • How do we represent the data?
    • Each data set consists of one value of y and one value for each of the x (^) j variables
    • For data set m, we can call the value of y, ym , and we can call the value of x (^) j for data set m xjm

j

K

j

y b ∑ bj x

=

1

0

8

Data Set with K = 3 and N = 8

m y x 1 x 2 x 3 0 2.55 3.00 440 500 1 1.95 3.47 350 400 2 1.89 3.14 440 540 3 2.24 3.46 350 370 4 2.31 3.59 450 480 5 1.74 1.75 200 320 6 1.87 3.03 310 470 7 0.83 3.18 290 400

  • Each data

set, m, has a

value for y

and each xj

  • What are

y 4 , x 23 , x 31?

y 0

x 12

x 35

9

Summary of Data

  • We use K different variables (x 1 to xK) to

predict the value of another variable, y

  • We have N sets of data
    • numbered from m = 0 to m = N-
    • each data set has one value of y, called y (^) m , and one value of each xj , called xjm
    • all x and y values from file input
    • all data used to determine b 0 to b (^) K

10

How do we find b j?

  • Define x0m = 1 for all m = 0, ... , N-
    • Note that there is no x 0 in model
    • Setting x0m = 1 used to simplify equations
  • Values of b 0 , ... , b (^) K found by solving set

of K + 1 simultaneous linear equations

  • A (^) i0 b 0 + A (^) i1 b 1 + A (^) i2 b 2 +.....+ A (^) iK b (^) K = c (^) i
  • Compute A (^) ij and c (^) i from input data
  • Use library program to solve equations

11

Equations to be Used

  • Compute the Aij

coefficients ∑

−

=

1

0

N

m

Aij ximxjm

−

=

=

1

0

N

m

  • Compute the ci ximym

c i coefficients

  • Use the “library”

routine provided

to solve for b j

A b ci i K

K

j

ij j^0 , ,

0

= = K

=

12

Computing one A[i][j] (A[1][2])

m y x 0 x 1 x 2 x 3 0 2.55 1 3.00 440 500 1 1.95 1 3.47 350 400 2 1.89 1 3.14 440 540 3 2.24 1 3.46 350 370 4 2.31 1 3.59 450 480 5 1.74 1 1.75 200 320 6 1.87 1 3.03 310 470 7 0.83 1 3.18 290 400

A 12 = x 10 x 20 +

x 11 x 21 + x 12 x 22

x 13 x 23 + x 14 x 24

+ x 15 x 25 +

x 16 x 26 + x 16 x 26

+ x 17 x 27 = A 21

−

=

=

1

0

N

m

Aij ximxjm

19

Arrays for Computing Aij and c i

  • A (^) ij is 2D array with both dimensions at least the number of variables plus two
  • c (^) i is 1D array with same dimension as A (^) ij
  • xim is 2D array with different dimensions
    • First is the number of variables plus one
    • Second is maximum data points
  • ym is 1D array; dimension is maximum data points

−

=

1

0

N

m

Aij ximxjm ∑

−

=

1

0

N

m

ci ximym

20

Code for Computing A[i][j]

−

=

1

0

N

m

Aij ximxjm

  • Use typical code for computing a sum
  • A[i][j] += x[i][m] * x[j][m]
  • What is the for loop for this sum?
  • for ( m = 0; m < N; m++ )
  • How do we compute all the A[i][j]?
    • We need to place the computation of one A[i][j] inside two for loops over i and j

21

Library Function to Get bj

  • Code variables use typical notation for

system of linear equations shown above

  • a (^) ij are left-hand side coefficients
  • b (^) i are right-hand-side coefficients
  • xj are unknowns
  • There are N equations to be solved

0 , , 1

1

0

= = −

−

=

∑ a^ x bi i N

N

j

ij j K

22

Library Function Definition

  • Function prototype (second dimension

required for passing 2D arrays to functions)

bool GaussianElim( double a[][MAX_VAR], double b[], double x[], int N_eqn );

a[][MAX_VAR] is left-hand side array (aij)

b[] is right hand side array (b i)

x[] is unknown array (xj)

N_eqn is the number of equations (N)

23

Using bool GaussianElim

  • Function returns an error flag in the

function name

  • True indicates an error
  • False indicates a valid solution if ( GaussianElim( A, c, b, K+1 ) ) { cout << “No solution for b[j]”; return EXIT_FAILURE; } // remainder of code

24

Compare prototype and call

if ( GaussianElim( A, c, b, K+1 ) ) { cout << “No solution for b[j]”; return EXIT_FAILURE; }

bool GaussianElim( double a[][MAX_VAR], double b[], double x[], int N_eqn ); Left-side

Right-side Unknowns

Number of equations

  • Prototype

25

Passing the Second Dimension

  • Use two files: your code and the library

program

  • Place prototype for library program in your code
  • Make sure that the second dimension for a[i][j] (or A[i][j]) is the same for three cases - The header in the library program - The prototype in your code - Your declaration of the A[i][j] array

26

Project Three Code Summary

  • Input data on K, N, ym, and xjm
  • Set each x0m = 1
  • Compute A (^) ij and c (^) i
  • Use library program that takes Aij and c (^) i

as inputs and returns solution for b j

  • Use b (^) j to compute predicted values:
  • Compute R^2 and output results

yˆ m