A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
43
Task/Multiple-regression/PicoLisp/multiple-regression-1.l
Normal file
43
Task/Multiple-regression/PicoLisp/multiple-regression-1.l
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
(scl 20)
|
||||
|
||||
# Matrix transposition
|
||||
(de matTrans (Mat)
|
||||
(apply mapcar Mat list) )
|
||||
|
||||
# Matrix multiplication
|
||||
(de matMul (Mat1 Mat2)
|
||||
(mapcar
|
||||
'((Row)
|
||||
(apply mapcar Mat2
|
||||
'(@ (sum */ Row (rest) (1.0 .))) ) )
|
||||
Mat1 ) )
|
||||
|
||||
# Matrix identity
|
||||
(de matIdent (N)
|
||||
(let L (need N (1.0) 0)
|
||||
(mapcar '(() (copy (rot L))) L) ) )
|
||||
|
||||
# Reduced row echelon form
|
||||
(de reducedRowEchelonForm (Mat)
|
||||
(let (Lead 1 Cols (length (car Mat)))
|
||||
(for (X Mat X (cdr X))
|
||||
(NIL
|
||||
(loop
|
||||
(T (seek '((R) (n0 (get R 1 Lead))) X)
|
||||
@ )
|
||||
(T (> (inc 'Lead) Cols)) ) )
|
||||
(xchg @ X)
|
||||
(let D (get X 1 Lead)
|
||||
(map
|
||||
'((R) (set R (*/ (car R) 1.0 D)))
|
||||
(car X) ) )
|
||||
(for Y Mat
|
||||
(unless (== Y (car X))
|
||||
(let N (- (get Y Lead))
|
||||
(map
|
||||
'((Dst Src)
|
||||
(inc Dst (*/ N (car Src) 1.0)) )
|
||||
Y
|
||||
(car X) ) ) ) )
|
||||
(T (> (inc 'Lead) Cols)) ) )
|
||||
Mat )
|
||||
19
Task/Multiple-regression/PicoLisp/multiple-regression-2.l
Normal file
19
Task/Multiple-regression/PicoLisp/multiple-regression-2.l
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(de matInverse (Mat)
|
||||
(let N (length Mat)
|
||||
(unless (= N (length (car Mat)))
|
||||
(quit "can't invert a non-square matrix") )
|
||||
(mapc conc Mat (matIdent N))
|
||||
(mapcar '((L) (tail N L)) (reducedRowEchelonForm Mat)) ) )
|
||||
|
||||
(de columnVector (Ary)
|
||||
(mapcar cons Ary) )
|
||||
|
||||
(de regressionCoefficients (Mat X)
|
||||
(let Xt (matTrans X)
|
||||
(matMul (matMul (matInverse (matMul Xt X)) Xt) Mat) ) )
|
||||
|
||||
(setq
|
||||
Y (columnVector (1.0 2.0 3.0 4.0 5.0))
|
||||
X (columnVector (2.0 1.0 3.0 4.0 5.0)) )
|
||||
|
||||
(round (caar (regressionCoefficients Y X)) 17)
|
||||
Loading…
Add table
Add a link
Reference in a new issue