File: gengraph_powerlaw.h

package info (click to toggle)
r-cran-igraph 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,984 kB
  • sloc: ansic: 117,319; cpp: 22,287; fortran: 4,551; yacc: 1,150; tcl: 931; lex: 478; makefile: 149; sh: 9
file content (84 lines) | stat: -rw-r--r-- 2,918 bytes parent folder | download | duplicates (8)
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
/*
 *
 * gengraph - generation of random simple connected graphs with prescribed
 *            degree sequence
 *
 * Copyright (C) 2006  Fabien Viger
 *
 * This program 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef _POWERLAW_H
#define _POWERLAW_H

// pascalou
#ifndef pascalou
#include "gengraph_definitions.h"
#endif

// Discrete integer power-law : P(X=min+k) is proportionnal to (k+k0)^-alpha
// - possibility to determine a range [Min, Max] of possible samples
// - possibility to automatically compute k0 to obtain a given mean z

namespace gengraph {

#define POWERLAW_TABLE 10000

class powerlaw {
private:
  double alpha;  // Exponent
  int mini; // Minimum sample
  int maxi; // Maximum sample
  double offset; // Offset
  int tabulated; // Number of values to tabulate
  int *table;    // Table containing cumulative distribution for k=mini..mini+tabulated-1
  int *dt;        // Table delimiters
  int max_dt;     // number of delimiters - 1
  double proba_big;   // Probability to take a non-tabulated value
  double table_mul;   // equal to (1-proba_big)/(RAND_MAX+1)

  // Sample a non-tabulated value >= mini+tabulated
  inline double big_sample(double randomfloat) {
    return double(mini)+pow(_a * randomfloat + _b, _exp)-offset;
  }
  inline double big_inv_sample(double s) {
    return (pow(s-double(mini)+offset,1.0/_exp)-_b)/_a;
  }
  double _exp, _a, _b; // Cached values used by big_sample();

  // Dichotomic adjust of offset, so that to_adjust() returns value with
  // a precision of eps. Note that to_adjust() must be an increasing function of offset.
  void adjust_offset_mean(double value, double eps, double fac);

public:
  int sample();      // Return a random integer
  double proba(int); // Return probability to return integer
  double error();    // Returns relative numerical error done by this class
  double mean();     // Returns mean of the sampler
  int median();      // Returns median of the sampler

  // Initialize the power-law sampler.
  void init_to_offset(double, int);
  // Same, but also returns the offset found
  double init_to_mean(double);
  double init_to_median(double);

  inline void init() { init_to_offset(double(mini),POWERLAW_TABLE); };

  ~powerlaw();
  powerlaw(double exponent, int mini, int maxi=-1);
};

} // namespace gengraph

#endif //_POWERLAW_H