Page Menu
Home
HEPForge
Search
Configure Global Search
Log In
Files
F11221278
test_LocalLogisticRegression.cc
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Subscribers
None
test_LocalLogisticRegression.cc
View Options
#include
"UnitTest++.h"
#include
"test_utils.hh"
#include
"npstat/stat/LocalLogisticRegression.hh"
#include
"npstat/nm/ArrayND.hh"
#include
"npstat/stat/DistributionsND.hh"
#include
"geners/CPP11_array.hh"
using
namespace
npstat
;
using
namespace
std
;
#define KDDIM 2
#define NPOINTS 1000
namespace
{
typedef
CPP11_array
<
float
,(
KDDIM
+
1
)
>
Point
;
struct
PassFunctor
{
inline
bool
operator
()(
const
Point
&
pt
)
const
{
return
pt
[
KDDIM
]
>
0.5
;
}
};
TEST
(
LogisticRegressionOnKDTree
)
{
const
unsigned
idim
=
KDDIM
;
// Generate a random box
BoxND
<
double
>
box
(
idim
);
for
(
unsigned
i
=
0
;
i
<
idim
;
++
i
)
box
[
i
].
setBounds
(
test_rng
(),
1.0
+
test_rng
());
// Generate a random array shape
ArrayShape
shape
(
idim
);
for
(
unsigned
i
=
0
;
i
<
idim
;
++
i
)
shape
[
i
]
=
static_cast
<
unsigned
>
(
3
+
5
*
test_rng
());
// Generate a random weight array
ArrayND
<
double
>
w
(
shape
);
const
unsigned
long
arrlen
=
w
.
length
();
for
(
unsigned
long
i
=
0
;
i
<
arrlen
;
++
i
)
w
.
linearValue
(
i
)
=
test_rng
();
// Generate a density out of this data
std
::
vector
<
double
>
location
(
idim
);
std
::
vector
<
double
>
scale
(
idim
);
for
(
unsigned
i
=
0
;
i
<
idim
;
++
i
)
{
location
[
i
]
=
box
[
i
].
min
();
scale
[
i
]
=
box
[
i
].
length
();
}
BinnedDensityND
dens
(
&
location
[
0
],
&
scale
[
0
],
idim
,
w
,
0
);
// Build the quadratic orthogonal polynomials out of it
QuadraticOrthoPolyND
poly
(
dens
,
box
,
&
shape
[
0
],
shape
.
size
());
// Generate a vector of points
std
::
vector
<
Point
>
data
;
data
.
reserve
(
NPOINTS
);
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
{
Point
p
;
for
(
unsigned
i
=
0
;
i
<=
KDDIM
;
++
i
)
p
[
i
]
=
test_rng
();
data
.
push_back
(
p
);
}
// Generate the tree
unsigned
dimUse
[
KDDIM
]
=
{
1U
,
2U
};
KDTree
<
Point
,
double
>
tree
(
data
,
dimUse
,
KDDIM
);
// Generate the logistic regression object
PassFunctor
f
;
LogisticRegressionOnKDTree
<
Point
,
double
,
PassFunctor
>
reg
(
tree
,
f
,
poly
,
true
);
// Check the gradient calculations by this object
const
unsigned
nterms
=
poly
.
nTerms
();
std
::
vector
<
double
>
coeffs
(
nterms
);
std
::
vector
<
double
>
grad
(
nterms
);
for
(
unsigned
i
=
0
;
i
<
nterms
;
++
i
)
coeffs
[
i
]
=
test_rng
();
reg
.
setRegressionBox
(
BoxND
<
double
>::
unitBox
(
KDDIM
));
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
reg
.
getGradient
(
&
grad
[
0
],
grad
.
size
());
double
gEps
=
1.e-7
;
for
(
unsigned
i
=
0
;
i
<
nterms
;
++
i
)
{
const
double
center
=
coeffs
[
i
];
volatile
const
double
plus
=
center
+
gEps
;
volatile
const
double
minus
=
center
-
gEps
;
coeffs
[
i
]
=
plus
;
const
double
vp
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
coeffs
[
i
]
=
minus
;
const
double
vm
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
coeffs
[
i
]
=
center
;
const
double
numgrad
=
(
vp
-
vm
)
/
(
plus
-
minus
);
CHECK_CLOSE
(
numgrad
,
grad
[
i
],
1.e-4
);
}
// Check the gradient calculations for some crazy
// values of the coefficients
gEps
=
1.e13
;
for
(
int
j
=-
1
;
j
<
2
;
j
+=
2
)
{
for
(
unsigned
i
=
0
;
i
<
nterms
;
++
i
)
{
const
double
oldval
=
coeffs
[
i
];
coeffs
[
i
]
=
j
*
1.e20
;
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
reg
.
getGradient
(
&
grad
[
0
],
grad
.
size
());
volatile
const
double
plus
=
coeffs
[
i
]
+
gEps
;
volatile
const
double
minus
=
coeffs
[
i
]
-
gEps
;
coeffs
[
i
]
=
plus
;
const
double
vp
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
coeffs
[
i
]
=
minus
;
const
double
vm
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
const
double
numgrad
=
(
vp
-
vm
)
/
(
plus
-
minus
);
CHECK_CLOSE
(
numgrad
,
grad
[
i
],
1.e-5
);
coeffs
[
i
]
=
oldval
;
}
}
}
TEST
(
LogisticRegressionOnGrid
)
{
// const double eps = 1.e-8;
const
unsigned
idim
=
KDDIM
;
// Generate a random box
BoxND
<
double
>
box
(
idim
);
for
(
unsigned
i
=
0
;
i
<
idim
;
++
i
)
box
[
i
].
setBounds
(
test_rng
(),
1.0
+
test_rng
());
// Generate a random array shape
ArrayShape
shape
(
idim
);
for
(
unsigned
i
=
0
;
i
<
idim
;
++
i
)
shape
[
i
]
=
static_cast
<
unsigned
>
(
3
+
5
*
test_rng
());
// Generate a random weight array
ArrayND
<
double
>
w
(
shape
);
const
unsigned
long
arrlen
=
w
.
length
();
for
(
unsigned
long
i
=
0
;
i
<
arrlen
;
++
i
)
w
.
linearValue
(
i
)
=
test_rng
();
// Generate a density out of this data
std
::
vector
<
double
>
location
(
idim
);
std
::
vector
<
double
>
scale
(
idim
);
for
(
unsigned
i
=
0
;
i
<
idim
;
++
i
)
{
location
[
i
]
=
box
[
i
].
min
();
scale
[
i
]
=
box
[
i
].
length
();
}
BinnedDensityND
dens
(
&
location
[
0
],
&
scale
[
0
],
idim
,
w
,
0
);
// Build the quadratic orthogonal polynomials out of it
QuadraticOrthoPolyND
poly
(
dens
,
box
,
&
shape
[
0
],
shape
.
size
());
// Generate the denominator array
ArrayND
<
int
>
denom
(
shape
);
const
unsigned
len
=
denom
.
length
();
for
(
unsigned
i
=
0
;
i
<
len
;
++
i
)
denom
.
linearValue
(
i
)
=
static_cast
<
int
>
(
3
+
15
*
test_rng
());
// Generate the numerator array
ArrayND
<
int
>
num
(
shape
);
for
(
unsigned
i
=
0
;
i
<
len
;
++
i
)
num
.
linearValue
(
i
)
=
static_cast
<
int
>
(
test_rng
()
*
denom
.
linearValue
(
i
));
// Generate the logistic regression object
LogisticRegressionOnGrid
<
int
>
reg
(
num
,
denom
,
poly
,
true
);
// Check the gradient calculations by this object
const
unsigned
nterms
=
poly
.
nTerms
();
std
::
vector
<
double
>
coeffs
(
nterms
);
std
::
vector
<
double
>
grad
(
nterms
);
for
(
unsigned
i
=
0
;
i
<
nterms
;
++
i
)
coeffs
[
i
]
=
test_rng
();
reg
.
setRegressionBox
(
BoxND
<
int
>
(
shape
));
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
reg
.
getGradient
(
&
grad
[
0
],
grad
.
size
());
double
gEps
=
1.e-7
;
for
(
unsigned
i
=
0
;
i
<
nterms
;
++
i
)
{
const
double
center
=
coeffs
[
i
];
volatile
const
double
plus
=
center
+
gEps
;
volatile
const
double
minus
=
center
-
gEps
;
coeffs
[
i
]
=
plus
;
const
double
vp
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
coeffs
[
i
]
=
minus
;
const
double
vm
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
coeffs
[
i
]
=
center
;
const
double
numgrad
=
(
vp
-
vm
)
/
(
plus
-
minus
);
CHECK_CLOSE
(
0.0
,
(
numgrad
-
grad
[
i
])
/
(
fabs
(
numgrad
)
+
fabs
(
grad
[
i
])),
2.e-6
);
}
// Check the gradient calculations for some crazy
// values of the coefficients
gEps
=
1.e13
;
for
(
int
j
=-
1
;
j
<
2
;
j
+=
2
)
{
for
(
unsigned
i
=
0
;
i
<
nterms
;
++
i
)
{
const
double
oldval
=
coeffs
[
i
];
coeffs
[
i
]
=
j
*
1.e20
;
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
reg
.
getGradient
(
&
grad
[
0
],
grad
.
size
());
volatile
const
double
plus
=
coeffs
[
i
]
+
gEps
;
volatile
const
double
minus
=
coeffs
[
i
]
-
gEps
;
coeffs
[
i
]
=
plus
;
const
double
vp
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
coeffs
[
i
]
=
minus
;
const
double
vm
=
reg
.
calculateLogLikelihood
(
&
coeffs
[
0
],
nterms
);
const
double
numgrad
=
(
vp
-
vm
)
/
(
plus
-
minus
);
CHECK_CLOSE
(
0.0
,
(
numgrad
-
grad
[
i
])
/
(
fabs
(
numgrad
)
+
fabs
(
grad
[
i
])),
1.e-6
);
coeffs
[
i
]
=
oldval
;
}
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Wed, May 14, 10:10 AM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5041499
Default Alt Text
test_LocalLogisticRegression.cc (7 KB)
Attached To
rNPSTATSVN npstatsvn
Event Timeline
Log In to Comment