Debian Codegraph @

tests/benchmarks/igraph_power_law_fit.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 igraph library.
3 Copyright (C) 2024 The igraph development team <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19 #include <igraph.h>cxx . . $ `<file>/include/igraph.h`/
•20 #include <plfit/plfit_sampling.h>
21
22 #include "bench.h"cxx . . $ `<file>/tests/benchmarks/bench.h`/
23
24 igraph_vector_t datacxx . . $ data.;
25
26 double rparetocxx . . $ rpareto(d8e7e0733fa41c87).(double xmin, double alpha) {
27 /* 1-u is used in the base here because we want to avoid the case of
28 * sampling zero */
29 return powcxx . . $ pow(d8e7e0733fa41c87).(1 - RNG_UNIF01cxx . . $ `include/igraph_random.h:155:9`!(), -1.0 / alpha) * xmin;
30 }
31
32 double rzetacxx . . $ rzeta(9976ed6c9a6c4849).(igraph_int_tcxx . . $ igraph_int_t# xmin, double alpha) {
33 double u, v, t;
34 igraph_int_tcxx . . $ igraph_int_t# x;
35 double alpha_minus_1 = alpha-1;
36 double minus_1_over_alpha_minus_1 = -1.0 / (alpha-1);
37 double b;
38 double one_over_b_minus_1;
39
40 xmin = (igraph_int_tcxx . . $ igraph_int_t#) roundcxx . . $ round(447c67c6676f75c3).(xmin);
41
42 /* Rejection sampling for the win. We use Y=floor(U^{-1/alpha} * xmin) as the
43 * envelope distribution, similarly to Chapter X.6 of Luc Devroye's book
44 * (where xmin is assumed to be 1): http://luc.devroye.org/chapter_ten.pdf
45 *
46 * Some notes that should help me recover what I was doing:
47 *
48 * p_i = 1/zeta(alpha, xmin) * i^-alpha
49 * q_i = (xmin/i)^{alpha-1} - (xmin/(i+1))^{alpha-1}
50 * = (i/xmin)^{1-alpha} - ((i+1)/xmin)^{1-alpha}
51 * = [i^{1-alpha} - (i+1)^{1-alpha}] / xmin^{1-alpha}
52 *
53 * p_i / q_i attains its maximum at xmin=i, so the rejection constant is:
54 *
55 * c = p_xmin / q_xmin
56 *
57 * We have to accept the sample if V <= (p_i / q_i) * (q_xmin / p_xmin) =
58 * (i/xmin)^-alpha * [xmin^{1-alpha} - (xmin+1)^{1-alpha}] / [i^{1-alpha} - (i+1)^{1-alpha}] =
59 * [xmin - xmin^alpha / (xmin+1)^{alpha-1}] / [i - i^alpha / (i+1)^{alpha-1}] =
60 * xmin/i * [1-(xmin/(xmin+1))^{alpha-1}]/[1-(i/(i+1))^{alpha-1}]
61 *
62 * In other words (and substituting i with X, which is the same),
63 *
64 * V * (X/xmin) <= [1 - (1+1/xmin)^{1-alpha}] / [1 - (1+1/i)^{1-alpha}]
65 *
66 * Let b := (1+1/xmin)^{alpha-1} and let T := (1+1/i)^{alpha-1}. Then:
67 *
68 * V * (X/xmin) <= [(b-1)/b] / [(T-1)/T]
69 * V * (X/xmin) * (T-1) / (b-1) <= T / b
70 *
71 * which is the same as in Devroye's book, except for the X/xmin term, and
72 * the definition of b.
73 */
74 b = powcxx . . $ pow(d8e7e0733fa41c87).(1 + 1.0/xmin, alpha_minus_1);
75 one_over_b_minus_1 = 1.0/(b-1);
76 do {
77 do {
78 u = RNG_UNIF01cxx . . $ `include/igraph_random.h:155:9`!();
79 v = RNG_UNIF01cxx . . $ `include/igraph_random.h:155:9`!();
80 /* 1-u is used in the base here because we want to avoid the case of
81 * having zero in x */
82 x = (igraph_int_tcxx . . $ igraph_int_t#) floorcxx . . $ floor(447c67c6676f75c3).(powcxx . . $ pow(d8e7e0733fa41c87).(1-u, minus_1_over_alpha_minus_1) * xmin);
83 } while (x < xmin);
84 t = powcxx . . $ pow(d8e7e0733fa41c87).((x+1.0)/x, alpha_minus_1);
85 } while (v*x*(t-1)*one_over_b_minus_1*b > t*xmin);
86
87 return x;
88 }
89
90 int generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(double xmin, double alpha, size_t num_samples) {
91 IGRAPH_CHECKcxx . . $ `include/igraph_error.h:655:9`!(igraph_vector_resize(&data, num_samples));
92
93 for (size_t i = 0; i < num_samples; i++) {
94 VECTORcxx . . $ `/build/tmp.EEZvKkyHCW/package/include/igraph_vector_pmt.h:60:9`!(data)[i] = rparetocxx . . $ rpareto(d8e7e0733fa41c87).(xmin, alpha);
95 }
96
97 return IGRAPH_SUCCESScxx . . $ IGRAPH_SUCCESS.;
98 }
99
100 int generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(double xmin, double alpha, size_t num_samples) {
101 IGRAPH_CHECKcxx . . $ `include/igraph_error.h:655:9`!(igraph_vector_resize(&data, num_samples));
102
103 for (size_t i = 0; i < num_samples; i++) {
104 VECTORcxx . . $ `/build/tmp.EEZvKkyHCW/package/include/igraph_vector_pmt.h:60:9`!(data)[i] = rzetacxx . . $ rzeta(9976ed6c9a6c4849).(xmin, alpha);
105 }
106
107 return IGRAPH_SUCCESScxx . . $ IGRAPH_SUCCESS.;
108 }
109
110 int fit_continuouscxx . . $ fit_continuous(620b9ac44d2573a6).(double known_xmin) {
111 igraph_plfit_result_tcxx . . $ igraph_plfit_result_t# result;
112 IGRAPH_CHECKcxx . . $ `include/igraph_error.h:655:9`!(igraph_power_law_fit(&data, &result, known_xmin, /* force_continuous = */ 1));
113 return IGRAPH_SUCCESScxx . . $ IGRAPH_SUCCESS.;
114 }
115
116 int fit_discretecxx . . $ fit_discrete(620b9ac44d2573a6).(double known_xmin) {
117 igraph_plfit_result_tcxx . . $ igraph_plfit_result_t# result;
118 IGRAPH_CHECKcxx . . $ `include/igraph_error.h:655:9`!(igraph_power_law_fit(&data, &result, known_xmin, 0));
119 return IGRAPH_SUCCESScxx . . $ IGRAPH_SUCCESS.;
120 }
121
122 int maincxx . . $ main(b126dc7c1de90089).(void) {
123 igraph_rng_seedcxx . . $ igraph_rng_seed(485eb74070911893).(igraph_rng_defaultcxx . . $ igraph_rng_default(f4ecc4061b2467f1).(), 42);
124
125 igraph_vector_init(&datacxx . . $ data., 0);
126
127 BENCH_INITcxx . . $ `tests/benchmarks/bench.h:37:9`!();
128
129 generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(1, 3, 100000);
130 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 1 Continuous, xmin = 1, alpha = 3, samples = 100K, fitting alpha only", fit_continuous(1));
131
132 generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(1, 3, 200000);
133 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 2 Continuous, xmin = 1, alpha = 3, samples = 200K, fitting alpha only", fit_continuous(1));
134
135 generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(1, 3, 500000);
136 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 3 Continuous, xmin = 1, alpha = 3, samples = 500K, fitting alpha only", fit_continuous(1));
137
138 generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(1, 3, 5000);
139 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 4 Continuous, xmin = 1, alpha = 3, samples = 5K, fitting xmin and alpha", fit_continuous(-1));
140
141 generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(1, 3, 10000);
142 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 5 Continuous, xmin = 1, alpha = 3, samples = 10K, fitting xmin and alpha", fit_continuous(-1));
143
144 generate_continuouscxx . . $ generate_continuous(78bec9541aede43e).(1, 3, 15000);
145 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 6 Continuous, xmin = 1, alpha = 3, samples = 15K, fitting xmin and alpha", fit_continuous(-1));
146
147 generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(3, 3, 1000000);
148 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 7 Discrete, xmin = 3, alpha = 3, samples = 1M, fitting alpha only", fit_discrete(3));
149
150 generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(3, 3, 5000000);
151 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 8 Discrete, xmin = 3, alpha = 3, samples = 5M, fitting alpha only", fit_discrete(3));
152
153 generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(3, 3, 10000000);
154 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!(" 9 Discrete, xmin = 3, alpha = 3, samples = 10M, fitting alpha only", fit_discrete(3));
155
156 generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(3, 3, 1000000);
157 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!("10 Discrete, xmin = 3, alpha = 3, samples = 1M, fitting xmin and alpha", fit_discrete(-1));
158
159 generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(3, 3, 5000000);
160 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!("11 Discrete, xmin = 3, alpha = 3, samples = 5M, fitting xmin and alpha", fit_discrete(-1));
161
162 generate_discretecxx . . $ generate_discrete(78bec9541aede43e).(3, 3, 10000000);
163 BENCHcxx . . $ `tests/benchmarks/bench.h:49:9`!("12 Discrete, xmin = 3, alpha = 3, samples = 10M, fitting xmin and alpha", fit_discrete(-1));
164
165 igraph_vector_destroy(&datacxx . . $ data.);
166
167 return 0;
168 }