FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ErrorsInVariables.jl/src/estimator.jl at main · jbytecode/ErrorsInVariables.jl · GitHub
jbytecode
/
ErrorsInVariables.jl
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
8
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ErrorsInVariables.jl
/
src
/
estimator.jl
Copy path
More file actions
More file actions
Latest commit
History
History
History
196 lines (141 loc) · 4.86 KB
Breadcrumbs
ErrorsInVariables.jl
/
src
/
estimator.jl
Copy path
File metadata and controls
196 lines (141 loc) · 4.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
184
185
186
187
188
189
190
191
192
193
194
195
196
module
Estimator
export
eive
import
..
CGA
:
cga
import
..
SimpleEiveResult
import
Random
:
AbstractRNG, MersenneTwister
"""
eive(;
dirtyx::Vector{T},
y::Vector{T},
otherx::Union{Nothing, Matrix{T}, Vector{T}},
popsize::Int = 50,
numdummies::Int = 10,
rng::RNGType = MersenneTwister(1234))::SimpleEiveResult where {T<:Real, RNGType<:AbstractRNG}
# Description:
The method searches for a set of dummy (binary) variables that separates the erroneous independent variable
into clean part and error part. The clean part is then used in the main regression estimation.
Those dummy variables minimize the sum of squares of residuals of the main regression. In other terms
the methods searches for a set of proxy variables that do not exist in real. Please see the reference for details.
# Arguments:
- dirtyx: Independent variable measured with some error
- y: Dependent variable
- otherx: Matrix of other independent variables
- popsize: Number of individuals in the population (optional)
- numdummies: Number of dummy variables to use (optional)
- rng: Random number generator (optional)
# Examples
```julia-repl
julia> import Random
julia> using ErrorsInVariables
julia> rng = Random.MersenneTwister(1234)
julia> n = 30
julia> deltax = randn(rng, n) * sqrt(3.0)
julia> cleanx = randn(rng, n) * sqrt(7.0)
julia> e = randn(rng, n) * sqrt(5.0)
julia> y = 20.0 .+ 10.0 .* cleanx .+ e
julia> dirtyx = cleanx + deltax
julia> eive(dirtyx = dirtyx, y = y, otherx = nothing)
EiveResult([20.28458307772922, 9.456757289676714])
julia> X = hcat(ones(n), dirtyx);
julia> # Biased OLS estimates:
julia> X
\\
y
2-element Vector{Float64}:
17.94867860059858
5.8099584879737876
```
# References
Satman, M. Hakan, and Erkin Diyarbakirlioglu. "Reducing errors-in-variables bias in linear
regression using compact genetic algorithms." Journal of Statistical Computation and Simulation
85.16 (2015): 3216-3235.
"""
function
eive
(;
dirtyx
::
Vector{T}
,
y
::
Vector{T}
,
otherx
::
Union{Nothing, Matrix{T}, Vector{T}}
,
popsize
::
Int
=
50
,
numdummies
::
Int
=
10
,
rng
::
RNGType
=
MersenneTwister
(
1234
))
::
SimpleEiveResult
where
{T
<:
Real
, RNGType
<:
AbstractRNG
}
if
isnothing
(otherx)
return
eivewithoutotherx
(dirtyx, y, popsize, numdummies, rng)
else
return
eivewithotherx
(dirtyx, y, otherx, popsize, numdummies, rng)
end
end
"""
eivewithotherx(
dirtyx::Vector{T},
y::Vector{T},
otherx::Union{Matrix{T}, Vector{T}},
popsize::Int = 50,
numdummies::Int = 10,
rng::RNGType = MersenneTwister(1234)
)::EiveResult where {T<:Real, RNGType<:AbstractRNG}
"""
function
eivewithotherx
(
dirtyx
::
Vector{T}
,
y
::
Vector{T}
,
otherx
::
Union{Matrix{T}, Vector{T}}
,
popsize
::
Int
=
50
,
numdummies
::
Int
=
10
,
rng
::
RNGType
=
MersenneTwister
(
1234
))
::
SimpleEiveResult
where
{T
<:
Real
, RNGType
<:
AbstractRNG
}
n
=
length
(dirtyx)
myones
=
ones
(Float64, n)
chsize
=
n
*
numdummies
res
=
Array
{Float64, 1}
(undef, n)
auxX
=
Array
{Float64, 2}
(undef, n, numdummies)
betas
=
Array
{Float64, 1}
(undef, numdummies)
function
costfn
(bits
::
Vector{Int}
)
auxX .
=
reshape
(bits, n, numdummies)
betas .
=
auxX
\
dirtyx
cleanX
=
auxX
*
betas
X
=
hcat
(myones, cleanX, otherx)
outerbetas
=
X
\
y
res .
=
y
.-
X
*
outerbetas
return
sum
(res
.^
2.0
)
end
finalbits
=
cga
(chsize
=
chsize, costfunction
=
costfn, popsize
=
popsize, rng
=
rng)
auxX .
=
reshape
(finalbits, n, numdummies)
betas .
=
auxX
\
dirtyx
cleanX
=
auxX
*
betas
X
=
hcat
(myones, cleanX, otherx)
outerbetas
=
X
\
y
return
SimpleEiveResult
(outerbetas,
true
)
end
"""
eivewithoutotherx(
dirtyx::Vector{T},
y::Vector{T},
popsize::Int = 50,
numdummies::Int = 10,
rng::RNGType = MersenneTwister(1234))::EiveResult where {T<:Real, RNGType<:AbstractRNG}
"""
function
eivewithoutotherx
(
dirtyx
::
Vector{T}
,
y
::
Vector{T}
,
popsize
::
Int
=
50
,
numdummies
::
Int
=
10
,
rng
::
RNGType
=
MersenneTwister
(
1234
))
::
SimpleEiveResult
where
{T
<:
Real
, RNGType
<:
AbstractRNG
}
n
=
length
(dirtyx)
myones
=
ones
(Float64, n)
chsize
=
n
*
numdummies
res
=
Array
{Float64, 1}
(undef, n)
auxX
=
Array
{Float64, 2}
(undef, n, numdummies)
betas
=
Array
{Float64, 1}
(undef, numdummies)
function
costfn
(bits
::
Vector{Int}
)
auxX .
=
reshape
(bits, n, numdummies)
betas .
=
auxX
\
dirtyx
cleanX
=
auxX
*
betas
X
=
hcat
(myones, cleanX)
outerbetas
=
X
\
y
res .
=
y
.-
X
*
outerbetas
return
sum
(res
.^
2.0
)
end
finalbits
=
cga
(chsize
=
chsize, costfunction
=
costfn, popsize
=
popsize, rng
=
rng)
auxX .
=
reshape
(finalbits, n, numdummies)
betas .
=
auxX
\
dirtyx
cleanX
=
auxX
*
betas
X
=
hcat
(myones, cleanX)
outerbetas
=
X
\
y
return
SimpleEiveResult
(outerbetas,
true
)
end
end
#
end of module
Back
|
FazBrowse Home
|
New Git URL