-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpowerlawCommon.h
303 lines (257 loc) · 6.64 KB
/
powerlawCommon.h
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#ifndef __powerlawCommon_h__
#define __powerlawCommon_h__
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <vector>
#include <iterator>
#include <functional>
#include <numeric>
#include <boost/detail/algorithm.hpp>
#include <boost/math/special_functions/zeta.hpp>
#include <boost/math/distributions/chi_squared.hpp>
#include <boost/accumulators/numeric/functional/vector.hpp>
#include <boost/accumulators/numeric/functional/complex.hpp>
#include <boost/accumulators/numeric/functional/valarray.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random.hpp>
/**
* @author: W.M. Otte (wim@invivonmr.uu.nl); Image Sciences Institute, UMC Utrecht, NL.
* @date: 19-11-2009
*
* Function definitions of powerlaw scaling parameter estimation.
*
* ***************************************************************************
* Method: "Power-law distributions in empirical data", Clauset et al, 2009
* http://www.santafe.edu/~aaronc/powerlaws/
* ***************************************************************************
*/
namespace graph
{
/**
* STL Predicate: find floating number.
*/
template< class T >
struct floating_point: public std::unary_function< T, bool >
{
bool operator()( const T& x ) const
{
return !( floor( x ) == x );
}
};
/**
* STL helper: print number.
*/
template< class T >
struct print: public std::unary_function< T, void >
{
void operator()( const T& x ) const
{
std::cout << x << std::endl;
}
};
/**
* STL helper: log( x / y ).
*/
template< class T >
struct log_div: public std::binary_function< T, T, T >
{
T operator()( const T& x, const T& y ) const
{
return std::log( x / y );
}
};
/**
* STL helper: log( x ).
*/
template< class T >
struct log: public std::unary_function< T, T >
{
T operator()( const T& x ) const
{
return std::log( x );
}
};
/**
* STL helper: 1 - x^N.
*/
template< class T >
struct power_minus_one: public std::binary_function< T, T, T >
{
T operator()( const T& x, const T& N ) const
{
return 1.0f - pow( x, N );
}
};
/**
* STL helper: x^N.
*/
template< class T >
struct power: public std::binary_function< T, T, T >
{
T operator()( const T& x, const T& N ) const
{
return pow( x, N );
}
};
/**
* Boost library zeta function.
*/
template< class T >
struct zeta: public std::unary_function< T, T >
{
T operator()( const T& x ) const
{
return boost::math::zeta< T >( x );
}
};
/**
* abs.
*/
template< class T >
struct abs: public std::unary_function< T, T >
{
T operator()( const T& x ) const
{
return static_cast< T >( std::fabs( x ) );
}
};
// *********************************************************
// POWERLAW
// *********************************************************
/**
*
*/
template< class ValueType >
class Powerlaw
{
public:
typedef long IntegerType;
typedef std::vector< ValueType > VectorType;
typedef boost::mt19937 random_number_type;
typedef boost::uniform_real< ValueType > real_distribution_type;
typedef boost::uniform_int< IntegerType > int_distribution_type;
typedef boost::variate_generator< random_number_type&, real_distribution_type > real_generator_type;
typedef boost::variate_generator< random_number_type&, int_distribution_type > int_generator_type;
/**
*
*/
static void SingleFit( const VectorType& input, VectorType& results, bool nosmall, bool finiteSize, float startValue,
float incrementValue, float endValue );
/**
*
*/
static void BootstrapFit( const VectorType& input, VectorType& results, bool nosmall, bool finiteSize, ValueType startValue,
ValueType incrementValue, ValueType endValue, unsigned int bootstrapIterations, bool verbose );
protected:
/**
*
*/
static bool IsDiscrete( const VectorType& V );
/**
*
*/
static void Unique( const VectorType& V, VectorType& results );
/**
*
*/
static void RemoveLastElement( VectorType& V );
/**
*
*/
static void Sort( const VectorType& V, VectorType& W );
/**
*
*/
static void KeepLowerOrEqual( VectorType& V, ValueType x );
/**
*
*/
static void KeepHigherOrEqual( VectorType& V, ValueType x );
/**
*
*/
static void GetIncrementVector( ValueType s, ValueType i, ValueType e, VectorType& V );
/**
*
*/
static ValueType GetSD( const std::vector< ValueType >& V );
/**
*
*/
static void CumulativeSum( const VectorType& V, VectorType& W );
/**
*
*/
static void GetRandomValue( const VectorType& inputs,
random_number_type& generator, VectorType& results );
/**
*
*/
static void MleInt( const VectorType& x, bool nosmall, bool finiteSize,
ValueType startValue, ValueType increment, ValueType endValue, VectorType& results );
/**
*
*/
static void MleReal( const VectorType& x, bool nosmall, bool finiteSize,
VectorType& results );
/**
*
*/
static void Bootstrap( const VectorType& inputs, bool nosmall, bool finiteSize,
ValueType startValue, ValueType increment, ValueType endValue,
bool discrete, unsigned int n, VectorType& results, bool verbose );
/**
*
*/
static void Mle( const VectorType& inputs, bool nosmall, bool finiteSize,
ValueType startValue, ValueType increment,
ValueType endValue, bool discrete, VectorType& results );
};
/**
* Simple histogram implementation.
*/
template< class ValueType >
class Histogram
{
private:
std::vector< ValueType > histogram;
public:
/**
* Construct histogram.
*/
Histogram( const std::vector< ValueType >& data, ValueType low, ValueType high, unsigned int bins, bool normalize )
{
histogram.assign( bins + 1, 0 ); // plus outlier bin...
ValueType width = ( high - low ) / static_cast< ValueType >( bins );
for ( unsigned int i = 0; i < data.size(); i++ )
{
unsigned int bin = static_cast< unsigned int >( ( data[i] - low ) / width );
if ( bin < bins )
histogram[bin]++;
else if ( bin >= bins ) // insert outliers in highest bin...
histogram[bins]++;
}
if ( normalize )
std::transform( histogram.begin(), histogram.end(), histogram.begin(), std::bind2nd( std::divides< ValueType >(),
data.size() ) );
}
/**
* Return histogram.
*/
std::vector< ValueType > getHistogram()
{
return histogram;
}
};
} // end namespace graph
#endif /*__powerlawCommon_h__*/