1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
{ ******************************************************************
Multiple linear regression (Gauss-Jordan method)
****************************************************************** }
unit umulfit;
interface
uses
utypes, ulineq;
procedure MulFit(X : PMatrix;
Y : PVector;
Lb, Ub, Nvar : Integer;
ConsTerm : Boolean;
B : PVector;
V : PMatrix);
{ ------------------------------------------------------------------
Multiple linear regression: Y = B(0) + B(1) * X + B(2) * X2 + ...
------------------------------------------------------------------
Input parameters: X = matrix of independent variables
Y = vector of dependent variable
Lb, Ub = array bounds
Nvar = number of independent variables
ConsTerm = presence of constant term B(0)
Output parameters: B = regression parameters
V = inverse matrix
------------------------------------------------------------------ }
procedure WMulFit(X : PMatrix;
Y, S : PVector;
Lb, Ub, Nvar : Integer;
ConsTerm : Boolean;
B : PVector;
V : PMatrix);
{ ----------------------------------------------------------------------
Weighted multiple linear regression
----------------------------------------------------------------------
S = standard deviations of observations
Other parameters as in MulFit
---------------------------------------------------------------------- }
implementation
procedure MulFit(X : PMatrix;
Y : PVector;
Lb, Ub, Nvar : Integer;
ConsTerm : Boolean;
B : PVector;
V : PMatrix);
var
Lb1 : Integer; { Index of first param. (0 if cst term, 1 otherwise) }
I, J, K : Integer; { Loop variables }
Det : Float; { Determinant }
begin
if Ub - Lb < Nvar then
begin
SetErrCode(MatErrDim);
Exit;
end;
{ Initialize }
for I := 0 to Nvar do
begin
for J := 0 to Nvar do
V^[I]^[J] := 0.0;
B^[I] := 0.0;
end;
{ If constant term, set line 0 and column 0 of matrix V }
if ConsTerm then
begin
V^[0]^[0] := Int(Ub - Lb + 1);
for K := Lb to Ub do
begin
for J := 1 to Nvar do
V^[0]^[J] := V^[0]^[J] + X^[K]^[J];
B^[0] := B^[0] + Y^[K];
end;
for J := 1 to Nvar do
V^[J]^[0] := V^[0]^[J];
end;
{ Set other elements of V }
for K := Lb to Ub do
for I := 1 to Nvar do
begin
for J := I to Nvar do
V^[I]^[J] := V^[I]^[J] + X^[K]^[I] * X^[K]^[J];
B^[I] := B^[I] + X^[K]^[I] * Y^[K];
end;
{ Fill in symmetric matrix }
for I := 2 to Nvar do
for J := 1 to Pred(I) do
V^[I]^[J] := V^[J]^[I];
{ Solve normal equations }
if ConsTerm then Lb1 := 0 else Lb1 := 1;
LinEq(V, B, Lb1, Nvar, Det);
end;
procedure WMulFit(X : PMatrix;
Y, S : PVector;
Lb, Ub, Nvar : Integer;
ConsTerm : Boolean;
B : PVector;
V : PMatrix);
var
Lb1 : Integer; { Index of first param. (0 if cst term, 1 otherwise) }
I, J, K : Integer; { Loop variables }
W : PVector; { Vector of weights }
WX : Float; { W * X }
Det : Float; { Determinant }
begin
if Ub - Lb < Nvar then
begin
SetErrCode(MatErrDim);
Exit;
end;
for K := Lb to Ub do
if S^[K] <= 0.0 then
begin
SetErrCode(MatSing);
Exit;
end;
DimVector(W, Ub);
for K := Lb to Ub do
W^[K] := 1.0 / Sqr(S^[K]);
{ Initialize }
for I := 0 to Nvar do
begin
for J := 0 to Nvar do
V^[I]^[J] := 0.0;
B^[I] := 0.0;
end;
{ If constant term, set line 0 and column 0 of matrix V }
if ConsTerm then
begin
for K := Lb to Ub do
begin
V^[0]^[0] := V^[0]^[0] + W^[K];
for J := 1 to Nvar do
V^[0]^[J] := V^[0]^[J] + W^[K] * X^[K]^[J];
B^[0] := B^[0] + W^[K] * Y^[K];
end;
for J := 1 to Nvar do
V^[J]^[0] := V^[0]^[J];
end;
{ Set other elements of V }
for K := Lb to Ub do
for I := 1 to Nvar do
begin
WX := W^[K] * X^[K]^[I];
for J := I to Nvar do
V^[I]^[J] := V^[I]^[J] + WX * X^[K]^[J];
B^[I] := B^[I] + WX * Y^[K];
end;
{ Fill in symmetric matrix }
for I := 2 to Nvar do
for J := 1 to Pred(I) do
V^[I]^[J] := V^[J]^[I];
{ Solve normal equations }
if ConsTerm then Lb1 := 0 else Lb1 := 1;
LinEq(V, B, Lb1, Nvar, Det);
DelVector(W, Ub);
end;
end.
|