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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
!
! Robust estimate of a plane
!
! Copyright © 2015 F.Hroch (hroch@physics.muni.cz)
!
! This file is part of Munipack.
!
! Munipack is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! Munipack is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with Munipack. If not, see <http://www.gnu.org/licenses/>.
!
!
module robustplane
implicit none
! numerical precision of real numbers
integer, parameter, private :: dbl = selected_real_kind(15)
! print debug informations ?
logical, parameter, private :: verbose = .false.
real(dbl), dimension(:), allocatable, private :: xdata, ydata, zdata, &
xsig, ysig, zsig
real(dbl), private :: rsig
private :: planefun,planedif,planelog,res
contains
subroutine rplane(x,dx,y,dy,z,dz,q,dq,sig,info)
use minpacks
use NelderMead
use oakleaf
real(dbl), parameter :: macheps = epsilon(1.0_dbl)
integer, parameter :: maxeval = 1000000
real(dbl), dimension(:), intent(in) :: x,y,z,dx,dy,dz
real(dbl), dimension(:), intent(in out) :: q,dq
real(dbl), intent(out) :: sig
integer, intent(out) :: info
integer :: icount, numres, ifault, ndata, nprint
real(dbl), dimension(size(q)+1) :: p,dp,p0
real(dbl), dimension(size(p),size(p)) :: jac,hess
real(dbl),dimension(:), allocatable :: f,df,r
real(dbl) :: s,d,mad,sum2,sum3
integer :: i
if( size(q) /= 3 ) stop 'Plane needs to have tree parameters.'
if( verbose ) then
nprint = 1
else
nprint = 0
end if
info = 3 ! no initialisation
ndata = size(x)
allocate(xdata(ndata),ydata(ndata),zdata(ndata),xsig(ndata),ysig(ndata),zsig(ndata),&
f(ndata),df(ndata),r(ndata))
xdata = x
ydata = y
zdata = z
xsig = dx
ysig = dy
zsig = dz
! init by absolute deviations
p(1:3) = q
dp(1:3) = dq
p0 = p
call nelmin(planefun,3,p0(1:3),p(1:3),mad,macheps,dp(1:3),1,maxeval,icount,numres,ifault)
if( verbose ) write(*,'(a,3f15.3,i5)') 'rplane init:',p(1:3),ifault
if( ifault /= 0 ) then
info = 2 ! no convergence
if( verbose ) &
write(*,*) "rplane init finished prematurely without convergence."
goto 666
end if
q = p(1:3)
r = abs(zdata - (p(1) + p(2)*xdata + p(3)*ydata))
sig = median(r) / 0.6745
if( verbose ) write(*,'(a,f0.5)') 'sig0 = ',sig
rsig = sig
if( verbose ) write(*,*) 'Winsorisation:'
do i = 1,size(zdata)
r(i) = (zdata(i) - (p(1) + p(2)*xdata(i) + p(3)*ydata(i))) / sig
if( abs(r(i)) > 3 ) then
d = p(1) + p(2)*xdata(i) + p(3)*ydata(i) + sign(3*sig,r(i))
if( verbose ) write(*,'(4g15.5)') i,d,zdata(i),r(i)
zdata(i) = d
end if
end do
! locate proper minimum
p(4) = 1
p0 = p
dp = abs(0.1*p + 0.01*sig + 1e-3)
dp(4) = 0.1
dp = 0.1*dp
call nelmin(planelog,size(p),p0,p,s,macheps,dp,1,maxeval,icount,numres,ifault)
if( verbose ) write(*,'(a,i3,4g13.5)') 'Approximate solution: ',ifault,p
if( ifault /= 0 ) then
info = 2 ! no convergence
if( verbose ) &
write(*,*) "rplane finished prematurely without likelihood convergence."
goto 666
end if
q = p(1:3)
! robust estimate
call lmdif2(planedif,p,epsilon(p),nprint,info)
if( verbose ) write(*,'(a,4f10.5,i5)') 'robust:',p,info
if( info == 5 ) then
info = 2 ! no convergence
if( verbose ) &
write(*,*) "rplane finished prematurely without lmdif convergence."
goto 666
end if
q = p(1:3)
! estimation of uncertainties
call res(p(1),p(2),p(3),r)
f = huber(r)
df = dhuber(r)
sum2 = sum(df)
sum3 = sum(f**2)
if( sum2 > epsilon(sum2) ) then
! Huber (6.6)
! compute jac!!
call qrinv(jac,hess)
sig = sig*sqrt(sum3/sum2*ndata/(ndata-1.0))
do i = 1, size(q)
if( abs(hess(i,i)) > 0 ) then
dq(i) = sig * sqrt(abs(hess(i,i)))
else
dq(i) = sig / sqrt(ndata - 1.0)
end if
end do
if( verbose ) then
write(*,*) 'jac:',real(jac(1,:))
write(*,*) 'jac:',real(jac(2,:))
write(*,*) 'jac:',real(jac(3,:))
write(*,*) 'jac:',real(jac(4,:))
write(*,*) 'hess:',real(hess(1,:))
write(*,*) 'hess:',real(hess(2,:))
write(*,*) 'hess:',real(hess(3,:))
write(*,*) 'hess:',real(hess(4,:))
end if
else
dq = 0
end if
info = 0
666 continue
deallocate(xdata,ydata,zdata,xsig,ysig,zsig,f,df,r)
end subroutine rplane
function planefun(p) result(s)
real(dbl), dimension(:), intent(in) :: p
real(dbl) :: s
s = sum(abs(p(1) + p(2)*xdata + p(3)*ydata - zdata)) / size(xdata)
end function planefun
subroutine res(a,b,c,r)
real(dbl), intent(in) :: a,b,c
real(dbl), dimension(:), intent(out) :: r
r = (zdata - (a + b*xdata + c*ydata)) / &
sqrt(zsig**2 + b**2*xsig**2 + c**2*ysig**2 + rsig**2)
end subroutine res
function planelog(p)
use oakleaf
real(dbl), dimension(:), intent(in) :: p
real(dbl) :: planelog
real(dbl), dimension(:), allocatable :: r,f,ds
real(dbl) :: a,b,c,s
integer :: n
n = size(xdata)
a = p(1)
b = p(2)
c = p(3)
s = p(4)
if( s < epsilon(s) ) then
planelog = 100*n
return
end if
allocate(r(n),f(n),ds(n))
call res(a,b,c,r)
r = r / s
f = ihuber(r)
planelog = sum(f) + n*log(s)
! write(*,'(7f10.5)') p,planelog,sum(f),n*log(s)
deallocate(r,f,ds)
end function planelog
subroutine planedif(m,np,p,fvec,iflag)
use oakleaf
integer, intent(in) :: m,np
integer, intent(in out) :: iflag
real(dbl), dimension(np), intent(in) :: p
real(dbl), dimension(m), intent(out) :: fvec
real(dbl), dimension(:), allocatable :: r,f,ds
integer :: n
real(dbl) :: a,b,c,s
if( iflag == 0 ) then
! write(*,'(6g13.5)') p
! write(*,'(6g13.5)') fvec
return
end if
n = size(xdata)
allocate(r(n),f(n),ds(n))
a = p(1)
b = p(2)
c = p(3)
s = p(4)
ds = sqrt(zsig**2 + b**2*xsig**2 + c**2*ysig**2 + rsig**2)
call res(a,b,c,r)
r = r / s
f = huber(r)
fvec(1) = sum(f/ds)
fvec(2) = sum(f*(xdata + r*b*xsig**2/ds)/ds)
fvec(3) = sum(f*(ydata + r*c*ysig**2/ds)/ds)
fvec(4) = sum(f*r)/s - n
fvec = - fvec / s
deallocate(r,f,ds)
end subroutine planedif
end module robustplane
|