Issuu on Google+

DATA ANALYSIS Example

Using Genfit: Power Regression This QuickSheet demonstrates the use of Mathcad's genfit func ons to fit data to a power curve model. The specialized built‐in fi ng func on, pwrfit, may also be used.

data =

⎤ ⎡0 0 ⎢ 1 2.004 ⎥ ⎢ ⎥ ⎢ 2 7.287 ⎥ ⎢ 3 13.72 ⎥ ⎢⎣ ⎥⎦ ⋮

⟨⟩ X ≔ data⟨0⟩

Note: data matrix definition to right 

⟨⟩ Y ≔ data⟨1⟩

First look at the use of genfit, taking the fi ng func on to be y = A · x , where A and b are unknown. From this model, define the func on vector for use with the genfit func on.

⎤ model func on ⎡ A ⋅ nb ⎢ ⎥ f ((n , A , b)) ≔ ⎢ nb ⎥ deriva ve to first parameter b ⎢⎣ A ⋅ ln0 ((n)) ⋅ n ⎥⎦ deriva ve to second parameter

The ln0 func on is used here instead of ln, because it can be evaluated approximately at 0, which is necessary for adjus ng the parameter values while minimizing the error. If you change the above expression for f to use ln, genfit fails to converge.

Note: Use just the first element in the vector of func ons if you wish genfit to calculate the deriva ves numerically. Choose "Op mized Levenberg‐Marquardt" for the genfit method by right‐clicking on genfit to use this op on. Define a vector of guesses:

⎡2⎤ guess ≔ ⎢ ⎥ ⎣2⎦

Use genfit to find the parameters in the model func on: ⎡ 2.746 ⎤ cg = ⎢ ⎣ 1.487 ⎥⎦

cg ≔ genfit ((X , Y , guess , f))

Then, define a func on using these coefficients: h1 ((x)) ≔ cg ⋅ x

cg 1

0

Page 1 of 4


DATA ANALYSIS Example Plot the results: 150

120

Y h1 ((x))

90

60

30

0 0

1

2

3

4

5

6

7

8

X

9

10

11

12

13

14

15

x

Now take a look at pwrfit. The fi ng func on is y = a · x + c, where a, b, and c are unknown. ⎡2⎤ ⎢ Guess ≔ 2 ⎥ ⎢ ⎥ ⎣0⎦

Define a vector of guesses:

Use pwrfit to find the parameters in the model func on: ⎡ 2.839 ⎤ cpw = ⎢ 1.475 ⎥ ⎢ ⎥ ⎣ −0.452 ⎦

cpw ≔ pwrfit ((X , Y , Guess))

Define a func on using these coefficients, and define a range variable over which to graph it. h2 ((x)) ≔ cpw ⋅ x 0

cpw 1

+ cpw

x ≔ min ((X)) ‥ max ((X))

2

150

120

Y h2 ((x))

90

60

30

0 0

1

2

3

4

5

6

7

X

Page 2 of 4

8

x

9

10

11

12

13

14

15


DATA ANALYSIS Example Looking at residual plots, it doesn't seem that either model fits the data very well. The largest residual values are:

――→ max ⎛⎝h1 ((X)) − Y⎞⎠ = 14.207

and:

――→ max ⎛⎝h2 ((X)) − Y⎞⎠ = 14.288

The smallest residual values are:

――→ min ⎛⎝h1 ((X)) − Y⎞⎠ = −10.471

and:

――→ min ⎛⎝h2 ((X)) − Y⎞⎠ = −10.379

15 12 9 6

――→ h1 ((X)) − Y ――→ h2 ((X)) − Y

0

3 0 -3 -6 -9 -12 -15 0

1

2

3

4

5

6

X

7

8

9

10

11

12

13

14

Calculate the correla on coefficients, however, and the fit seems much be er than the residual plot might reveal. ――→ corr ⎛⎝h1 ((X)) , Y⎞⎠ = 0.993

――→ corr ⎛⎝h2 ((X)) , Y⎞⎠ = 0.993

For data that exists on a large scale, par cularly power or exponen al data that changes rapidly over larger and larger values of X, a residual plot is not necessarily the best tool for determining the goodness of a fit.

Weighted genfit If you need to modify the vector of func ons fed to genfit based on other parameters, you might use local func ons to create the vector. For example, suppose you want to create a func on that weights the data according to its standard devia ons. v ⎡ ⎤ 1 v ⋅ n ⎢ ⎥ 0 ⎢ ⎥ v ⎢ ⎥ 1 ( ) f ( n , v) ≔ n ⎢ ⎥ v ⎢ v ⋅ ln0 (n) ⋅ n 1 ⎥ ( ) ⎢⎣ 0 ⎥⎦

Minimize the merit func on: Error = ∑ ⎛wt ⋅ ⎛f ⎛x , coef⎞ − y ⎞⎞ i ⎝ ⎝ i i⎠⎠ ⎠ i ⎝

2

Page 3 of 4


DATA ANALYSIS Example You've used the vector input form for parameters so the weighted func on can easily be used for other types of fi ng func ons with an arbitrary number of parameters. ⎡ 0.137 ⎤ ⎢ 0.157 ⎥ ⎢ ⎥ ⎢ 1.37 ⎥ ⎢ 0.274 ⎥ ⎢ 0.356 ⎥ ⎢ 0.463 ⎥ ⎢ ⎥ 0.492 ⎢ ⎥ σ ≔ ⎢ 0.638 ⎥ ⎢ 0.714 ⎥ ⎢ 0.962 ⎥ ⎢ 0.837 ⎥ ⎢ ⎥ ⎢ 1.187 ⎥ ⎢ 1.251 ⎥ ⎢ 1.384 ⎥ ⎢⎣ 1.507 ⎥⎦

‖ | ‖ 1| ‖ | ( ) ‖ | genfitwt (x , y , σ , guess , f) ≔ wt ← s ← ― ‖ | σ| ‖ ‖ | ‖ s | ‖ | ‖― ‖ | ‖ ||s|| | ‖ | ( )| ‖ for i ∊ 0 ‥ last (x)| | ‖ ‖ vx ← i | i ‖ ‖ | ‖ | ――→ ‖ | ( ) ‖ vy ← (y ⋅ wt) | ‖ fwt ((i , v)) ← wti ⋅ f ⎛xi , v⎞ | ⎝ ⎠ ‖ | ⎛⎝vx , vy , guess , fwt⎞⎠ return genfit ‖ ‖ |

Skew one of the data points from the original set and use the set of standard devia ons above Y ≔ 123.3 2

⎛ ⎡ Ag ⎤⎞ fit ((x)) ≔ f ⎜x , ⎢ ⎝ ⎣ bg ⎥⎦⎟⎠

⎡ Ag ⎤ ⎡ 2.978 ⎤ ⎢⎣ bg ⎥⎦ ≔ genfitwt ((X , Y , σ , guess , f)) = ⎢⎣ 1.446 ⎥⎦

0

Using the genfit func on without weights: ⎛ ⎡ Anw ⎤⎞ fitunweighted ((x)) ≔ f ⎜x , ⎢ ⎝ ⎣ bnw ⎥⎦⎟⎠

⎡ Anw ⎤ ⎡ 7.982 ⎤ ⎢⎣ bnw ⎥⎦ ≔ genfit ((X , Y , guess , f)) = ⎢⎣ 1.047 ⎥⎦

0

fit ((xt))

Y

fitunweighted ((xt))

150 135 120 105 90 75 60 45 30 15 0 0

1

2

3

4

5

6

7

X

8

xt

Page 4 of 4

9

10

11

12

13

14

15


GenFit