June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,2 +1,2 @@
---
note: Matrices
note: Probability and statistics

View file

@ -12,20 +12,20 @@
*
* INPUT ARRAYS ARE DESTROYED!
*
*___Name_________Type_______________In/Out____Description________________________
* X(N,K) Double precision In Predictors
* Y(N) Double precision Both On input: N Observations
* On output: K beta weights
* N Integer In Number of observations
* K Integer In Number of predictor variables
* DWORK(3*K) Double precision Neither Workspace
* IWORK(K) Integer Neither Workspace
*___Name___________Type_______________In/Out____Description_____________
* X(N,K) Double precision In Predictors
* Y(N) Double precision Both On input: N Observations
* On output: K beta weights
* N Integer In Number of observations
* K Integer In Number of predictor variables
* DWORK(N+2*K) Double precision Neither Workspace
* IWORK(K) Integer Neither Workspace
*-----------------------------------------------------------------------
SUBROUTINE MR (X, Y, N, K, DWORK, IWORK)
IMPLICIT NONE
INTEGER K, N, IWORK
DOUBLE PRECISION X, Y, DWORK
DIMENSION X(N,K), Y(N), DWORK(3*K), IWORK(K)
DIMENSION X(N,K), Y(N), DWORK(N+2*K), IWORK(K)
* local variables
INTEGER I, J
@ -44,7 +44,7 @@
* call function
CALL DHFTI (X, N, N, K, Y, N, 1, TAU,
$ J, DWORK(1), DWORK(K+1), DWORK(2*K+1), IWORK)
$ J, DWORK(1), DWORK(N+1), DWORK(N+K+1), IWORK)
IF (J < K) PRINT *, 'mr: solution is rank deficient!'
RETURN
END ! of MR
@ -55,7 +55,7 @@
INTEGER N, K
PARAMETER (N=15, K=3)
INTEGER IWORK(K), I, J
DOUBLE PRECISION XIN(N), X(N,K), Y(N), DWORK(3*K)
DOUBLE PRECISION XIN(N), X(N,K), Y(N), DWORK(N+2*K)
DATA XIN / 1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68,
$ 1.70, 1.73, 1.75, 1.78, 1.80, 1.83 /

View file

@ -0,0 +1,121 @@
// Version 1.2.31
typealias Vector = DoubleArray
typealias Matrix = Array<Vector>
operator fun Matrix.times(other: Matrix): Matrix {
val rows1 = this.size
val cols1 = this[0].size
val rows2 = other.size
val cols2 = other[0].size
require(cols1 == rows2)
val result = Matrix(rows1) { Vector(cols2) }
for (i in 0 until rows1) {
for (j in 0 until cols2) {
for (k in 0 until rows2) {
result[i][j] += this[i][k] * other[k][j]
}
}
}
return result
}
fun Matrix.transpose(): Matrix {
val rows = this.size
val cols = this[0].size
val trans = Matrix(cols) { Vector(rows) }
for (i in 0 until cols) {
for (j in 0 until rows) trans[i][j] = this[j][i]
}
return trans
}
fun Matrix.inverse(): Matrix {
val len = this.size
require(this.all { it.size == len }) { "Not a square matrix" }
val aug = Array(len) { DoubleArray(2 * len) }
for (i in 0 until len) {
for (j in 0 until len) aug[i][j] = this[i][j]
// augment by identity matrix to right
aug[i][i + len] = 1.0
}
aug.toReducedRowEchelonForm()
val inv = Array(len) { DoubleArray(len) }
// remove identity matrix to left
for (i in 0 until len) {
for (j in len until 2 * len) inv[i][j - len] = aug[i][j]
}
return inv
}
fun Matrix.toReducedRowEchelonForm() {
var lead = 0
val rowCount = this.size
val colCount = this[0].size
for (r in 0 until rowCount) {
if (colCount <= lead) return
var i = r
while (this[i][lead] == 0.0) {
i++
if (rowCount == i) {
i = r
lead++
if (colCount == lead) return
}
}
val temp = this[i]
this[i] = this[r]
this[r] = temp
if (this[r][lead] != 0.0) {
val div = this[r][lead]
for (j in 0 until colCount) this[r][j] /= div
}
for (k in 0 until rowCount) {
if (k != r) {
val mult = this[k][lead]
for (j in 0 until colCount) this[k][j] -= this[r][j] * mult
}
}
lead++
}
}
fun printVector(v: Vector) {
println(v.asList())
println()
}
fun multipleRegression(y: Vector, x: Matrix): Vector {
val cy = (arrayOf(y)).transpose() // convert 'y' to column vector
val cx = x.transpose() // convert 'x' to column vector array
return ((x * cx).inverse() * x * cy).transpose()[0]
}
fun main(args: Array<String>) {
var y = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
var x = arrayOf(doubleArrayOf(2.0, 1.0, 3.0, 4.0, 5.0))
var v = multipleRegression(y, x)
printVector(v)
y = doubleArrayOf(3.0, 4.0, 5.0)
x = arrayOf(
doubleArrayOf(1.0, 2.0, 1.0),
doubleArrayOf(1.0, 1.0, 2.0)
)
v = multipleRegression(y, x)
printVector(v)
y = doubleArrayOf(52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29,
63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46)
val a = doubleArrayOf(1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70,
1.73, 1.75, 1.78, 1.80, 1.83)
x = arrayOf(DoubleArray(a.size) { 1.0 }, a, a.map { it * it }.toDoubleArray())
v = multipleRegression(y, x)
printVector(v)
}

View file

@ -0,0 +1,7 @@
clear
set seed 17760704
set obs 200
forv i=1/4 {
gen x`i'=rnormal()
}
gen y=1.5+0.8*x1-0.7*x2+1.1*x3-1.7*x4+rnormal()

View file

@ -0,0 +1 @@
reg y x*

View file

@ -0,0 +1,11 @@
. di _b[x1]
.75252466
. di _b[_cons]
1.3991314
. di _se[x1]
.06895593
. di _se[_cons]
.06978623

View file

@ -0,0 +1,25 @@
. estat ic
Akaike's information criterion and Bayesian information criterion
-----------------------------------------------------------------------------
Model | Obs ll(null) ll(model) df AIC BIC
-------------+---------------------------------------------------------------
. | 200 -487.1455 -275.6985 5 561.397 577.8886
-----------------------------------------------------------------------------
Note: N=Obs used in calculating BIC; see [R] BIC note.
. estat vce
Covariance matrix of coefficients of regress model
e(V) | x1 x2 x3 x4 _cons
-------------+------------------------------------------------------------
x1 | .00475492
x2 | -.00040258 .00486445
x3 | -.00042516 .00017355 .00521125
x4 | -.00011915 -.0002568 .00054646 .00386583
_cons | .00030777 -.00031109 -.00023794 .00058926 .00487012
. predict yhat, xb
. predict r, r