├── .abapgit.xml ├── LICENSE ├── README.md ├── img ├── ecdf.png ├── gauss.png ├── hist.png └── stat_abap.png └── src ├── package.devc.xml ├── zcx_tbox_stats.clas.abap ├── zcx_tbox_stats.clas.xml ├── ztbox_cl_stats.clas.abap ├── ztbox_cl_stats.clas.xml ├── ztbox_cl_stats_apack.clas.abap ├── ztbox_cl_stats_apack.clas.xml ├── ztbox_cl_stats_group.clas.abap └── ztbox_cl_stats_group.clas.xml /.abapgit.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | E 6 | /src/ 7 | PREFIX 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Marco Marrone 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ABAP Statistical Tools 2 | 3 |

4 | 5 |

6 | 7 | Statistics with ABAP: why not? This project consist of an ABAP class `ztbox_cl_stats` where some of the most common descriptive statistics functions have been included together with simple tools to generate distributions and produce empirical inference analyses. 8 | 9 | ## Basic Features & Elementary Statistics 10 | Let's compute some statistics on `SBOOK` table 11 | ```abap 12 | SELECT * FROM sbook INTO TABLE @DATA(T_SBOOK). 13 | 14 | DATA(stats) = NEW ztbox_cl_stats( t_sbook ). 15 | ``` 16 | 17 | Use `->col( )` method to select a column on which make calculations 18 | 19 | ```abap 20 | DATA(prices) = stats->col( `LOCCURAM` ). 21 | ``` 22 | 23 | Each statistic has its own method 24 | 25 | ```abap 26 | * The smallest value 27 | DATA(min) = prices->min( ). " [148.00] 28 | 29 | * The largest value 30 | DATA(max) = prices->max( ). " [6960.12] 31 | 32 | * The range, i.e. the difference between largest and smallest values 33 | DATA(range) = prices->range( ). " [6812.12] 34 | 35 | * The sum of the values 36 | DATA(tot) = prices->sum( ). " [25055655.41] 37 | 38 | * The sample mean of the values 39 | DATA(mean) = prices->mean( ). " [922.96] 40 | 41 | * The mean absolute deviation (MAD) from the mean 42 | DATA(mad_mean) = prices->mad_mean( ). " [480.41] 43 | 44 | * The sample median of the values 45 | DATA(median) = prices->median( ). " [670.34] 46 | 47 | * The mean absolute deviation (MAD) from the median 48 | DATA(mad_median) = prices->mad_median( ). " [436.36] 49 | 50 | * The sample variance of the values 51 | DATA(variance) = prices->variance( ). " [572404.48] 52 | 53 | * The sample standard deviation of the values 54 | DATA(std_dev) = prices->standard_deviation( ). " [756.57] 55 | 56 | * The coefficent of variation, ratio of the standard deviation to the mean 57 | DATA(coeff_var) = prices->coefficient_variation( ). " [0.819] 58 | 59 | * The dispersion index, ratio of the variance to the mean 60 | DATA(disp_index) = prices->dispersion_index( ). " [620.18] 61 | 62 | * The number of distinct values 63 | DATA(dist_val) = prices->count_distinct( ). " [324] 64 | 65 | * The number of not initial values 66 | DATA(not_init) = prices->count_not_initial( ). " [27147] 67 | ``` 68 | 69 | Alternatively, you can use the main instance, which represents the entire table, passing the name of the relevant column: 70 | 71 | ```abap 72 | DATA(min_price) = stats->min( `LOCCURAM` ). 73 | ``` 74 | 75 | ## More specific descriptive statistics 76 | 77 | ### Quartiles 78 | 25% of the data is below the *first quartile* $Q1$ 79 | 80 | ```abap 81 | DATA(first_quartile) = prices->first_quartile( ). " [566.10] 82 | ``` 83 | 84 | 50% of the data is below the *second quartile* or *median* $Q2$ 85 | 86 | ```abap 87 | DATA(second_quartile) = prices->second_quartile( ). " [670.34] 88 | DATA(median) = prices->median( ). " It's just a synonym for second_quartile( ) 89 | ``` 90 | 91 | 75% of the data is below the *third quartile* $Q3$ 92 | 93 | ```abap 94 | DATA(third_quartile) = prices->third_quartile( ). " [978.50] 95 | ``` 96 | 97 | The difference between third and first quartile is called *interquartile range* $\mathrm{IQR} = Q3 - Q1$, and it is a measure of spread of the data 98 | 99 | ```abap 100 | DATA(iqr) = prices->interquartile_range( ). " [412.40] 101 | ``` 102 | 103 | A value outside the range $\left[Q1 - 1.5\mathrm{IQR},\ Q3 + 1.5\mathrm{IQR}\right]$ can be considered an *outlier* 104 | ```abap 105 | DATA(outliers) = prices->outliers( ). " Found 94 outliers, from 1638.36 to 6960.12 106 | ``` 107 | 108 | ### Means 109 | 110 | Harmonic Mean is $\frac{n}{\frac{1}{x_1}\+\ \ldots\ +\ \frac{1}{x_n}}$, used often in averaging rates 111 | 112 | ```abap 113 | DATA(hmean) = prices->harmonic_mean( ). " [586.17] 114 | ``` 115 | 116 | Geometric Mean is $\sqrt[n]{x_1\cdot \ldots \cdot x_n}$, used for population growth or interest rates 117 | 118 | ```abap 119 | DATA(gmean) = prices->geometric_mean( ). " [731.17] 120 | ``` 121 | 122 | Quadratic Mean is $\sqrt{\frac{x_1^2\ +\ \ldots\ +\ x_n^2}{n}}$, used, among other things, to measure the fit of an estimator to a data set 123 | 124 | ```abap 125 | DATA(qmean) = prices->quadratic_mean( ). " [1193.42] 126 | 127 | * The values calculated so far confirm the HM-GM-AM-QM inequalities 128 | * harmonic mean <= geometric mean <= arithmetic mean <= quadratic mean 129 | ``` 130 | 131 | ### Moments 132 | 133 | *Skewness* is a measure of the asymmetry of the distribution of a real random value about its mean. We estimate it with a sample skewness computed with the adjusted Fisher-Pearson standardized moment coefficient (the same used by Excel). 134 | 135 | $$\mathrm{skewness} = \frac{n}{(n-1)(n-2)}\frac{\sum\limits_{i=1}^n {(x_i - \bar{x})}^3}{\left[\frac{1}{n-1}\sum\limits_{i=1}^{n} (x_i - \bar{x})^2 \right]^{3/2}}$$ 136 | 137 | ```abap 138 | DATA(skewness) = prices->skenewss( ). " [3.19] 139 | * positive skewness: right tail is longer, the mass of the distribution is concentrated on the left 140 | ``` 141 | 142 | *Kurtosis* is a measure of the tailedness of the distribution of a real random value: higher kurtosis corresponds to greater extremity of outliers 143 | 144 | $$\mathrm{kurtosis} = \frac{1}{(n-1)}\frac{\sum\limits_{i=1}^n {(x_i - \bar{x})}^4}{\left[\frac{1}{n-1}\sum\limits_{i=1}^{n} (x_i - \bar{x})^2 \right]^2}$$ 145 | 146 | ```abap 147 | DATA(kurtosis) = prices->kurtosis( ). " [19.18] 148 | * positive excess kurtosis (kurtosis minus 3): leptokurtic distribution with fatter tails 149 | ``` 150 | 151 | ## Empirical Inference 152 | 153 | The *histogram* is a table of couples $(\mathrm{bin}_i, \mathrm{f}_i)$ where $\mathrm{bin}_i$ is the first endpoint of the $i$-th *bin*, i.e. the interval with which the values were partitioned, and $\mathrm{f}_i$ is the $i$-th frequency, i.e. the number of values inside the $i$-th bin. 154 | 155 | ```abap 156 | DATA(histogram) = prices->histogram( ). 157 | ``` 158 | 159 |

160 | 161 |

162 | 163 | The bins are created using *Freedman-Diaconis rule*: the bins width is $\frac{2\mathrm{iqr}}{\sqrt[3]{n}}$ where $\mathrm{iqr}$ is the interquartile range, and the total number of bins is $\mathrm{floor}\left(\frac{\mathrm{max} - \mathrm{min}}{\mathrm{bin\ width}}\right)$ 164 | 165 | Dividing each frequency by the total we get an estimate of the probability to draw a value in the corresponding bin, this is the *empirical probability* 166 | 167 | ```abap 168 | DATA(empirical_prob) = prices->empirical_pdf( ). 169 | ``` 170 | 171 | Similarly, for each distinct value $x$, we can compute the number $\frac{\mathrm{number\ of\ elements}\ \le\ x}{n}$, this is the *empirical distribution function* 172 | 173 | ```abap 174 | DATA(empirical_dist) = prices->empirical_cdf( ). 175 | ``` 176 |

177 | 178 |

179 | 180 | In order to answer the question "are the values normally distributed?" you can use method `->are_normal( )` 181 | 182 | ```abap 183 | DATA(normality_test) = prices->are_normal( ) " [abap_false]. 184 | ``` 185 | 186 | This method implements the [Jarque-Bera normality test](https://en.wikipedia.org/wiki/Jarque%E2%80%93Bera_test). The $p$-value is an exported parameter and the test is considered passed if $p\mathrm{-value} > \alpha$ where $\alpha = 0.5$ by default (it's an optional parameter). 187 | 188 | ## Distributions 189 | 190 | The following are static methods to generate samples from various distributions 191 | 192 | ```abap 193 | " Continuous Uniform Distribution 194 | DATA(uniform_values) = ztbox_cl_stats=>uniform( low = 1 high = 50 size = 10000 ). 195 | " Generate a sample of 10000 values from a uniform distribution in the interval [1, 50] 196 | " default is =>uniform( low = 0 high = 1 size = 1 ) 197 | 198 | " Continuous Normal Distribution 199 | DATA(normal_values) = ztbox_cl_stats=>normal( mean = `-3` variance = 13 size = 1000 ). 200 | " Generate a sample of 1000 values from a normal distribution with mean = -3 and variance 13 201 | " default is =>normal( mean = 0 variance = 1 size = 1 ) 202 | 203 | " Continuous Standard Distribution 204 | DATA(standard_values) = ztbox_cl_stats=>standard( size = 100 ). 205 | " Generate a sample of 100 values from a standard distribution, i.e. a normal distribution 206 | " with mean = 0 and variance = 1 207 | " default is =>normal( size = 1 ) 208 | 209 | " Discrete Bernoulli Distribution 210 | DATA(bernoulli_values) = ztbox_cl_stats=>bernoulli( p = `0.8` size = 100 ). 211 | " Generate a sample of 100 values from a bernoulli distribution with probability parameter = 0.8 212 | " default is =>bernoulli( p = `0.5` size = 1 ) 213 | 214 | " Discrete Binomial Distribution 215 | DATA(binomial_values) = ztbox_cl_stats=>binomial( n = 15 p = `0.4` size = 100 ). 216 | " Generate a sample of 100 values from a binomial distribution 217 | " with probability parameter = 0.4 and number of trials = 15 218 | " default is =>binomial( n = 2 p = `0.5` size = 1 ) 219 | 220 | " Discrete Geometric Distribution 221 | DATA(geometric_values) = ztbox_cl_stats=>geometric( p = `0.6` size = 100 ). 222 | " Generate a sample of 100 values from a geometric distribution with probability parameter = 0.6 223 | " default is =>geometric( p = `0.5` size = 1 ) 224 | 225 | " Discrete Poisson Distribution 226 | DATA(poisson_values) = ztbox_cl_stats=>poisson( l = 4 size = 100 ). 227 | " Generate a sample of 100 values from a poisson distribution with lambda parameter = 4 228 | " default is =>poisson( l = `1.0` size = 1 ) 229 | ``` 230 | 231 | Let's plot the *empirical probability density function* of a sample of 100000 values drawn from a generated standard normal distribution: 232 | 233 | ```abap 234 | DATA(gauss) = ztbox_cl_stats=>standard( size = 100000 ). 235 | DATA(gauss_stat) = NEW ztbox_cl_stats( gauss ). 236 | DATA(g_pdf) = gauss_stat->empirical_pdf( ). 237 | ``` 238 | 239 |

240 | 241 |

242 | 243 | yep! I recognize this shape. 244 | 245 | ## Feature Scaling 246 | In some cases can be useful to work with normalized data 247 | 248 | ```abap 249 | DATA(normalized_prices) = prices->normalize( ). 250 | " Each value is transformed subtracting the minimal value and dividing by the range (max - min) 251 | 252 | DATA(standardized_prices) = prices->standardize( ). 253 | " Each value is transformed subtracting the mean and dividing by the standard deviation 254 | ``` 255 | 256 | ## Joint Variability 257 | ### Covariance 258 | In order to compute the sample covariance of two columns call method `->covariance` passing the columns separated by comma 259 | 260 | ```abap 261 | DATA(stats) = NEW ztbox_cl_stats( t_sbook ). 262 | DATA(covariance) = stats->covariance( `LOCCURAM, LUGGWEIGHT` ). " [1037.40] 263 | ``` 264 | 265 | ### Correlation 266 | The sample correlation coefficient is computed by calling `->correlation` method 267 | ```abap 268 | DATA(stats) = NEW ztbox_cl_stats( t_sbook ). 269 | DATA(covariance) = stats->covariance( `LOCCURAM, LUGGWEIGHT` ). " [0.17] 270 | ``` 271 | 272 | ## Aggregations 273 | Each descriptive statistics explained so far can be calculated performing first a group-by with other columns 274 | 275 | ```abap 276 | DATA(stats) = NEW ztbox_cl_stats( sbook ). 277 | DATA(grouped_by_currency) = stats->group_by( `FORCURKEY` ). 278 | " You can also perform a group-by with multiple columns, just comma-separate them 279 | " e.g. stats->group_by( `FORCURKEY, SMOKER` ). 280 | DATA(prices_per_currency) = grouped_by_currency->col( `FORCURAM` ). 281 | DATA(dev_cur) = prices_per_currency->standard_deviation( ). 282 | ``` 283 | 284 | `dev_cur` is a table with two fields: the first one is a table with the group-by conditions (group-by field and value), the second one contains the statistics computed (standard deviation in this example). 285 | 286 | The same result can be obtained passing a table having the group-by fields and an additional field for the statistic 287 | 288 | ```abap 289 | TYPES: BEGIN OF ty_dev_cur, 290 | forcurkey TYPE sbook-forcurkey, 291 | price_std_dev TYPE f, 292 | END OF ty_dev_cur. 293 | 294 | DATA t_dev_cur TYPE TABLE OF ty_dev_cur. 295 | 296 | prices_per_currency->standard_deviation( IMPORTING e_result = t_dev_cur ). 297 | ``` 298 | 299 | | FORCURKEY | PRICE_STD_DEV | 300 | | :---: | :---: | 301 | | EUR | 5.1572747413790194E+02 | 302 | | USD | 4.5762828742456850E+02 | 303 | | GBP | 2.9501066968757806E+02 | 304 | | JPY | 5.2009995569407386E+02 | 305 | | CHF | 8.5376086718562442E+02 | 306 | | AUD | 3.9095624219014348E+02 | 307 | | ZAR | 4.3830708141667837E+03 | 308 | | SGD | 1.0340758423220680E+03 | 309 | | SEK | 4.4754710657225996E+03 | 310 | | CAD | 7.7769277990938747E+02 | 311 | 312 | # Contributions 313 | Many features can be improved or extended (new distribution generators? implementing statistic tests?) every contribution is appreciated 314 | 315 | # Installation 316 | Install this project using [abapGit](https://abapgit.org/) 317 | -------------------------------------------------------------------------------- /img/ecdf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zenrosadira/abap-tbox-stats/dfaf75cbe390b1deea58da63facbab7f2bb18941/img/ecdf.png -------------------------------------------------------------------------------- /img/gauss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zenrosadira/abap-tbox-stats/dfaf75cbe390b1deea58da63facbab7f2bb18941/img/gauss.png -------------------------------------------------------------------------------- /img/hist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zenrosadira/abap-tbox-stats/dfaf75cbe390b1deea58da63facbab7f2bb18941/img/hist.png -------------------------------------------------------------------------------- /img/stat_abap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zenrosadira/abap-tbox-stats/dfaf75cbe390b1deea58da63facbab7f2bb18941/img/stat_abap.png -------------------------------------------------------------------------------- /src/package.devc.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | TBOX | Statistical Utilities 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/zcx_tbox_stats.clas.abap: -------------------------------------------------------------------------------- 1 | class ZCX_TBOX_STATS definition 2 | public 3 | inheriting from CX_STATIC_CHECK 4 | create private 5 | 6 | global friends ZTBOX_CL_STATS 7 | ZTBOX_CL_STATS_GROUP . 8 | 9 | public section. 10 | 11 | constants TABLE_TYPE_NOT_SUPPORTED type SOTR_CONC value '00155D595A021EDDB5AEDAB6AD3443CB' ##NO_TEXT. 12 | constants COLUMN_NOT_NUMERICAL type SOTR_CONC value '00155D595A021EDDB5AF220CBC1D040A' ##NO_TEXT. 13 | constants COLUMN_GROUP_NOT_FOUND type SOTR_CONC value '00155D595A021EDDB5B0093DAD7D44C8' ##NO_TEXT. 14 | constants COLUMN_NOT_CATEGORICAL type SOTR_CONC value '00155D59578C1EEDB5C9B8AA49208260' ##NO_TEXT. 15 | constants COLUMN_NOT_FOUND type SOTR_CONC value '00155D59578C1EEDB5C9B9916D0A8260' ##NO_TEXT. 16 | constants P_NOT_VALID type SOTR_CONC value '00155D2A11B01EEDB6BA806E960B11FD' ##NO_TEXT. 17 | constants L_NOT_VALID type SOTR_CONC value '00155D2A11B01EEDB6BA806E960B31FD' ##NO_TEXT. 18 | constants S_NOT_VALID type SOTR_CONC value '00155D2A11B01EEDB6BA8220726E91FD' ##NO_TEXT. 19 | constants N_NOT_VALID type SOTR_CONC value '00155D2A11B01EEDB6BA8220726EB1FD' ##NO_TEXT. 20 | constants V_NOT_VALID type SOTR_CONC value '00155D2A11B01EEDB6BA92D70902D209' ##NO_TEXT. 21 | data COLUMN type NAME_FELD . 22 | 23 | methods CONSTRUCTOR 24 | importing 25 | !TEXTID like TEXTID optional 26 | !PREVIOUS like PREVIOUS optional 27 | !COLUMN type NAME_FELD optional . 28 | protected section. 29 | private section. 30 | ENDCLASS. 31 | 32 | 33 | 34 | CLASS ZCX_TBOX_STATS IMPLEMENTATION. 35 | 36 | 37 | method CONSTRUCTOR. 38 | CALL METHOD SUPER->CONSTRUCTOR 39 | EXPORTING 40 | TEXTID = TEXTID 41 | PREVIOUS = PREVIOUS 42 | . 43 | me->COLUMN = COLUMN . 44 | endmethod. 45 | ENDCLASS. 46 | -------------------------------------------------------------------------------- /src/zcx_tbox_stats.clas.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | ZCX_TBOX_STATS 7 | E 8 | TBOX | Stats Exceptions 9 | 40 10 | 1 11 | X 12 | X 13 | X 14 | 15 | 16 | 17 |
18 | 00155D2A11B01EEDB6BA806E960B11FD 19 | E 20 | 1 21 | CA== 22 |
23 | 24 | 25 | 00155D2A11B01EEDB6BA806E960B11FD 26 | E 27 | 0001 28 | X 29 | R 30 | 255 31 | Probability parameter must be between 0 and 1 32 | 33 | 34 |
35 | 36 |
37 | 00155D2A11B01EEDB6BA806E960B31FD 38 | E 39 | 1 40 | CA== 41 |
42 | 43 | 44 | 00155D2A11B01EEDB6BA806E960B31FD 45 | E 46 | 0001 47 | X 48 | R 49 | 255 50 | Poisson lambda parameter must be positive 51 | 52 | 53 |
54 | 55 |
56 | 00155D2A11B01EEDB6BA8220726E91FD 57 | E 58 | 1 59 | CA== 60 |
61 | 62 | 63 | 00155D2A11B01EEDB6BA8220726E91FD 64 | E 65 | 0001 66 | X 67 | R 68 | 255 69 | Size parameter must be a positive integer 70 | 71 | 72 |
73 | 74 |
75 | 00155D2A11B01EEDB6BA8220726EB1FD 76 | E 77 | 1 78 | CA== 79 |
80 | 81 | 82 | 00155D2A11B01EEDB6BA8220726EB1FD 83 | E 84 | 0001 85 | X 86 | R 87 | 255 88 | N parameter must be a positive integer 89 | 90 | 91 |
92 | 93 |
94 | 00155D2A11B01EEDB6BA92D70902D209 95 | E 96 | 1 97 | CA== 98 |
99 | 100 | 101 | 00155D2A11B01EEDB6BA92D70902D209 102 | E 103 | 0001 104 | X 105 | R 106 | 255 107 | Variance must a positive number 108 | 109 | 110 |
111 | 112 |
113 | 00155D59578C1EEDB5C9B8AA49208260 114 | E 115 | 1 116 | CA== 117 |
118 | 119 | 120 | 00155D59578C1EEDB5C9B8AA49208260 121 | E 122 | 0001 123 | X 124 | R 125 | 255 126 | Column must contain categorical values 127 | 128 | 129 |
130 | 131 |
132 | 00155D59578C1EEDB5C9B9916D0A8260 133 | E 134 | 1 135 | CA== 136 |
137 | 138 | 139 | 00155D59578C1EEDB5C9B9916D0A8260 140 | E 141 | 0001 142 | X 143 | R 144 | 255 145 | Column not found 146 | 147 | 148 |
149 | 150 |
151 | 00155D595A021EDDB5AEDAB6AD3443CB 152 | E 153 | 1 154 | CA== 155 |
156 | 157 | 158 | 00155D595A021EDDB5AEDAB6AD3443CB 159 | E 160 | 0001 161 | X 162 | R 163 | 255 164 | Table type not supported 165 | 166 | 167 |
168 | 169 |
170 | 00155D595A021EDDB5AF220CBC1D040A 171 | E 172 | 1 173 | CA== 174 |
175 | 176 | 177 | 00155D595A021EDDB5AF220CBC1D040A 178 | E 179 | 0001 180 | X 181 | R 182 | 255 183 | Column must contain numerical values 184 | 185 | 186 |
187 | 188 |
189 | 00155D595A021EDDB5B0093DAD7D44C8 190 | E 191 | 1 192 | CA== 193 |
194 | 195 | 196 | 00155D595A021EDDB5B0093DAD7D44C8 197 | E 198 | 0001 199 | X 200 | R 201 | 255 202 | Column &COLUMN& not found 203 | 204 | 205 |
206 |
207 | 208 | 209 | LIMU 210 | CPUB 211 | ZCX_TBOX_STATS 212 | 00155D2A11B01EEDB6BA806E960B11FD 213 | 0001 214 | 215 | 216 | LIMU 217 | CPUB 218 | ZCX_TBOX_STATS 219 | 00155D2A11B01EEDB6BA806E960B31FD 220 | 0001 221 | 222 | 223 | LIMU 224 | CPUB 225 | ZCX_TBOX_STATS 226 | 00155D2A11B01EEDB6BA8220726E91FD 227 | 0001 228 | 229 | 230 | LIMU 231 | CPUB 232 | ZCX_TBOX_STATS 233 | 00155D2A11B01EEDB6BA8220726EB1FD 234 | 0001 235 | 236 | 237 | LIMU 238 | CPUB 239 | ZCX_TBOX_STATS 240 | 00155D2A11B01EEDB6BA92D70902D209 241 | 0001 242 | 243 | 244 | LIMU 245 | CPUB 246 | ZCX_TBOX_STATS 247 | 00155D59578C1EEDB5C9B8AA49208260 248 | 0001 249 | 250 | 251 | LIMU 252 | CPUB 253 | ZCX_TBOX_STATS 254 | 00155D59578C1EEDB5C9B9916D0A8260 255 | 0001 256 | 257 | 258 | LIMU 259 | CPUB 260 | ZCX_TBOX_STATS 261 | 00155D595A021EDDB5AEDAB6AD3443CB 262 | 0001 263 | 264 | 265 | LIMU 266 | CPUB 267 | ZCX_TBOX_STATS 268 | 00155D595A021EDDB5AF220CBC1D040A 269 | 0001 270 | 271 | 272 | LIMU 273 | CPUB 274 | ZCX_TBOX_STATS 275 | 00155D595A021EDDB5B0093DAD7D44C8 276 | 0001 277 | 278 | 279 | 280 | 281 | LIMU 282 | CPUB 283 | ZCX_TBOX_STATS 284 | 0001 285 | 286 | 287 | 288 | 289 | COLUMN 290 | E 291 | Field name 292 | 293 | 294 | CONSTRUCTOR 295 | E 296 | CONSTRUCTOR 297 | 298 | 299 |
300 |
301 |
302 | -------------------------------------------------------------------------------- /src/ztbox_cl_stats.clas.abap: -------------------------------------------------------------------------------- 1 | ********************************************************************** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2023 Marco Marrone 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | ********************************************************************** 24 | class ZTBOX_CL_STATS definition 25 | public 26 | final 27 | create public . 28 | 29 | public section. 30 | 31 | types: 32 | ty_ints TYPE TABLE OF i WITH DEFAULT KEY . 33 | types: 34 | ty_floats TYPE TABLE OF f WITH DEFAULT KEY . 35 | types: 36 | BEGIN OF ty_values, 37 | x TYPE string, 38 | y TYPE string, 39 | END OF ty_values . 40 | types: 41 | ty_values_t TYPE TABLE OF ty_values WITH DEFAULT KEY . 42 | types: 43 | BEGIN OF ty_edist, 44 | x TYPE string, 45 | y TYPE f, 46 | END OF ty_edist . 47 | types: 48 | ty_edist_t TYPE TABLE OF ty_edist WITH DEFAULT KEY . 49 | types: 50 | BEGIN OF ty_bin, 51 | x TYPE f, 52 | y TYPE f, 53 | END OF ty_bin . 54 | types: 55 | ty_bins TYPE TABLE OF ty_bin WITH DEFAULT KEY . 56 | types: 57 | BEGIN OF ty_hist, 58 | x TYPE string, 59 | y TYPE i, 60 | END OF ty_hist . 61 | types: 62 | ty_hist_t TYPE TABLE OF ty_hist WITH DEFAULT KEY . 63 | 64 | class-methods CLASS_CONSTRUCTOR . 65 | methods CONSTRUCTOR 66 | importing 67 | !TABLE type ANY TABLE 68 | raising 69 | resumable(ZCX_TBOX_STATS) . 70 | methods STANDARDIZE 71 | importing 72 | !COL type NAME_FELD default `TABLE_LINE` 73 | returning 74 | value(R) type TY_FLOATS 75 | raising 76 | resumable(ZCX_TBOX_STATS) . 77 | methods NORMALIZE 78 | importing 79 | !COL type NAME_FELD default `TABLE_LINE` 80 | returning 81 | value(R) type TY_FLOATS 82 | raising 83 | resumable(ZCX_TBOX_STATS) . 84 | methods COUNT 85 | returning 86 | value(R) type I . 87 | methods COUNT_NOT_INITIAL 88 | importing 89 | !COL type NAME_FELD default `TABLE_LINE` 90 | returning 91 | value(R) type I 92 | raising 93 | resumable(ZCX_TBOX_STATS) . 94 | methods COUNT_DISTINCT 95 | importing 96 | !COL type NAME_FELD default `TABLE_LINE` 97 | returning 98 | value(R) type I 99 | raising 100 | resumable(ZCX_TBOX_STATS) . 101 | methods INTERQUARTILE_RANGE 102 | importing 103 | !COL type NAME_FELD default `TABLE_LINE` 104 | returning 105 | value(R) type STRING 106 | raising 107 | resumable(ZCX_TBOX_STATS) . 108 | methods THIRD_QUARTILE 109 | importing 110 | !COL type NAME_FELD default `TABLE_LINE` 111 | returning 112 | value(R) type STRING 113 | raising 114 | resumable(ZCX_TBOX_STATS) . 115 | methods SECOND_QUARTILE 116 | importing 117 | !COL type NAME_FELD default `TABLE_LINE` 118 | returning 119 | value(R) type STRING 120 | raising 121 | resumable(ZCX_TBOX_STATS) . 122 | methods FIRST_QUARTILE 123 | importing 124 | !COL type NAME_FELD default `TABLE_LINE` 125 | returning 126 | value(R) type STRING 127 | raising 128 | resumable(ZCX_TBOX_STATS) . 129 | methods DISPERSION_INDEX 130 | importing 131 | !COL type NAME_FELD default `TABLE_LINE` 132 | returning 133 | value(R) type STRING 134 | raising 135 | resumable(ZCX_TBOX_STATS) . 136 | methods COEFFICIENT_VARIATION 137 | importing 138 | !COL type NAME_FELD default `TABLE_LINE` 139 | returning 140 | value(R) type STRING 141 | raising 142 | resumable(ZCX_TBOX_STATS) . 143 | methods STANDARD_DEVIATION 144 | importing 145 | !COL type NAME_FELD default `TABLE_LINE` 146 | returning 147 | value(R) type STRING 148 | raising 149 | resumable(ZCX_TBOX_STATS) . 150 | methods KURTOSIS 151 | importing 152 | !COL type NAME_FELD default `TABLE_LINE` 153 | returning 154 | value(R) type STRING 155 | raising 156 | resumable(ZCX_TBOX_STATS) . 157 | methods SKEWNESS 158 | importing 159 | !COL type NAME_FELD default `TABLE_LINE` 160 | returning 161 | value(R) type STRING 162 | raising 163 | resumable(ZCX_TBOX_STATS) . 164 | methods VARIANCE 165 | importing 166 | !COL type NAME_FELD default `TABLE_LINE` 167 | returning 168 | value(R) type STRING 169 | raising 170 | resumable(ZCX_TBOX_STATS) . 171 | methods MEDIAN 172 | importing 173 | !COL type NAME_FELD default `TABLE_LINE` 174 | returning 175 | value(R) type STRING 176 | raising 177 | resumable(ZCX_TBOX_STATS) . 178 | methods QUADRATIC_MEAN 179 | importing 180 | !COL type NAME_FELD default `TABLE_LINE` 181 | returning 182 | value(R) type STRING 183 | raising 184 | resumable(ZCX_TBOX_STATS) . 185 | methods HARMONIC_MEAN 186 | importing 187 | !COL type NAME_FELD default `TABLE_LINE` 188 | returning 189 | value(R) type STRING 190 | raising 191 | resumable(ZCX_TBOX_STATS) . 192 | methods GEOMETRIC_MEAN 193 | importing 194 | !COL type NAME_FELD default `TABLE_LINE` 195 | returning 196 | value(R) type STRING 197 | raising 198 | resumable(ZCX_TBOX_STATS) . 199 | methods MAD_MEDIAN 200 | importing 201 | !COL type NAME_FELD default `TABLE_LINE` 202 | returning 203 | value(R) type STRING 204 | raising 205 | resumable(ZCX_TBOX_STATS) . 206 | methods MAD_MEAN 207 | importing 208 | !COL type NAME_FELD default `TABLE_LINE` 209 | returning 210 | value(R) type STRING 211 | raising 212 | resumable(ZCX_TBOX_STATS) . 213 | methods MEAN 214 | importing 215 | !COL type NAME_FELD default `TABLE_LINE` 216 | returning 217 | value(R) type STRING 218 | raising 219 | resumable(ZCX_TBOX_STATS) . 220 | methods SUM 221 | importing 222 | !COL type NAME_FELD default `TABLE_LINE` 223 | returning 224 | value(R) type STRING 225 | raising 226 | resumable(ZCX_TBOX_STATS) . 227 | methods MAX 228 | importing 229 | !COL type NAME_FELD default `TABLE_LINE` 230 | returning 231 | value(R) type STRING 232 | raising 233 | resumable(ZCX_TBOX_STATS) . 234 | methods RANGE 235 | importing 236 | !COL type NAME_FELD default `TABLE_LINE` 237 | returning 238 | value(R) type STRING 239 | raising 240 | resumable(ZCX_TBOX_STATS) . 241 | methods MIN 242 | importing 243 | !COL type NAME_FELD default `TABLE_LINE` 244 | returning 245 | value(R) type STRING 246 | raising 247 | resumable(ZCX_TBOX_STATS) . 248 | methods _NUM_DISTINCT 249 | importing 250 | !COL type NAME_FELD default `TABLE_LINE` 251 | returning 252 | value(R) type STRING_TABLE 253 | raising 254 | resumable(ZCX_TBOX_STATS) . 255 | methods _CAT_DISTINCT 256 | importing 257 | !COL type NAME_FELD default `TABLE_LINE` 258 | returning 259 | value(R) type STRING_TABLE 260 | raising 261 | resumable(ZCX_TBOX_STATS) . 262 | methods DISTINCT 263 | importing 264 | !COL type NAME_FELD default `TABLE_LINE` 265 | returning 266 | value(R) type STRING_TABLE 267 | raising 268 | resumable(ZCX_TBOX_STATS) . 269 | methods GROUP_BY 270 | importing 271 | !GROUPING type STRING 272 | returning 273 | value(R) type ref to ZTBOX_CL_STATS_GROUP 274 | raising 275 | resumable(ZCX_TBOX_STATS) . 276 | methods OUTLIERS 277 | importing 278 | !COL type NAME_FELD default `TABLE_LINE` 279 | returning 280 | value(R) type STRING_TABLE 281 | raising 282 | resumable(ZCX_TBOX_STATS) . 283 | methods HISTOGRAM 284 | importing 285 | !COL type NAME_FELD default `TABLE_LINE` 286 | returning 287 | value(R) type TY_HIST_T 288 | raising 289 | resumable(ZCX_TBOX_STATS) . 290 | methods EMPIRICAL_PDF 291 | importing 292 | !COL type NAME_FELD default `TABLE_LINE` 293 | returning 294 | value(R) type TY_VALUES_T 295 | raising 296 | resumable(ZCX_TBOX_STATS) . 297 | methods EMPIRICAL_CDF 298 | importing 299 | !COL type NAME_FELD default `TABLE_LINE` 300 | returning 301 | value(R) type TY_EDIST_T 302 | raising 303 | resumable(ZCX_TBOX_STATS) . 304 | methods CORRELATION 305 | importing 306 | !COLS type STRING 307 | returning 308 | value(R) type STRING 309 | raising 310 | resumable(ZCX_TBOX_STATS) . 311 | methods COVARIANCE 312 | importing 313 | !COLS type STRING 314 | returning 315 | value(R) type STRING 316 | raising 317 | resumable(ZCX_TBOX_STATS) . 318 | class-methods STANDARD 319 | importing 320 | !SIZE type I default 1 321 | returning 322 | value(R) type TY_FLOATS 323 | raising 324 | resumable(ZCX_TBOX_STATS) . 325 | class-methods NORMAL 326 | importing 327 | !MEAN type F default 0 328 | !VARIANCE type F default 1 329 | !SIZE type I default 1 330 | returning 331 | value(R) type TY_FLOATS 332 | raising 333 | resumable(ZCX_TBOX_STATS) . 334 | class-methods BINOMIAL 335 | importing 336 | !N type I default 2 337 | !P type F default `0.5` 338 | !SIZE type I default 1 339 | returning 340 | value(R) type TY_INTS 341 | raising 342 | resumable(ZCX_TBOX_STATS) . 343 | class-methods GEOMETRIC 344 | importing 345 | !P type F default `0.5` 346 | !SIZE type I default 1 347 | returning 348 | value(R) type TY_INTS 349 | raising 350 | resumable(ZCX_TBOX_STATS) . 351 | class-methods POISSON 352 | importing 353 | !L type F default `1.0` 354 | !SIZE type I default 1 355 | returning 356 | value(R) type TY_INTS 357 | raising 358 | resumable(ZCX_TBOX_STATS) . 359 | class-methods BERNOULLI 360 | importing 361 | !P type F default `0.5` 362 | !SIZE type I default 1 363 | returning 364 | value(R) type TY_INTS 365 | raising 366 | resumable(ZCX_TBOX_STATS) . 367 | class-methods UNIFORM 368 | importing 369 | !LOW type F default 0 370 | !HIGH type F default 1 371 | !SIZE type I default 1 372 | returning 373 | value(R) type TY_FLOATS 374 | raising 375 | resumable(ZCX_TBOX_STATS) . 376 | methods ARE_NORMAL 377 | importing 378 | !COL type NAME_FELD default `TABLE_LINE` 379 | !ALPHA type F default `0.5` 380 | exporting 381 | !P_VALUE type F 382 | returning 383 | value(R) type ABAP_BOOL 384 | raising 385 | resumable(ZCX_TBOX_STATS) . 386 | methods COL 387 | importing 388 | !COL type NAME_FELD 389 | returning 390 | value(R) type ref to ZTBOX_CL_STATS 391 | raising 392 | resumable(ZCX_TBOX_STATS) . 393 | PROTECTED SECTION. 394 | private section. 395 | 396 | types: 397 | BEGIN OF ty_buffers, 398 | col TYPE name_feld, 399 | object TYPE string, 400 | data TYPE REF TO data, 401 | END OF ty_buffers . 402 | types: 403 | ty_buffers_t TYPE TABLE OF ty_buffers . 404 | types: 405 | BEGIN OF ty_float_value, 406 | ix TYPE i, 407 | value TYPE f, 408 | END OF ty_float_value . 409 | types: 410 | ty_float_values TYPE TABLE OF ty_float_value WITH DEFAULT KEY . 411 | types: 412 | BEGIN OF ty_string_value, 413 | ix TYPE i, 414 | value TYPE string, 415 | END OF ty_string_value . 416 | types: 417 | ty_string_values TYPE TABLE OF ty_string_value WITH DEFAULT KEY . 418 | types: 419 | BEGIN OF ty_values_indexed, 420 | ix TYPE i, 421 | value TYPE f, 422 | END OF ty_values_indexed . 423 | types: 424 | ty_values_indexed_t TYPE TABLE OF ty_values_indexed WITH DEFAULT KEY . 425 | types: 426 | BEGIN OF ty_num_value, 427 | column TYPE name_feld, 428 | elem TYPE REF TO cl_abap_elemdescr, 429 | values TYPE ty_float_values, 430 | END OF ty_num_value . 431 | types: 432 | ty_num_values TYPE TABLE OF ty_num_value WITH DEFAULT KEY . 433 | types: 434 | BEGIN OF ty_str_value, 435 | column TYPE name_feld, 436 | elem TYPE REF TO cl_abap_elemdescr, 437 | values TYPE ty_string_values, 438 | END OF ty_str_value . 439 | types: 440 | ty_str_values TYPE TABLE OF ty_str_value WITH DEFAULT KEY . 441 | types: 442 | BEGIN OF ty_catalog, 443 | column TYPE name_feld, 444 | elem TYPE REF TO cl_abap_elemdescr, 445 | values TYPE TABLE OF ty_values_indexed WITH DEFAULT KEY, 446 | END OF ty_catalog . 447 | types: 448 | ty_catalog_t TYPE TABLE OF ty_catalog WITH DEFAULT KEY . 449 | 450 | data _DATA type ref to DATA . 451 | class-data: 452 | _numerical_types TYPE RANGE OF abap_typekind . 453 | data _COMPONENTS type ABAP_COMPONENT_TAB . 454 | data _NUMERICAL_VALUES type TY_NUM_VALUES . 455 | data _CATEGORIAL_VALUES type TY_STR_VALUES . 456 | data _TABLE_LINE type ABAP_BOOL . 457 | data _BUFFERS type TY_BUFFERS_T . 458 | 459 | class-methods _SET_NUMERICAL_TYPES . 460 | methods _CREATE_BINS 461 | importing 462 | !COL type NAME_FELD 463 | returning 464 | value(R) type TY_BINS 465 | raising 466 | resumable(ZCX_TBOX_STATS) . 467 | methods _SET_VALUES 468 | raising 469 | resumable(ZCX_TBOX_STATS) . 470 | methods _GET_VALUES 471 | importing 472 | !COL type NAME_FELD default `TABLE_LINE` 473 | returning 474 | value(R) type TY_STRING_VALUES . 475 | methods _GET_CAT_VALUES 476 | importing 477 | !COL type NAME_FELD default `TABLE_LINE` 478 | returning 479 | value(R) type TY_STRING_VALUES . 480 | methods _GET_NUM_VALUES 481 | importing 482 | !COL type NAME_FELD default `TABLE_LINE` 483 | returning 484 | value(R) type TY_FLOAT_VALUES . 485 | methods _CREATE_TABLE_LIKE 486 | importing 487 | !COL type NAME_FELD 488 | returning 489 | value(R) type ref to DATA . 490 | methods _CREATE_DATA_LIKE 491 | importing 492 | !COL type NAME_FELD 493 | returning 494 | value(R) type ref to DATA . 495 | methods _CHECK_COL 496 | importing 497 | !COL type NAME_FELD default `TABLE_LINE` 498 | raising 499 | resumable(ZCX_TBOX_STATS) . 500 | methods _CHECK_CAT_COL 501 | importing 502 | !COL type NAME_FELD default `TABLE_LINE` 503 | raising 504 | resumable(ZCX_TBOX_STATS) . 505 | methods _CHECK_NUM_COL 506 | importing 507 | !COL type NAME_FELD default `TABLE_LINE` 508 | raising 509 | resumable(ZCX_TBOX_STATS) . 510 | methods _SET_COMPONENTS 511 | importing 512 | !TABLE type ANY TABLE 513 | raising 514 | resumable(ZCX_TBOX_STATS) . 515 | class-methods _UNIQUE 516 | importing 517 | !TAB type TY_FLOATS 518 | returning 519 | value(R) type F . 520 | class-methods _CHECK_SIZE 521 | importing 522 | !S type I 523 | raising 524 | resumable(ZCX_TBOX_STATS) . 525 | class-methods _CHECK_SIGMA 526 | importing 527 | !S type F 528 | raising 529 | resumable(ZCX_TBOX_STATS) . 530 | class-methods _CHECK_LAMBDA 531 | importing 532 | !L type F 533 | raising 534 | resumable(ZCX_TBOX_STATS) . 535 | class-methods _CHECK_N 536 | importing 537 | !N type I 538 | raising 539 | resumable(ZCX_TBOX_STATS) . 540 | class-methods _CHECK_P 541 | importing 542 | !P type F 543 | raising 544 | resumable(ZCX_TBOX_STATS) . 545 | methods _ADD_TO_BUFFER 546 | importing 547 | !COL type NAME_FELD default `TABLE_LINE` 548 | !OBJECT type STRING 549 | !DATA type ANY . 550 | methods _BUFFERIZED 551 | importing 552 | !COL type NAME_FELD default `TABLE_LINE` 553 | !OBJECT type STRING 554 | returning 555 | value(R) type ABAP_BOOL . 556 | methods _GET_FROM_BUFFER 557 | importing 558 | !COL type NAME_FELD default `TABLE_LINE` 559 | !OBJECT type STRING 560 | exporting 561 | !DATA type ANY . 562 | ENDCLASS. 563 | 564 | 565 | 566 | CLASS ZTBOX_CL_STATS IMPLEMENTATION. 567 | 568 | 569 | METHOD are_normal. 570 | ********************************************************************** 571 | * Jarque-Bera normality test 572 | ********************************************************************** 573 | 574 | _check_num_col( col ). 575 | 576 | DATA(values) = _get_num_values( col ). 577 | 578 | DATA(total) = CONV f( count( ) ). 579 | DATA(skew) = CONV f( skewness( col ) ). 580 | DATA(kurt) = CONV f( kurtosis( col ) ). 581 | 582 | DATA(jb) = ( ( total + `1.0` ) / `6.0` ) * ( skew ** 2 + ( ( kurt - `3.0` ) ** 2 ) / `4.0` ). 583 | 584 | p_value = exp( ( `-1.0` ) * jb / `2.0` ). 585 | 586 | r = xsdbool( p_value > alpha ). 587 | 588 | ENDMETHOD. 589 | 590 | 591 | METHOD bernoulli. 592 | 593 | _check_p( p ). 594 | _check_size( size ). 595 | 596 | DATA(unif) = uniform( size = size ). 597 | 598 | r = VALUE #( FOR u IN unif ( COND #( WHEN u LE p THEN 1 ELSE 0 ) ) ). 599 | 600 | ENDMETHOD. 601 | 602 | 603 | METHOD binomial. 604 | 605 | _check_p( p ). 606 | _check_n( n ). 607 | _check_size( size ). 608 | 609 | r = VALUE #( FOR i = 0 UNTIL i = size ( REDUCE f( LET b = bernoulli( size = n p = p ) IN INIT s = 0 FOR x IN b NEXT s = s + x ) ) ). 610 | 611 | ENDMETHOD. 612 | 613 | 614 | METHOD class_constructor. 615 | 616 | _set_numerical_types( ). 617 | 618 | ENDMETHOD. 619 | 620 | 621 | METHOD coefficient_variation. 622 | 623 | IF _bufferized( col = col object = `CV` ). 624 | _get_from_buffer( EXPORTING col = col object = `CV` IMPORTING data = r ). 625 | RETURN. 626 | ENDIF. 627 | 628 | _check_num_col( col ). 629 | 630 | DATA(dev) = CONV f( standard_deviation( col ) ). 631 | DATA(mean) = CONV f( mean( col ) ). 632 | 633 | CHECK mean IS NOT INITIAL. 634 | 635 | r = dev / mean. 636 | 637 | _add_to_buffer( col = col object = `CV` data = r ). 638 | 639 | ENDMETHOD. 640 | 641 | 642 | METHOD col. 643 | 644 | _check_num_col( col ). 645 | 646 | DATA(tab_val) = _create_table_like( col ). 647 | FIELD-SYMBOLS TYPE ANY TABLE. 648 | ASSIGN tab_val->* TO . 649 | 650 | FIELD-SYMBOLS TYPE ANY TABLE. 651 | ASSIGN _data->* TO . 652 | 653 | LOOP AT ASSIGNING FIELD-SYMBOL(). 654 | ASSIGN COMPONENT col OF STRUCTURE TO FIELD-SYMBOL(). 655 | INSERT INTO TABLE . 656 | ENDLOOP. 657 | 658 | r = NEW #( ). 659 | 660 | ENDMETHOD. 661 | 662 | 663 | METHOD constructor. 664 | 665 | _set_components( table ). 666 | 667 | FIELD-SYMBOLS TYPE ANY TABLE. 668 | CREATE DATA _data LIKE table. 669 | 670 | ASSIGN _data->* TO . 671 | 672 | = table. 673 | 674 | _set_values( ). 675 | 676 | ENDMETHOD. 677 | 678 | 679 | METHOD correlation. 680 | 681 | SPLIT cols AT `,` INTO DATA(col_1) DATA(col_2). 682 | 683 | _check_num_col( condense( col_1 ) ). 684 | _check_num_col( condense( col_2 ) ). 685 | 686 | DATA(std_dev_1) = CONV f( standard_deviation( condense( col_1 ) ) ). 687 | DATA(std_dev_2) = CONV f( standard_deviation( condense( col_2 ) ) ). 688 | 689 | CHECK std_dev_1 IS NOT INITIAL. 690 | CHECK std_dev_2 IS NOT INITIAL. 691 | 692 | r = CONV f( covariance( cols ) ) / ( std_dev_1 * std_dev_2 ). 693 | 694 | ENDMETHOD. 695 | 696 | 697 | METHOD count. 698 | 699 | FIELD-SYMBOLS TYPE STANDARD TABLE. 700 | ASSIGN _data->* TO . 701 | 702 | r = lines( ). 703 | 704 | ENDMETHOD. 705 | 706 | 707 | METHOD count_distinct. 708 | 709 | _check_col( col ). 710 | 711 | r = lines( distinct( col ) ). 712 | 713 | ENDMETHOD. 714 | 715 | 716 | METHOD count_not_initial. 717 | 718 | _check_col( col ). 719 | 720 | DATA(values) = _get_values( col ). 721 | 722 | DELETE values WHERE value IS INITIAL. 723 | 724 | r = lines( values ). 725 | 726 | ENDMETHOD. 727 | 728 | 729 | METHOD covariance. 730 | 731 | SPLIT cols AT `,` INTO DATA(col_1) DATA(col_2). 732 | 733 | _check_num_col( condense( col_1 ) ). 734 | _check_num_col( condense( col_2 ) ). 735 | 736 | DATA(val_1) = _get_num_values( condense( col_1 ) ). 737 | DATA(mean_1) = CONV f( mean( condense( col_1 ) ) ). 738 | DATA(val_2) = _get_num_values( condense( col_2 ) ). 739 | DATA(mean_2) = CONV f( mean( condense( col_2 ) ) ). 740 | DATA(total) = CONV f( count( ) ). 741 | 742 | r = REDUCE f( INIT co = CONV f( 0 ) FOR i = 1 UNTIL i = total + 1 NEXT co = co + val_1[ i ]-value * val_2[ i ]-value - mean_1 * mean_2 ) / ( total - `1.0` ). 743 | 744 | ENDMETHOD. 745 | 746 | 747 | METHOD dispersion_index. 748 | 749 | IF _bufferized( col = col object = `DI` ). 750 | _get_from_buffer( EXPORTING col = col object = `DI` IMPORTING data = r ). 751 | RETURN. 752 | ENDIF. 753 | 754 | _check_num_col( col ). 755 | 756 | DATA(var) = CONV f( variance( col ) ). 757 | DATA(mean) = CONV f( mean( col ) ). 758 | 759 | CHECK mean IS NOT INITIAL. 760 | 761 | r = var / mean. 762 | 763 | _add_to_buffer( col = col object = `DI` data = r ). 764 | 765 | ENDMETHOD. 766 | 767 | 768 | METHOD distinct. 769 | 770 | IF _bufferized( col = col object = `DST` ). 771 | _get_from_buffer( EXPORTING col = col object = `DST` IMPORTING data = r ). 772 | RETURN. 773 | ENDIF. 774 | 775 | _check_col( col ). 776 | 777 | r = COND #( 778 | WHEN line_exists( _categorial_values[ column = col ] ) 779 | THEN _cat_distinct( col ) 780 | WHEN line_exists( _numerical_values[ column = col ] ) 781 | THEN _num_distinct( col ) ). 782 | 783 | _add_to_buffer( col = col object = `DST` data = r ). 784 | 785 | ENDMETHOD. 786 | 787 | 788 | METHOD empirical_cdf. 789 | 790 | IF _bufferized( col = col object = `ECDF` ). 791 | _get_from_buffer( EXPORTING col = col object = `ECDF` IMPORTING data = r ). 792 | RETURN. 793 | ENDIF. 794 | 795 | _check_num_col( col ). 796 | 797 | DATA(values) = _get_num_values( col ). 798 | DATA(total) = CONV f( count( ) ). 799 | DATA(dist_values) = values. 800 | 801 | SORT dist_values BY value. 802 | DELETE ADJACENT DUPLICATES FROM dist_values COMPARING value. 803 | 804 | CHECK total IS NOT INITIAL. 805 | 806 | SORT values BY value. 807 | 808 | r = VALUE #( FOR dv IN dist_values 809 | ( x = dv-value ) ). 810 | 811 | FIELD-SYMBOLS LIKE LINE OF r. 812 | DATA ix TYPE i VALUE 0. 813 | DATA prev TYPE f. 814 | 815 | LOOP AT values INTO DATA(val). 816 | 817 | IF IS NOT ASSIGNED OR val-value > -x. 818 | prev = COND f( WHEN IS ASSIGNED THEN -y ELSE `0.0` ). 819 | ix = ix + 1. 820 | READ TABLE r ASSIGNING INDEX ix. 821 | -y = prev. 822 | ENDIF. 823 | 824 | -y = -y + `1.0`. 825 | 826 | ENDLOOP. 827 | 828 | LOOP AT r ASSIGNING . 829 | -y = -y / total. 830 | ENDLOOP. 831 | 832 | _add_to_buffer( col = col object = `ECDF` data = r ). 833 | 834 | ENDMETHOD. 835 | 836 | 837 | METHOD empirical_pdf. 838 | 839 | IF _bufferized( col = col object = `EPDF` ). 840 | _get_from_buffer( EXPORTING col = col object = `EPDF` IMPORTING data = r ). 841 | RETURN. 842 | ENDIF. 843 | 844 | _check_num_col( col ). 845 | 846 | DATA(hist) = histogram( col ). 847 | DATA(n) = CONV f( count( ) ). 848 | 849 | CHECK n IS NOT INITIAL. 850 | 851 | r = VALUE #( FOR _hist IN hist ( x = _hist-x y = CONV f( _hist-y ) / n ) ). 852 | 853 | _add_to_buffer( col = col object = `EPDF` data = r ). 854 | 855 | ENDMETHOD. 856 | 857 | 858 | METHOD first_quartile. 859 | 860 | IF _bufferized( col = col object = `FQR` ). 861 | _get_from_buffer( EXPORTING col = col object = `FQR` IMPORTING data = r ). 862 | RETURN. 863 | ENDIF. 864 | 865 | _check_num_col( col ). 866 | 867 | DATA(values) = _get_num_values( col ). 868 | DATA(total) = count( ). 869 | DATA(value_ref) = _create_data_like( col ). 870 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 871 | 872 | CHECK values IS NOT INITIAL. 873 | SORT values BY value. 874 | 875 | DATA(left_ix) = floor( CONV f( ( total + 1 ) / 4 ) ). 876 | DATA(right_ix) = ceil( CONV f( ( total + 1 ) / 4 ) ). 877 | 878 | r = = ( values[ left_ix ]-value + values[ right_ix ]-value ) / 2. 879 | 880 | _add_to_buffer( col = col object = `FQR` data = r ). 881 | 882 | ENDMETHOD. 883 | 884 | 885 | METHOD geometric. 886 | 887 | _check_p( p ). 888 | _check_size( size ). 889 | 890 | r = VALUE #( FOR i = 0 UNTIL i = size ( ceil( log( 1 - _unique( uniform( ) ) ) / log( 1 - p ) ) ) ). 891 | 892 | ENDMETHOD. 893 | 894 | 895 | METHOD geometric_mean. 896 | 897 | IF _bufferized( col = col object = `GME` ). 898 | _get_from_buffer( EXPORTING col = col object = `GME` IMPORTING data = r ). 899 | RETURN. 900 | ENDIF. 901 | 902 | _check_num_col( col ). 903 | 904 | DATA(values) = _get_num_values( col ). 905 | DATA(lines) = CONV f( count( ) ). 906 | 907 | CHECK values IS NOT INITIAL. 908 | 909 | r = REDUCE #( INIT gm = CONV f( 1 ) FOR val IN values NEXT gm = gm * ( val-value ** ( `1.0` / lines ) ) ). 910 | 911 | _add_to_buffer( col = col object = `GME` data = r ). 912 | 913 | ENDMETHOD. 914 | 915 | 916 | METHOD group_by. 917 | 918 | SPLIT grouping AT `,` INTO TABLE DATA(fields_group). 919 | 920 | r = NEW #( 921 | data = _data 922 | grouping = fields_group ). 923 | 924 | ENDMETHOD. 925 | 926 | 927 | METHOD harmonic_mean. 928 | 929 | IF _bufferized( col = col object = `HME` ). 930 | _get_from_buffer( EXPORTING col = col object = `HME` IMPORTING data = r ). 931 | RETURN. 932 | ENDIF. 933 | 934 | _check_num_col( col ). 935 | 936 | DATA(values) = _get_num_values( col ). 937 | DATA(lines) = CONV f( count( ) ). 938 | 939 | CHECK values IS NOT INITIAL. 940 | IF line_exists( values[ value = `0.0` ] ). 941 | r = `0`. 942 | RETURN. 943 | ENDIF. 944 | 945 | r = lines / REDUCE #( INIT sr = CONV f( 0 ) FOR val IN values NEXT sr = sr + ( `1.0` / val-value ) ). 946 | 947 | _add_to_buffer( col = col object = `HME` data = r ). 948 | 949 | ENDMETHOD. 950 | 951 | 952 | METHOD histogram. 953 | 954 | IF _bufferized( col = col object = `HIST` ). 955 | _get_from_buffer( EXPORTING col = col object = `HIST` IMPORTING data = r ). 956 | RETURN. 957 | ENDIF. 958 | 959 | _check_num_col( col ). 960 | 961 | DATA(values) = _get_num_values( col ). 962 | DATA(bins) = _create_bins( col ). 963 | 964 | r = VALUE #( FOR _bin IN bins 965 | ( x = _bin-x ) ). 966 | 967 | SORT values BY value. 968 | 969 | FIELD-SYMBOLS LIKE LINE OF r. 970 | DATA bin LIKE LINE OF bins. 971 | DATA ix TYPE i VALUE 0. 972 | 973 | LOOP AT values INTO DATA(val). 974 | 975 | IF IS NOT ASSIGNED OR val-value NOT BETWEEN bin-x AND bin-y. 976 | ix = ix + 1. 977 | READ TABLE bins INTO bin INDEX ix. 978 | READ TABLE r ASSIGNING WITH KEY x = bin-x. 979 | ENDIF. 980 | 981 | -y = -y + 1. 982 | 983 | ENDLOOP. 984 | 985 | _add_to_buffer( col = col object = `HIST` data = r ). 986 | 987 | ENDMETHOD. 988 | 989 | 990 | METHOD interquartile_range. 991 | 992 | IF _bufferized( col = col object = `IQR` ). 993 | _get_from_buffer( EXPORTING col = col object = `IQR` IMPORTING data = r ). 994 | RETURN. 995 | ENDIF. 996 | 997 | _check_num_col( col ). 998 | 999 | DATA(value_ref) = _create_data_like( col ). 1000 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1001 | 1002 | r = = third_quartile( col ) - first_quartile( col ). 1003 | 1004 | _add_to_buffer( col = col object = `IQR` data = r ). 1005 | 1006 | ENDMETHOD. 1007 | 1008 | 1009 | METHOD kurtosis. 1010 | 1011 | IF _bufferized( col = col object = `KRT` ). 1012 | _get_from_buffer( EXPORTING col = col object = `KRT` IMPORTING data = r ). 1013 | RETURN. 1014 | ENDIF. 1015 | 1016 | _check_num_col( col ). 1017 | 1018 | DATA(values) = _get_num_values( col ). 1019 | DATA(std_dev) = CONV f( standard_deviation( col ) ). 1020 | DATA(mean_value) = CONV f( mean( col ) ). 1021 | DATA(total) = CONV f( count( ) ). 1022 | 1023 | CHECK total > `1.0`. 1024 | CHECK std_dev IS NOT INITIAL. 1025 | 1026 | r = REDUCE #( INIT v = CONV f( 0 ) FOR val IN values NEXT v = v + ( ( val-value - mean_value ) / std_dev ) ** 4 ) / ( total - `1.0` ). 1027 | 1028 | _add_to_buffer( col = col object = `KRT` data = r ). 1029 | 1030 | ENDMETHOD. 1031 | 1032 | 1033 | METHOD mad_mean. 1034 | 1035 | IF _bufferized( col = col object = `MAD_MEA` ). 1036 | _get_from_buffer( EXPORTING col = col object = `MAD_MEA` IMPORTING data = r ). 1037 | RETURN. 1038 | ENDIF. 1039 | 1040 | _check_num_col( col ). 1041 | 1042 | DATA(total) = CONV f( count( ) ). 1043 | DATA(values) = _get_num_values( col ). 1044 | DATA(mean) = CONV f( mean( col ) ). 1045 | 1046 | CHECK total IS NOT INITIAL. 1047 | 1048 | r = REDUCE f( INIT mad = CONV f( 0 ) FOR v IN values NEXT mad = mad + abs( v-value - mean ) ) / total. 1049 | 1050 | _add_to_buffer( col = col object = `MAD_MEA` data = r ). 1051 | 1052 | ENDMETHOD. 1053 | 1054 | 1055 | METHOD mad_median. 1056 | 1057 | IF _bufferized( col = col object = `MAD_MED` ). 1058 | _get_from_buffer( EXPORTING col = col object = `MAD_MED` IMPORTING data = r ). 1059 | RETURN. 1060 | ENDIF. 1061 | 1062 | _check_num_col( col ). 1063 | 1064 | DATA(total) = CONV f( count( ) ). 1065 | DATA(values) = _get_num_values( col ). 1066 | DATA(median) = CONV f( median( col ) ). 1067 | 1068 | CHECK total IS NOT INITIAL. 1069 | 1070 | r = REDUCE f( INIT mad = CONV f( 0 ) FOR v IN values NEXT mad = mad + abs( v-value - median ) ) / total. 1071 | 1072 | _add_to_buffer( col = col object = `MAD_MED` data = r ). 1073 | 1074 | ENDMETHOD. 1075 | 1076 | 1077 | METHOD max. 1078 | 1079 | IF _bufferized( col = col object = `MAX` ). 1080 | _get_from_buffer( EXPORTING col = col object = `MAX` IMPORTING data = r ). 1081 | RETURN. 1082 | ENDIF. 1083 | 1084 | _check_num_col( col ). 1085 | 1086 | DATA(values) = _get_num_values( col ). 1087 | DATA(value_ref) = _create_data_like( col ). 1088 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1089 | 1090 | CHECK values IS NOT INITIAL. 1091 | 1092 | SORT values BY value DESCENDING. 1093 | r = = values[ 1 ]-value. 1094 | 1095 | _add_to_buffer( col = col object = `MAX` data = r ). 1096 | 1097 | ENDMETHOD. 1098 | 1099 | 1100 | METHOD mean. 1101 | 1102 | IF _bufferized( col = col object = `MEA` ). 1103 | _get_from_buffer( EXPORTING col = col object = `MEA` IMPORTING data = r ). 1104 | RETURN. 1105 | ENDIF. 1106 | 1107 | _check_num_col( col ). 1108 | 1109 | CHECK count( ) IS NOT INITIAL. 1110 | 1111 | r = CONV f( sum( col ) ) / CONV f( count( ) ). 1112 | 1113 | _add_to_buffer( col = col object = `MEA` data = r ). 1114 | 1115 | ENDMETHOD. 1116 | 1117 | 1118 | METHOD median. 1119 | 1120 | _check_num_col( col ). 1121 | 1122 | r = second_quartile( col ). 1123 | 1124 | ENDMETHOD. 1125 | 1126 | 1127 | METHOD min. 1128 | 1129 | IF _bufferized( col = col object = `MIN` ). 1130 | _get_from_buffer( EXPORTING col = col object = `MIN` IMPORTING data = r ). 1131 | RETURN. 1132 | ENDIF. 1133 | 1134 | _check_num_col( col ). 1135 | 1136 | DATA(values) = _get_num_values( col ). 1137 | DATA(value_ref) = _create_data_like( col ). 1138 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1139 | 1140 | CHECK values IS NOT INITIAL. 1141 | 1142 | SORT values BY value. 1143 | r = = values[ 1 ]-value. 1144 | 1145 | _add_to_buffer( col = col object = `MIN` data = r ). 1146 | 1147 | ENDMETHOD. 1148 | 1149 | 1150 | METHOD normal. 1151 | 1152 | _check_sigma( variance ). 1153 | _check_size( size ). 1154 | 1155 | DATA(s) = standard( size ). 1156 | 1157 | r = VALUE #( FOR x IN s ( x * sqrt( variance ) + mean ) ). 1158 | 1159 | ENDMETHOD. 1160 | 1161 | 1162 | METHOD normalize. 1163 | 1164 | _check_num_col( col ). 1165 | 1166 | DATA(min) = CONV f( min( col ) ). 1167 | DATA(max) = CONV f( max( col ) ). 1168 | 1169 | CHECK min NE max. 1170 | 1171 | READ TABLE _numerical_values ASSIGNING FIELD-SYMBOL() WITH KEY column = col. 1172 | 1173 | LOOP AT -values ASSIGNING FIELD-SYMBOL(). 1174 | -value = ( -value - min ) / ( max - min ). 1175 | ENDLOOP. 1176 | 1177 | DELETE _buffers WHERE col = col. 1178 | 1179 | r = VALUE #( FOR v IN -values ( v-value ) ). 1180 | 1181 | ENDMETHOD. 1182 | 1183 | 1184 | METHOD outliers. 1185 | 1186 | IF _bufferized( col = col object = `OUT` ). 1187 | _get_from_buffer( EXPORTING col = col object = `OUT` IMPORTING data = r ). 1188 | RETURN. 1189 | ENDIF. 1190 | 1191 | _check_num_col( col ). 1192 | 1193 | DATA(dist_values) = distinct( col ). 1194 | DATA(first_quartile) = CONV f( first_quartile( col ) ). 1195 | DATA(third_quartile) = CONV f( third_quartile( col ) ). 1196 | DATA(iqr) = third_quartile - first_quartile. 1197 | 1198 | LOOP AT dist_values INTO DATA(value). 1199 | 1200 | IF CONV f( value ) < ( first_quartile - ( 3 * iqr / 2 ) ) OR 1201 | CONV f( value ) > ( third_quartile + ( 3 * iqr / 2 ) ). 1202 | 1203 | APPEND value TO r. 1204 | 1205 | ENDIF. 1206 | 1207 | ENDLOOP. 1208 | 1209 | _add_to_buffer( col = col object = `OUT` data = r ). 1210 | 1211 | ENDMETHOD. 1212 | 1213 | 1214 | METHOD poisson. 1215 | 1216 | r = VALUE #( FOR i = 0 UNTIL i = size ( REDUCE #( INIT k = 0 FOR p = CONV f( 1 ) THEN p * _unique( uniform( ) ) WHILE p > exp( ( -1 ) * l ) NEXT k = k + 1 ) - 1 ) ). 1217 | 1218 | ENDMETHOD. 1219 | 1220 | 1221 | METHOD quadratic_mean. 1222 | 1223 | IF _bufferized( col = col object = `QME` ). 1224 | _get_from_buffer( EXPORTING col = col object = `QME` IMPORTING data = r ). 1225 | RETURN. 1226 | ENDIF. 1227 | 1228 | _check_num_col( col ). 1229 | 1230 | DATA(values) = _get_num_values( col ). 1231 | DATA(total) = CONV f( count( ) ). 1232 | 1233 | CHECK values IS NOT INITIAL. 1234 | 1235 | r = sqrt( REDUCE #( INIT sr = CONV f( 0 ) FOR val IN values NEXT sr = sr + ( val-value ) ** 2 ) / total ). 1236 | 1237 | _add_to_buffer( col = col object = `QME` data = r ). 1238 | 1239 | ENDMETHOD. 1240 | 1241 | 1242 | METHOD range. 1243 | 1244 | IF _bufferized( col = col object = `RAN` ). 1245 | _get_from_buffer( EXPORTING col = col object = `RAN` IMPORTING data = r ). 1246 | RETURN. 1247 | ENDIF. 1248 | 1249 | _check_num_col( col ). 1250 | 1251 | DATA(values) = _get_num_values( col ). 1252 | DATA(value_ref) = _create_data_like( col ). 1253 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1254 | 1255 | r = = max( ) - min( ). 1256 | 1257 | _add_to_buffer( col = col object = `RAN` data = r ). 1258 | 1259 | ENDMETHOD. 1260 | 1261 | 1262 | METHOD second_quartile. 1263 | 1264 | IF _bufferized( col = col object = `SQR` ). 1265 | _get_from_buffer( EXPORTING col = col object = `SQR` IMPORTING data = r ). 1266 | RETURN. 1267 | ENDIF. 1268 | 1269 | _check_num_col( col ). 1270 | 1271 | DATA(values) = _get_num_values( col ). 1272 | DATA(total) = count( ). 1273 | DATA(value_ref) = _create_data_like( col ). 1274 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1275 | 1276 | CHECK values IS NOT INITIAL. 1277 | SORT values BY value. 1278 | 1279 | DATA(left_ix) = floor( CONV f( ( total + 1 ) / 2 ) ). 1280 | DATA(right_ix) = ceil( CONV f( ( total + 1 ) / 2 ) ). 1281 | 1282 | r = = ( values[ left_ix ]-value + values[ right_ix ]-value ) / 2. 1283 | 1284 | _add_to_buffer( col = col object = `SQR` data = r ). 1285 | 1286 | ENDMETHOD. 1287 | 1288 | 1289 | METHOD skewness. 1290 | 1291 | IF _bufferized( col = col object = `SKW` ). 1292 | _get_from_buffer( EXPORTING col = col object = `SKW` IMPORTING data = r ). 1293 | RETURN. 1294 | ENDIF. 1295 | 1296 | _check_num_col( col ). 1297 | 1298 | DATA(values) = _get_num_values( col ). 1299 | DATA(std_dev) = CONV f( standard_deviation( col ) ). 1300 | DATA(mean_value) = CONV f( mean( col ) ). 1301 | DATA(total) = CONV f( count( ) ). 1302 | 1303 | CHECK total > `2.0`. 1304 | CHECK std_dev IS NOT INITIAL. 1305 | 1306 | r = total * REDUCE #( INIT v = CONV f( 0 ) FOR val IN values NEXT v = v + ( ( val-value - mean_value ) / std_dev ) ** 3 ) / ( ( total - `1.0` ) * ( total - `2.0` ) ). 1307 | 1308 | _add_to_buffer( col = col object = `SKW` data = r ). 1309 | 1310 | ENDMETHOD. 1311 | 1312 | 1313 | METHOD standard. 1314 | ********************************************************************** 1315 | * Box–Muller transform sampling method 1316 | ********************************************************************** 1317 | 1318 | _check_size( size ). 1319 | 1320 | DATA(i) = SWITCH i( size MOD 2 WHEN 0 THEN size / 2 ELSE ( size + 1 ) / 2 ). 1321 | 1322 | DO i TIMES. 1323 | 1324 | DATA(u_1) = uniform( ). 1325 | DATA(u_2) = uniform( ). 1326 | 1327 | APPEND ( sqrt( ( -2 ) * log( CONV f( u_1[ 1 ] ) ) ) * cos( 2 * acos( -1 ) * CONV f( u_2[ 1 ] ) ) ) TO r. 1328 | 1329 | IF sy-index EQ i AND i MOD 2 EQ 0. 1330 | APPEND ( sqrt( ( -2 ) * log( CONV f( u_1[ 1 ] ) ) ) * sin( 2 * acos( -1 ) * CONV f( u_2[ 1 ] ) ) ) TO r. 1331 | ENDIF. 1332 | 1333 | ENDDO. 1334 | 1335 | ENDMETHOD. 1336 | 1337 | 1338 | METHOD standardize. 1339 | 1340 | _check_num_col( col ). 1341 | 1342 | DATA(mean) = CONV f( mean( col ) ). 1343 | DATA(std_dev) = CONV f( standard_deviation( col ) ). 1344 | 1345 | CHECK std_dev IS NOT INITIAL. 1346 | 1347 | READ TABLE _numerical_values ASSIGNING FIELD-SYMBOL() WITH KEY column = col. 1348 | 1349 | LOOP AT -values ASSIGNING FIELD-SYMBOL(). 1350 | -value = ( -value - mean ) / std_dev. 1351 | ENDLOOP. 1352 | 1353 | DELETE _buffers WHERE col = col. 1354 | 1355 | r = VALUE #( FOR v IN -values ( v-value ) ). 1356 | 1357 | ENDMETHOD. 1358 | 1359 | 1360 | METHOD standard_deviation. 1361 | 1362 | IF _bufferized( col = col object = `DEV` ). 1363 | _get_from_buffer( EXPORTING col = col object = `DEV` IMPORTING data = r ). 1364 | RETURN. 1365 | ENDIF. 1366 | 1367 | _check_num_col( col ). 1368 | 1369 | r = sqrt( CONV f( variance( col ) ) ). 1370 | 1371 | _add_to_buffer( col = col object = `DEV` data = r ). 1372 | 1373 | 1374 | ENDMETHOD. 1375 | 1376 | 1377 | METHOD sum. 1378 | 1379 | IF _bufferized( col = col object = `SUM` ). 1380 | _get_from_buffer( EXPORTING col = col object = `SUM` IMPORTING data = r ). 1381 | RETURN. 1382 | ENDIF. 1383 | 1384 | _check_num_col( col ). 1385 | 1386 | DATA(values) = _get_num_values( col ). 1387 | DATA(value_ref) = _create_data_like( col ). 1388 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1389 | 1390 | DATA(res) = REDUCE #( INIT sum = CONV f( 0 ) FOR val IN values NEXT sum = sum + val-value ). 1391 | 1392 | TRY. 1393 | 1394 | r = = res. 1395 | 1396 | CATCH cx_sy_conversion_overflow. 1397 | 1398 | r = res. 1399 | 1400 | ENDTRY. 1401 | 1402 | _add_to_buffer( col = col object = `SUM` data = r ). 1403 | 1404 | ENDMETHOD. 1405 | 1406 | 1407 | METHOD third_quartile. 1408 | 1409 | IF _bufferized( col = col object = `TQR` ). 1410 | _get_from_buffer( EXPORTING col = col object = `TQR` IMPORTING data = r ). 1411 | RETURN. 1412 | ENDIF. 1413 | 1414 | _check_num_col( col ). 1415 | 1416 | DATA(values) = _get_num_values( col ). 1417 | DATA(total) = count( ). 1418 | DATA(value_ref) = _create_data_like( col ). 1419 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1420 | 1421 | CHECK values IS NOT INITIAL. 1422 | SORT values BY value. 1423 | 1424 | DATA(left_ix) = floor( CONV f( ( 3 * ( total + 1 ) ) / 4 ) ). 1425 | DATA(right_ix) = ceil( CONV f( ( 3 * ( total + 1 ) ) / 4 ) ). 1426 | 1427 | r = = ( values[ left_ix ]-value + values[ right_ix ]-value ) / 2. 1428 | 1429 | _add_to_buffer( col = col object = `TQR` data = r ). 1430 | 1431 | ENDMETHOD. 1432 | 1433 | 1434 | METHOD uniform. 1435 | 1436 | _check_size( size ). 1437 | 1438 | DATA(randomizer) = cl_abap_random_float=>create( cl_abap_random=>seed( ) ). 1439 | 1440 | DO size TIMES. 1441 | 1442 | DATA(rand_float) = randomizer->get_next( ). 1443 | 1444 | APPEND ( low + ( high - low ) * rand_float ) TO r. 1445 | 1446 | ENDDO. 1447 | 1448 | ENDMETHOD. 1449 | 1450 | 1451 | METHOD variance. 1452 | 1453 | IF _bufferized( col = col object = `VAR` ). 1454 | _get_from_buffer( EXPORTING col = col object = `VAR` IMPORTING data = r ). 1455 | RETURN. 1456 | ENDIF. 1457 | 1458 | _check_num_col( col ). 1459 | 1460 | DATA(values) = _get_num_values( col ). 1461 | DATA(mean_value) = CONV f( mean( col ) ). 1462 | DATA(total) = CONV f( count( ) ). 1463 | 1464 | CHECK total > `1.0`. 1465 | 1466 | r = REDUCE #( INIT v = CONV f( 0 ) FOR val IN values NEXT v = v + ( val-value - mean_value ) ** 2 ) / ( total - `1.0` ). 1467 | 1468 | _add_to_buffer( col = col object = `VAR` data = r ). 1469 | 1470 | ENDMETHOD. 1471 | 1472 | 1473 | METHOD _add_to_buffer. 1474 | 1475 | DATA data_ref TYPE REF TO data. 1476 | 1477 | CREATE DATA data_ref LIKE data. 1478 | ASSIGN data_ref->* TO FIELD-SYMBOL(). 1479 | = data. 1480 | 1481 | APPEND VALUE #( 1482 | col = col 1483 | object = object 1484 | data = data_ref ) TO _buffers. 1485 | 1486 | ENDMETHOD. 1487 | 1488 | 1489 | METHOD _bufferized. 1490 | 1491 | r = xsdbool( line_exists( _buffers[ col = col object = object ] ) ). 1492 | 1493 | ENDMETHOD. 1494 | 1495 | 1496 | METHOD _cat_distinct. 1497 | 1498 | DATA(values) = _get_cat_values( col ). 1499 | 1500 | SORT values BY value. 1501 | DELETE ADJACENT DUPLICATES FROM values COMPARING value. 1502 | 1503 | r = VALUE #( FOR v IN values ( v-value ) ). 1504 | 1505 | ENDMETHOD. 1506 | 1507 | 1508 | METHOD _check_cat_col. 1509 | 1510 | IF NOT line_exists( _categorial_values[ column = col ] ). 1511 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>column_not_categorical. 1512 | ENDIF. 1513 | 1514 | ENDMETHOD. 1515 | 1516 | 1517 | METHOD _check_col. 1518 | 1519 | IF NOT line_exists( _components[ name = col ] ). 1520 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>column_not_found. 1521 | ENDIF. 1522 | 1523 | ENDMETHOD. 1524 | 1525 | 1526 | METHOD _check_lambda. 1527 | 1528 | IF NOT l > `0.0`. 1529 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>l_not_valid. 1530 | ENDIF. 1531 | 1532 | ENDMETHOD. 1533 | 1534 | 1535 | METHOD _check_n. 1536 | 1537 | IF NOT n > 0. 1538 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>n_not_valid. 1539 | ENDIF. 1540 | 1541 | ENDMETHOD. 1542 | 1543 | 1544 | METHOD _check_num_col. 1545 | 1546 | IF NOT line_exists( _numerical_values[ column = col ] ). 1547 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>column_not_numerical. 1548 | ENDIF. 1549 | 1550 | ENDMETHOD. 1551 | 1552 | 1553 | METHOD _check_p. 1554 | 1555 | IF p NOT BETWEEN `0.0` AND `1.0`. 1556 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>p_not_valid. 1557 | ENDIF. 1558 | 1559 | ENDMETHOD. 1560 | 1561 | 1562 | METHOD _check_sigma. 1563 | 1564 | IF NOT s > `0.0`. 1565 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>v_not_valid. 1566 | ENDIF. 1567 | 1568 | ENDMETHOD. 1569 | 1570 | 1571 | METHOD _check_size. 1572 | 1573 | IF NOT s > 0. 1574 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>s_not_valid. 1575 | ENDIF. 1576 | 1577 | ENDMETHOD. 1578 | 1579 | 1580 | METHOD _create_bins. 1581 | 1582 | DATA(total) = CONV f( count( ) ). 1583 | DATA(min) = CONV f( min( ) ). 1584 | DATA(max) = CONV f( max( ) ). 1585 | DATA(iqr) = CONV f( interquartile_range( ) ). 1586 | 1587 | ********************************************************************** 1588 | * Freedman-Diaconis rule 1589 | ********************************************************************** 1590 | DATA(bin_width) = ( iqr / ( total ** ( 1 / 3 ) ) ) * 2. 1591 | DATA(bin_total) = CONV i( ( max - min ) / bin_width ). 1592 | 1593 | r = VALUE #( FOR i = 0 THEN i + 1 UNTIL i = bin_total ( x = min + i * bin_width y = min + ( i + 1 ) * bin_width ) ). 1594 | 1595 | ENDMETHOD. 1596 | 1597 | 1598 | METHOD _create_data_like. 1599 | 1600 | DATA(elem) = CAST cl_abap_elemdescr( _components[ name = col ]-type ). 1601 | 1602 | CREATE DATA r TYPE HANDLE elem. 1603 | 1604 | ENDMETHOD. 1605 | 1606 | 1607 | METHOD _create_table_like. 1608 | 1609 | DATA(elem) = CAST cl_abap_elemdescr( _components[ name = col ]-type ). 1610 | 1611 | DATA(table) = cl_abap_tabledescr=>create( elem ). 1612 | 1613 | CREATE DATA r TYPE HANDLE table. 1614 | 1615 | ENDMETHOD. 1616 | 1617 | 1618 | METHOD _get_cat_values. 1619 | 1620 | r = _categorial_values[ column = col ]-values. 1621 | 1622 | ENDMETHOD. 1623 | 1624 | 1625 | METHOD _get_from_buffer. 1626 | 1627 | DATA(data_ref) = _buffers[ col = col object = object ]-data. 1628 | 1629 | ASSIGN data_ref->* TO FIELD-SYMBOL(). 1630 | data = . 1631 | 1632 | ENDMETHOD. 1633 | 1634 | 1635 | METHOD _get_num_values. 1636 | 1637 | r = _numerical_values[ column = col ]-values. 1638 | 1639 | ENDMETHOD. 1640 | 1641 | 1642 | METHOD _get_values. 1643 | 1644 | IF line_exists( _categorial_values[ column = col ] ). 1645 | r = _categorial_values[ column = col ]-values. 1646 | RETURN. 1647 | ENDIF. 1648 | 1649 | IF line_exists( _numerical_values[ column = col ] ). 1650 | r = VALUE #( FOR n IN _numerical_values[ column = col ]-values ( ix = n-ix value = CONV string( n-value ) ) ). 1651 | ENDIF. 1652 | 1653 | ENDMETHOD. 1654 | 1655 | 1656 | METHOD _num_distinct. 1657 | 1658 | DATA(values) = _get_num_values( col ). 1659 | 1660 | SORT values BY value. 1661 | DELETE ADJACENT DUPLICATES FROM values COMPARING value. 1662 | 1663 | DATA(value_ref) = _create_data_like( col ). 1664 | ASSIGN value_ref->* TO FIELD-SYMBOL(). 1665 | 1666 | LOOP AT values INTO DATA(val). 1667 | 1668 | = val-value. 1669 | APPEND TO r. 1670 | 1671 | ENDLOOP. 1672 | 1673 | ENDMETHOD. 1674 | 1675 | 1676 | METHOD _set_components. 1677 | 1678 | DATA(tab_type) = CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_data( table ) ). 1679 | DATA(line_type) = tab_type->get_table_line_type( ). 1680 | 1681 | CASE line_type->kind. 1682 | 1683 | WHEN cl_abap_typedescr=>kind_struct. 1684 | DATA(str_type) = CAST cl_abap_structdescr( line_type ). 1685 | DATA(components) = str_type->components. 1686 | 1687 | _components = VALUE #( FOR comp IN components ( name = comp-name type = str_type->get_component_type( comp-name ) ) ). 1688 | 1689 | WHEN cl_abap_typedescr=>kind_elem. 1690 | DATA(elem) = CAST cl_abap_elemdescr( line_type ). 1691 | 1692 | _components = VALUE #( ( name = `TABLE_LINE` type = elem ) ). 1693 | _table_line = abap_true. 1694 | 1695 | WHEN OTHERS. 1696 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>table_type_not_supported. 1697 | 1698 | ENDCASE. 1699 | 1700 | _numerical_values = VALUE #( FOR _nc IN _components WHERE ( type->type_kind IN _numerical_types ) 1701 | ( column = _nc-name elem = CAST cl_abap_elemdescr( _nc-type ) ) ). 1702 | _categorial_values = VALUE #( FOR _sc IN _components WHERE ( type->type_kind NOT IN _numerical_types ) 1703 | ( column = _sc-name elem = CAST cl_abap_elemdescr( _sc-type ) ) ). 1704 | 1705 | ENDMETHOD. 1706 | 1707 | 1708 | METHOD _set_numerical_types. 1709 | 1710 | _numerical_types = VALUE #( 1711 | sign = if_fsbp_const_range=>sign_include 1712 | option = if_fsbp_const_range=>option_equal 1713 | ( low = cl_abap_typedescr=>typekind_int ) 1714 | ( low = cl_abap_typedescr=>typekind_int1 ) 1715 | ( low = cl_abap_typedescr=>typekind_int2 ) 1716 | ( low = cl_abap_typedescr=>typekind_int8 ) 1717 | ( low = cl_abap_typedescr=>typekind_packed ) 1718 | ( low = cl_abap_typedescr=>typekind_decfloat ) 1719 | ( low = cl_abap_typedescr=>typekind_decfloat16 ) 1720 | ( low = cl_abap_typedescr=>typekind_decfloat34 ) 1721 | ( low = cl_abap_typedescr=>typekind_float ) ). 1722 | 1723 | ENDMETHOD. 1724 | 1725 | 1726 | METHOD _set_values. 1727 | 1728 | FIELD-SYMBOLS TYPE any. 1729 | FIELD-SYMBOLS TYPE STANDARD TABLE. 1730 | ASSIGN _data->* TO . 1731 | 1732 | LOOP AT ASSIGNING FIELD-SYMBOL(). 1733 | DATA(ix) = sy-tabix. 1734 | 1735 | IF _table_line EQ abap_true. 1736 | ASSIGN TO . 1737 | ENDIF. 1738 | 1739 | LOOP AT _numerical_values ASSIGNING FIELD-SYMBOL(). 1740 | IF _table_line EQ abap_false. 1741 | ASSIGN COMPONENT -column OF STRUCTURE TO . 1742 | ENDIF. 1743 | APPEND VALUE #( ix = ix value = CONV f( ) ) TO -values. 1744 | ENDLOOP. 1745 | 1746 | LOOP AT _categorial_values ASSIGNING FIELD-SYMBOL(). 1747 | IF _table_line EQ abap_false. 1748 | ASSIGN COMPONENT -column OF STRUCTURE TO . 1749 | ENDIF. 1750 | APPEND VALUE #( ix = ix value = CONV string( ) ) TO -values. 1751 | ENDLOOP. 1752 | 1753 | ENDLOOP. 1754 | 1755 | ENDMETHOD. 1756 | 1757 | 1758 | METHOD _unique. 1759 | 1760 | r = tab[ 1 ]. 1761 | 1762 | ENDMETHOD. 1763 | ENDCLASS. 1764 | -------------------------------------------------------------------------------- /src/ztbox_cl_stats.clas.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | ZTBOX_CL_STATS 7 | E 8 | TBOX | Statistics 9 | 1 10 | X 11 | X 12 | X 13 | 14 | 15 | 16 | ARE_NORMAL 17 | COL 18 | E 19 | Field name 20 | 21 | 22 | ARE_NORMAL 23 | ZCX_TBOX_STATS 24 | E 25 | TBOX | Stats Exceptions 26 | 27 | 28 | BERNOULLI 29 | R 30 | E 31 | Table of Strings 32 | 33 | 34 | BERNOULLI 35 | ZCX_TBOX_STATS 36 | E 37 | TBOX | Stats Exceptions 38 | 39 | 40 | BINOMIAL 41 | R 42 | E 43 | Table of Strings 44 | 45 | 46 | BINOMIAL 47 | ZCX_TBOX_STATS 48 | E 49 | TBOX | Stats Exceptions 50 | 51 | 52 | COEFFICIENT_VARIATION 53 | COL 54 | E 55 | Field name 56 | 57 | 58 | COEFFICIENT_VARIATION 59 | ZCX_TBOX_STATS 60 | E 61 | TBOX | Stats Exceptions 62 | 63 | 64 | COL 65 | COL 66 | E 67 | Field name 68 | 69 | 70 | COL 71 | R 72 | E 73 | TBOX | Statistics 74 | 75 | 76 | COL 77 | ZCX_TBOX_STATS 78 | E 79 | TBOX | Stats Exceptions 80 | 81 | 82 | CONSTRUCTOR 83 | ZCX_TBOX_STATS 84 | E 85 | TBOX | Stats Exceptions 86 | 87 | 88 | CORRELATION 89 | ZCX_TBOX_STATS 90 | E 91 | TBOX | Stats Exceptions 92 | 93 | 94 | COUNT_DISTINCT 95 | COL 96 | E 97 | Field name 98 | 99 | 100 | COUNT_DISTINCT 101 | ZCX_TBOX_STATS 102 | E 103 | TBOX | Stats Exceptions 104 | 105 | 106 | COUNT_NOT_INITIAL 107 | COL 108 | E 109 | Field name 110 | 111 | 112 | COUNT_NOT_INITIAL 113 | ZCX_TBOX_STATS 114 | E 115 | TBOX | Stats Exceptions 116 | 117 | 118 | COVARIANCE 119 | ZCX_TBOX_STATS 120 | E 121 | TBOX | Stats Exceptions 122 | 123 | 124 | DISPERSION_INDEX 125 | COL 126 | E 127 | Field name 128 | 129 | 130 | DISPERSION_INDEX 131 | ZCX_TBOX_STATS 132 | E 133 | TBOX | Stats Exceptions 134 | 135 | 136 | DISTINCT 137 | COL 138 | E 139 | Field name 140 | 141 | 142 | DISTINCT 143 | R 144 | E 145 | Table of Strings 146 | 147 | 148 | DISTINCT 149 | ZCX_TBOX_STATS 150 | E 151 | TBOX | Stats Exceptions 152 | 153 | 154 | EMPIRICAL_CDF 155 | COL 156 | E 157 | Field name 158 | 159 | 160 | EMPIRICAL_CDF 161 | ZCX_TBOX_STATS 162 | E 163 | TBOX | Stats Exceptions 164 | 165 | 166 | EMPIRICAL_PDF 167 | COL 168 | E 169 | Field name 170 | 171 | 172 | EMPIRICAL_PDF 173 | ZCX_TBOX_STATS 174 | E 175 | TBOX | Stats Exceptions 176 | 177 | 178 | FIRST_QUARTILE 179 | COL 180 | E 181 | Field name 182 | 183 | 184 | FIRST_QUARTILE 185 | ZCX_TBOX_STATS 186 | E 187 | TBOX | Stats Exceptions 188 | 189 | 190 | GEOMETRIC 191 | R 192 | E 193 | Table of Strings 194 | 195 | 196 | GEOMETRIC 197 | ZCX_TBOX_STATS 198 | E 199 | TBOX | Stats Exceptions 200 | 201 | 202 | GEOMETRIC_MEAN 203 | COL 204 | E 205 | Field name 206 | 207 | 208 | GROUP_BY 209 | R 210 | E 211 | TBOX | Statistics 212 | 213 | 214 | GROUP_BY 215 | ZCX_TBOX_STATS 216 | E 217 | TBOX | Stats Exceptions 218 | 219 | 220 | HARMONIC_MEAN 221 | COL 222 | E 223 | Field name 224 | 225 | 226 | HARMONIC_MEAN 227 | ZCX_TBOX_STATS 228 | E 229 | TBOX | Stats Exceptions 230 | 231 | 232 | HISTOGRAM 233 | COL 234 | E 235 | Field name 236 | 237 | 238 | HISTOGRAM 239 | ZCX_TBOX_STATS 240 | E 241 | TBOX | Stats Exceptions 242 | 243 | 244 | INTERQUARTILE_RANGE 245 | COL 246 | E 247 | Field name 248 | 249 | 250 | INTERQUARTILE_RANGE 251 | ZCX_TBOX_STATS 252 | E 253 | TBOX | Stats Exceptions 254 | 255 | 256 | KURTOSIS 257 | COL 258 | E 259 | Field name 260 | 261 | 262 | KURTOSIS 263 | ZCX_TBOX_STATS 264 | E 265 | TBOX | Stats Exceptions 266 | 267 | 268 | MAD_MEAN 269 | COL 270 | E 271 | Field name 272 | 273 | 274 | MAD_MEAN 275 | ZCX_TBOX_STATS 276 | E 277 | TBOX | Stats Exceptions 278 | 279 | 280 | MAD_MEDIAN 281 | COL 282 | E 283 | Field name 284 | 285 | 286 | MAD_MEDIAN 287 | ZCX_TBOX_STATS 288 | E 289 | TBOX | Stats Exceptions 290 | 291 | 292 | MAX 293 | COL 294 | E 295 | Field name 296 | 297 | 298 | MAX 299 | ZCX_TBOX_STATS 300 | E 301 | TBOX | Stats Exceptions 302 | 303 | 304 | MEAN 305 | COL 306 | E 307 | Field name 308 | 309 | 310 | MEAN 311 | ZCX_TBOX_STATS 312 | E 313 | TBOX | Stats Exceptions 314 | 315 | 316 | MEDIAN 317 | COL 318 | E 319 | Field name 320 | 321 | 322 | MEDIAN 323 | ZCX_TBOX_STATS 324 | E 325 | TBOX | Stats Exceptions 326 | 327 | 328 | MIN 329 | COL 330 | E 331 | Field name 332 | 333 | 334 | MIN 335 | ZCX_TBOX_STATS 336 | E 337 | TBOX | Stats Exceptions 338 | 339 | 340 | NORMAL 341 | R 342 | E 343 | Table of Strings 344 | 345 | 346 | NORMAL 347 | ZCX_TBOX_STATS 348 | E 349 | TBOX | Stats Exceptions 350 | 351 | 352 | NORMALIZE 353 | COL 354 | E 355 | Field name 356 | 357 | 358 | NORMALIZE 359 | ZCX_TBOX_STATS 360 | E 361 | TBOX | Stats Exceptions 362 | 363 | 364 | OUTLIERS 365 | COL 366 | E 367 | Field name 368 | 369 | 370 | OUTLIERS 371 | ZCX_TBOX_STATS 372 | E 373 | TBOX | Stats Exceptions 374 | 375 | 376 | POISSON 377 | R 378 | E 379 | Table of Strings 380 | 381 | 382 | POISSON 383 | ZCX_TBOX_STATS 384 | E 385 | TBOX | Stats Exceptions 386 | 387 | 388 | QUADRATIC_MEAN 389 | COL 390 | E 391 | Field name 392 | 393 | 394 | QUADRATIC_MEAN 395 | ZCX_TBOX_STATS 396 | E 397 | TBOX | Stats Exceptions 398 | 399 | 400 | RANGE 401 | COL 402 | E 403 | Field name 404 | 405 | 406 | RANGE 407 | ZCX_TBOX_STATS 408 | E 409 | TBOX | Stats Exceptions 410 | 411 | 412 | SECOND_QUARTILE 413 | COL 414 | E 415 | Field name 416 | 417 | 418 | SECOND_QUARTILE 419 | ZCX_TBOX_STATS 420 | E 421 | TBOX | Stats Exceptions 422 | 423 | 424 | SKEWNESS 425 | COL 426 | E 427 | Field name 428 | 429 | 430 | SKEWNESS 431 | ZCX_TBOX_STATS 432 | E 433 | TBOX | Stats Exceptions 434 | 435 | 436 | STANDARD 437 | R 438 | E 439 | Table of Strings 440 | 441 | 442 | STANDARD 443 | ZCX_TBOX_STATS 444 | E 445 | TBOX | Stats Exceptions 446 | 447 | 448 | STANDARDIZE 449 | COL 450 | E 451 | Field name 452 | 453 | 454 | STANDARDIZE 455 | ZCX_TBOX_STATS 456 | E 457 | TBOX | Stats Exceptions 458 | 459 | 460 | STANDARD_DEVIATION 461 | COL 462 | E 463 | Field name 464 | 465 | 466 | STANDARD_DEVIATION 467 | ZCX_TBOX_STATS 468 | E 469 | TBOX | Stats Exceptions 470 | 471 | 472 | SUM 473 | COL 474 | E 475 | Field name 476 | 477 | 478 | SUM 479 | ZCX_TBOX_STATS 480 | E 481 | TBOX | Stats Exceptions 482 | 483 | 484 | THIRD_QUARTILE 485 | COL 486 | E 487 | Field name 488 | 489 | 490 | THIRD_QUARTILE 491 | ZCX_TBOX_STATS 492 | E 493 | TBOX | Stats Exceptions 494 | 495 | 496 | UNIFORM 497 | R 498 | E 499 | Table of Strings 500 | 501 | 502 | UNIFORM 503 | ZCX_TBOX_STATS 504 | E 505 | TBOX | Stats Exceptions 506 | 507 | 508 | VARIANCE 509 | COL 510 | E 511 | Field name 512 | 513 | 514 | VARIANCE 515 | ZCX_TBOX_STATS 516 | E 517 | TBOX | Stats Exceptions 518 | 519 | 520 | _ADD_TO_BUFFER 521 | COL 522 | E 523 | Field name 524 | 525 | 526 | _BUFFERIZED 527 | COL 528 | E 529 | Field name 530 | 531 | 532 | _CAT_DISTINCT 533 | COL 534 | E 535 | Field name 536 | 537 | 538 | _CAT_DISTINCT 539 | R 540 | E 541 | Table of Strings 542 | 543 | 544 | _CAT_DISTINCT 545 | ZCX_TBOX_STATS 546 | E 547 | TBOX | Stats Exceptions 548 | 549 | 550 | _CHECK_CAT_COL 551 | COL 552 | E 553 | Field name 554 | 555 | 556 | _CHECK_CAT_COL 557 | ZCX_TBOX_STATS 558 | E 559 | TBOX | Stats Exceptions 560 | 561 | 562 | _CHECK_COL 563 | COL 564 | E 565 | Field name 566 | 567 | 568 | _CHECK_COL 569 | ZCX_TBOX_STATS 570 | E 571 | TBOX | Stats Exceptions 572 | 573 | 574 | _CHECK_LAMBDA 575 | ZCX_TBOX_STATS 576 | E 577 | TBOX | Stats Exceptions 578 | 579 | 580 | _CHECK_N 581 | ZCX_TBOX_STATS 582 | E 583 | TBOX | Stats Exceptions 584 | 585 | 586 | _CHECK_NUM_COL 587 | COL 588 | E 589 | Field name 590 | 591 | 592 | _CHECK_NUM_COL 593 | ZCX_TBOX_STATS 594 | E 595 | TBOX | Stats Exceptions 596 | 597 | 598 | _CHECK_P 599 | ZCX_TBOX_STATS 600 | E 601 | TBOX | Stats Exceptions 602 | 603 | 604 | _CHECK_SIGMA 605 | ZCX_TBOX_STATS 606 | E 607 | TBOX | Stats Exceptions 608 | 609 | 610 | _CHECK_SIZE 611 | ZCX_TBOX_STATS 612 | E 613 | TBOX | Stats Exceptions 614 | 615 | 616 | _CREATE_BINS 617 | COL 618 | E 619 | Field name 620 | 621 | 622 | _CREATE_BINS 623 | R 624 | E 625 | Table of Strings 626 | 627 | 628 | _CREATE_BINS 629 | ZCX_TBOX_STATS 630 | E 631 | TBOX | Stats Exceptions 632 | 633 | 634 | _CREATE_DATA_LIKE 635 | COL 636 | E 637 | Field name 638 | 639 | 640 | _CREATE_TABLE_LIKE 641 | COL 642 | E 643 | Field name 644 | 645 | 646 | _GET_CAT_VALUES 647 | COL 648 | E 649 | Field name 650 | 651 | 652 | _GET_FROM_BUFFER 653 | COL 654 | E 655 | Field name 656 | 657 | 658 | _GET_NUM_VALUES 659 | COL 660 | E 661 | Field name 662 | 663 | 664 | _GET_VALUES 665 | COL 666 | E 667 | Field name 668 | 669 | 670 | _NUM_DISTINCT 671 | COL 672 | E 673 | Field name 674 | 675 | 676 | _NUM_DISTINCT 677 | R 678 | E 679 | Table of Strings 680 | 681 | 682 | _NUM_DISTINCT 683 | ZCX_TBOX_STATS 684 | E 685 | TBOX | Stats Exceptions 686 | 687 | 688 | _SET_COMPONENTS 689 | ZCX_TBOX_STATS 690 | E 691 | TBOX | Stats Exceptions 692 | 693 | 694 | _SET_VALUES 695 | ZCX_TBOX_STATS 696 | E 697 | TBOX | Stats Exceptions 698 | 699 | 700 | 701 | 702 | 703 | -------------------------------------------------------------------------------- /src/ztbox_cl_stats_apack.clas.abap: -------------------------------------------------------------------------------- 1 | class ZTBOX_CL_STATS_APACK definition 2 | public 3 | create private . 4 | 5 | public section. 6 | 7 | interfaces ZIF_APACK_MANIFEST . 8 | 9 | methods CONSTRUCTOR . 10 | protected section. 11 | private section. 12 | ENDCLASS. 13 | 14 | 15 | 16 | CLASS ZTBOX_CL_STATS_APACK IMPLEMENTATION. 17 | 18 | 19 | METHOD constructor. 20 | 21 | zif_apack_manifest~descriptor = VALUE #( 22 | group_id = 'ztbox' 23 | artifact_id = 'abap-tbox-stats' 24 | version = '0.1' 25 | git_url = 'https://github.com/zenrosadira/abap-tbox-stats.git' ). 26 | 27 | ENDMETHOD. 28 | ENDCLASS. 29 | -------------------------------------------------------------------------------- /src/ztbox_cl_stats_apack.clas.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | ZTBOX_CL_STATS_APACK 7 | E 8 | TBOX - Formatter Apack Metadata 9 | 1 10 | X 11 | X 12 | X 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/ztbox_cl_stats_group.clas.abap: -------------------------------------------------------------------------------- 1 | class ZTBOX_CL_STATS_GROUP definition 2 | public 3 | final 4 | create private 5 | 6 | global friends ZTBOX_CL_STATS . 7 | 8 | public section. 9 | 10 | types: 11 | BEGIN OF ty_group, 12 | group_field TYPE name_feld, 13 | group_value TYPE string, 14 | END OF ty_group . 15 | types: 16 | ty_group_t TYPE TABLE OF ty_group WITH DEFAULT KEY . 17 | types: 18 | BEGIN OF ty_aggregation, 19 | group_by TYPE TABLE OF ty_group WITH DEFAULT KEY, 20 | value TYPE string, 21 | END OF ty_aggregation . 22 | types: 23 | ty_aggregation_t TYPE TABLE OF ty_aggregation WITH DEFAULT KEY . 24 | 25 | methods CONSTRUCTOR 26 | importing 27 | !DATA type ref to DATA 28 | !GROUPING type STRING_TABLE 29 | raising 30 | resumable(ZCX_TBOX_STATS) . 31 | methods COUNT 32 | exporting 33 | !E_RESULT type ANY TABLE 34 | returning 35 | value(R) type TY_AGGREGATION_T 36 | raising 37 | resumable(ZCX_TBOX_STATS) . 38 | methods COUNT_DISTINCT 39 | importing 40 | !COL type NAME_FELD default `TABLE_LINE` 41 | exporting 42 | !E_RESULT type ANY TABLE 43 | returning 44 | value(R) type TY_AGGREGATION_T 45 | raising 46 | resumable(ZCX_TBOX_STATS) . 47 | methods COUNT_NOT_INITIAL 48 | importing 49 | !COL type NAME_FELD default `TABLE_LINE` 50 | exporting 51 | !E_RESULT type ANY TABLE 52 | returning 53 | value(R) type TY_AGGREGATION_T 54 | raising 55 | resumable(ZCX_TBOX_STATS) . 56 | methods KURTOSIS 57 | importing 58 | !COL type NAME_FELD default `TABLE_LINE` 59 | exporting 60 | !E_RESULT type ANY TABLE 61 | returning 62 | value(R) type TY_AGGREGATION_T 63 | raising 64 | resumable(ZCX_TBOX_STATS) . 65 | methods SKEWNESS 66 | importing 67 | !COL type NAME_FELD default `TABLE_LINE` 68 | exporting 69 | !E_RESULT type ANY TABLE 70 | returning 71 | value(R) type TY_AGGREGATION_T 72 | raising 73 | resumable(ZCX_TBOX_STATS) . 74 | methods STANDARD_DEVIATION 75 | importing 76 | !COL type NAME_FELD default `TABLE_LINE` 77 | exporting 78 | !E_RESULT type ANY TABLE 79 | returning 80 | value(R) type TY_AGGREGATION_T 81 | raising 82 | resumable(ZCX_TBOX_STATS) . 83 | methods VARIANCE 84 | importing 85 | !COL type NAME_FELD default `TABLE_LINE` 86 | exporting 87 | !E_RESULT type ANY TABLE 88 | returning 89 | value(R) type TY_AGGREGATION_T 90 | raising 91 | resumable(ZCX_TBOX_STATS) . 92 | methods ARE_NORMAL 93 | importing 94 | !COL type NAME_FELD default `TABLE_LINE` 95 | exporting 96 | !E_RESULT type ANY TABLE 97 | returning 98 | value(R) type TY_AGGREGATION_T 99 | raising 100 | resumable(ZCX_TBOX_STATS) . 101 | methods MEDIAN 102 | importing 103 | !COL type NAME_FELD default `TABLE_LINE` 104 | exporting 105 | !E_RESULT type ANY TABLE 106 | returning 107 | value(R) type TY_AGGREGATION_T 108 | raising 109 | resumable(ZCX_TBOX_STATS) . 110 | methods INTERQUARTILE_RANGE 111 | importing 112 | !COL type NAME_FELD default `TABLE_LINE` 113 | exporting 114 | !E_RESULT type ANY TABLE 115 | returning 116 | value(R) type TY_AGGREGATION_T 117 | raising 118 | resumable(ZCX_TBOX_STATS) . 119 | methods THIRD_QUARTILE 120 | importing 121 | !COL type NAME_FELD default `TABLE_LINE` 122 | exporting 123 | !E_RESULT type ANY TABLE 124 | returning 125 | value(R) type TY_AGGREGATION_T 126 | raising 127 | resumable(ZCX_TBOX_STATS) . 128 | methods SECOND_QUARTILE 129 | importing 130 | !COL type NAME_FELD default `TABLE_LINE` 131 | exporting 132 | !E_RESULT type ANY TABLE 133 | returning 134 | value(R) type TY_AGGREGATION_T 135 | raising 136 | resumable(ZCX_TBOX_STATS) . 137 | methods FIRST_QUARTILE 138 | importing 139 | !COL type NAME_FELD default `TABLE_LINE` 140 | exporting 141 | !E_RESULT type ANY TABLE 142 | returning 143 | value(R) type TY_AGGREGATION_T 144 | raising 145 | resumable(ZCX_TBOX_STATS) . 146 | methods COEFFICIENT_VARIATION 147 | importing 148 | !COL type NAME_FELD default `TABLE_LINE` 149 | exporting 150 | !E_RESULT type ANY TABLE 151 | returning 152 | value(R) type TY_AGGREGATION_T 153 | raising 154 | resumable(ZCX_TBOX_STATS) . 155 | methods DISPERSION_INDEX 156 | importing 157 | !COL type NAME_FELD default `TABLE_LINE` 158 | exporting 159 | !E_RESULT type ANY TABLE 160 | returning 161 | value(R) type TY_AGGREGATION_T 162 | raising 163 | resumable(ZCX_TBOX_STATS) . 164 | methods QUADRATIC_MEAN 165 | importing 166 | !COL type NAME_FELD default `TABLE_LINE` 167 | exporting 168 | !E_RESULT type ANY TABLE 169 | returning 170 | value(R) type TY_AGGREGATION_T 171 | raising 172 | resumable(ZCX_TBOX_STATS) . 173 | methods GEOMETRIC_MEAN 174 | importing 175 | !COL type NAME_FELD default `TABLE_LINE` 176 | exporting 177 | !E_RESULT type ANY TABLE 178 | returning 179 | value(R) type TY_AGGREGATION_T 180 | raising 181 | resumable(ZCX_TBOX_STATS) . 182 | methods HARMONIC_MEAN 183 | importing 184 | !COL type NAME_FELD default `TABLE_LINE` 185 | exporting 186 | !E_RESULT type ANY TABLE 187 | returning 188 | value(R) type TY_AGGREGATION_T 189 | raising 190 | resumable(ZCX_TBOX_STATS) . 191 | methods MAD_MEAN 192 | importing 193 | !COL type NAME_FELD default `TABLE_LINE` 194 | exporting 195 | !E_RESULT type ANY TABLE 196 | returning 197 | value(R) type TY_AGGREGATION_T 198 | raising 199 | resumable(ZCX_TBOX_STATS) . 200 | methods MAD_MEDIAN 201 | importing 202 | !COL type NAME_FELD default `TABLE_LINE` 203 | exporting 204 | !E_RESULT type ANY TABLE 205 | returning 206 | value(R) type TY_AGGREGATION_T 207 | raising 208 | resumable(ZCX_TBOX_STATS) . 209 | methods MEAN 210 | importing 211 | !COL type NAME_FELD default `TABLE_LINE` 212 | exporting 213 | !E_RESULT type ANY TABLE 214 | returning 215 | value(R) type TY_AGGREGATION_T 216 | raising 217 | resumable(ZCX_TBOX_STATS) . 218 | methods SUM 219 | importing 220 | !COL type NAME_FELD default `TABLE_LINE` 221 | exporting 222 | !E_RESULT type ANY TABLE 223 | returning 224 | value(R) type TY_AGGREGATION_T 225 | raising 226 | resumable(ZCX_TBOX_STATS) . 227 | methods RANGE 228 | importing 229 | !COL type NAME_FELD default `TABLE_LINE` 230 | exporting 231 | !E_RESULT type ANY TABLE 232 | returning 233 | value(R) type TY_AGGREGATION_T 234 | raising 235 | resumable(ZCX_TBOX_STATS) . 236 | methods MAX 237 | importing 238 | !COL type NAME_FELD default `TABLE_LINE` 239 | exporting 240 | !E_RESULT type ANY TABLE 241 | returning 242 | value(R) type TY_AGGREGATION_T 243 | raising 244 | resumable(ZCX_TBOX_STATS) . 245 | methods COVARIANCE 246 | importing 247 | !COLS type STRING default `TABLE_LINE` 248 | exporting 249 | !E_RESULT type ANY TABLE 250 | returning 251 | value(R) type TY_AGGREGATION_T 252 | raising 253 | resumable(ZCX_TBOX_STATS) . 254 | methods CORRELATION 255 | importing 256 | !COLS type STRING default `TABLE_LINE` 257 | exporting 258 | !E_RESULT type ANY TABLE 259 | returning 260 | value(R) type TY_AGGREGATION_T 261 | raising 262 | resumable(ZCX_TBOX_STATS) . 263 | methods MIN 264 | importing 265 | !COL type NAME_FELD default `TABLE_LINE` 266 | exporting 267 | !E_RESULT type ANY TABLE 268 | returning 269 | value(R) type TY_AGGREGATION_T 270 | raising 271 | resumable(ZCX_TBOX_STATS) . 272 | methods COL 273 | importing 274 | !COL type NAME_FELD default `TABLE_LINE` 275 | returning 276 | value(R) type ref to ZTBOX_CL_STATS_GROUP 277 | raising 278 | resumable(ZCX_TBOX_STATS) . 279 | PROTECTED SECTION. 280 | private section. 281 | 282 | types: 283 | BEGIN OF ty_stats, 284 | group_by TYPE TABLE OF ty_group WITH DEFAULT KEY, 285 | stats TYPE REF TO ztbox_cl_stats, 286 | END OF ty_stats . 287 | types: 288 | ty_stats_t TYPE TABLE OF ty_stats . 289 | 290 | data _DATA type ref to DATA . 291 | data _TEMP type ref to DATA . 292 | data _GROUPING type STRING_TABLE . 293 | data _GROUP_STATS type TY_STATS_T . 294 | 295 | methods _GET_GROUP_VALUE 296 | importing 297 | !ROW type ANY 298 | returning 299 | value(R) type TY_GROUP_T . 300 | methods _GROUP_TO_TAB 301 | importing 302 | !AGG type TY_AGGREGATION_T 303 | !COL type NAME_FELD 304 | exporting 305 | !TAB type ANY TABLE 306 | raising 307 | resumable(ZCX_TBOX_STATS) . 308 | methods _PREPARE_STATS 309 | raising 310 | resumable(ZCX_TBOX_STATS) . 311 | methods _NEXT_FREE_COL 312 | importing 313 | !TAB type ANY TABLE 314 | returning 315 | value(R) type NAME_FELD . 316 | ENDCLASS. 317 | 318 | 319 | 320 | CLASS ZTBOX_CL_STATS_GROUP IMPLEMENTATION. 321 | 322 | 323 | METHOD ARE_NORMAL. 324 | 325 | r = VALUE #( FOR group IN _group_stats 326 | ( group_by = group-group_by 327 | value = group-stats->are_normal( col = col ) ) ). 328 | 329 | IF e_result IS SUPPLIED. 330 | _group_to_tab( 331 | EXPORTING 332 | agg = r 333 | col = _next_free_col( e_result ) 334 | IMPORTING 335 | tab = e_result ). 336 | ENDIF. 337 | 338 | ENDMETHOD. 339 | 340 | 341 | METHOD COEFFICIENT_VARIATION. 342 | 343 | r = VALUE #( FOR group IN _group_stats 344 | ( group_by = group-group_by 345 | value = group-stats->coefficient_variation( col ) ) ). 346 | 347 | IF e_result IS SUPPLIED. 348 | _group_to_tab( 349 | EXPORTING 350 | agg = r 351 | col = _next_free_col( e_result ) 352 | IMPORTING 353 | tab = e_result ). 354 | ENDIF. 355 | 356 | ENDMETHOD. 357 | 358 | 359 | METHOD col. 360 | 361 | r = NEW #( grouping = _grouping data = _data ). 362 | 363 | LOOP AT r->_group_stats ASSIGNING FIELD-SYMBOL(). 364 | 365 | -stats = -stats->col( col ). 366 | 367 | ENDLOOP. 368 | 369 | ENDMETHOD. 370 | 371 | 372 | METHOD constructor. 373 | 374 | _data = data. 375 | _grouping = VALUE #( FOR _grp IN grouping ( condense( _grp ) ) ). 376 | 377 | _prepare_stats( ). 378 | 379 | ENDMETHOD. 380 | 381 | 382 | METHOD CORRELATION. 383 | 384 | r = VALUE #( FOR group IN _group_stats 385 | ( group_by = group-group_by 386 | value = group-stats->correlation( cols ) ) ). 387 | 388 | IF e_result IS SUPPLIED. 389 | _group_to_tab( 390 | EXPORTING 391 | agg = r 392 | col = _next_free_col( e_result ) 393 | IMPORTING 394 | tab = e_result ). 395 | ENDIF. 396 | 397 | ENDMETHOD. 398 | 399 | 400 | METHOD count. 401 | 402 | r = VALUE #( FOR group IN _group_stats 403 | ( group_by = group-group_by 404 | value = group-stats->count( ) ) ). 405 | 406 | IF e_result IS SUPPLIED. 407 | _group_to_tab( 408 | EXPORTING 409 | agg = r 410 | col = _next_free_col( e_result ) 411 | IMPORTING 412 | tab = e_result ). 413 | ENDIF. 414 | 415 | ENDMETHOD. 416 | 417 | 418 | METHOD count_distinct. 419 | 420 | r = VALUE #( FOR group IN _group_stats 421 | ( group_by = group-group_by 422 | value = group-stats->count_distinct( col ) ) ). 423 | 424 | IF e_result IS SUPPLIED. 425 | _group_to_tab( 426 | EXPORTING 427 | agg = r 428 | col = _next_free_col( e_result ) 429 | IMPORTING 430 | tab = e_result ). 431 | ENDIF. 432 | 433 | ENDMETHOD. 434 | 435 | 436 | METHOD COUNT_NOT_INITIAL. 437 | 438 | r = VALUE #( FOR group IN _group_stats 439 | ( group_by = group-group_by 440 | value = group-stats->count_not_initial( col ) ) ). 441 | 442 | IF e_result IS SUPPLIED. 443 | _group_to_tab( 444 | EXPORTING 445 | agg = r 446 | col = _next_free_col( e_result ) 447 | IMPORTING 448 | tab = e_result ). 449 | ENDIF. 450 | 451 | ENDMETHOD. 452 | 453 | 454 | METHOD COVARIANCE. 455 | 456 | r = VALUE #( FOR group IN _group_stats 457 | ( group_by = group-group_by 458 | value = group-stats->covariance( cols ) ) ). 459 | 460 | IF e_result IS SUPPLIED. 461 | _group_to_tab( 462 | EXPORTING 463 | agg = r 464 | col = _next_free_col( e_result ) 465 | IMPORTING 466 | tab = e_result ). 467 | ENDIF. 468 | 469 | ENDMETHOD. 470 | 471 | 472 | METHOD DISPERSION_INDEX. 473 | 474 | r = VALUE #( FOR group IN _group_stats 475 | ( group_by = group-group_by 476 | value = group-stats->dispersion_index( col ) ) ). 477 | 478 | IF e_result IS SUPPLIED. 479 | _group_to_tab( 480 | EXPORTING 481 | agg = r 482 | col = _next_free_col( e_result ) 483 | IMPORTING 484 | tab = e_result ). 485 | ENDIF. 486 | 487 | ENDMETHOD. 488 | 489 | 490 | METHOD first_quartile. 491 | 492 | r = VALUE #( FOR group IN _group_stats 493 | ( group_by = group-group_by 494 | value = group-stats->first_quartile( col ) ) ). 495 | 496 | IF e_result IS SUPPLIED. 497 | _group_to_tab( 498 | EXPORTING 499 | agg = r 500 | col = _next_free_col( e_result ) 501 | IMPORTING 502 | tab = e_result ). 503 | ENDIF. 504 | 505 | ENDMETHOD. 506 | 507 | 508 | METHOD GEOMETRIC_MEAN. 509 | 510 | r = VALUE #( FOR group IN _group_stats 511 | ( group_by = group-group_by 512 | value = group-stats->geometric_mean( col ) ) ). 513 | 514 | IF e_result IS SUPPLIED. 515 | _group_to_tab( 516 | EXPORTING 517 | agg = r 518 | col = _next_free_col( e_result ) 519 | IMPORTING 520 | tab = e_result ). 521 | ENDIF. 522 | 523 | ENDMETHOD. 524 | 525 | 526 | METHOD harmonic_mean. 527 | 528 | r = VALUE #( FOR group IN _group_stats 529 | ( group_by = group-group_by 530 | value = group-stats->harmonic_mean( col ) ) ). 531 | 532 | IF e_result IS SUPPLIED. 533 | _group_to_tab( 534 | EXPORTING 535 | agg = r 536 | col = _next_free_col( e_result ) 537 | IMPORTING 538 | tab = e_result ). 539 | ENDIF. 540 | 541 | ENDMETHOD. 542 | 543 | 544 | METHOD interquartile_range. 545 | 546 | r = VALUE #( FOR group IN _group_stats 547 | ( group_by = group-group_by 548 | value = group-stats->interquartile_range( col ) ) ). 549 | 550 | IF e_result IS SUPPLIED. 551 | _group_to_tab( 552 | EXPORTING 553 | agg = r 554 | col = _next_free_col( e_result ) 555 | IMPORTING 556 | tab = e_result ). 557 | ENDIF. 558 | 559 | ENDMETHOD. 560 | 561 | 562 | METHOD kurtosis. 563 | 564 | r = VALUE #( FOR group IN _group_stats 565 | ( group_by = group-group_by 566 | value = group-stats->kurtosis( col ) ) ). 567 | 568 | IF e_result IS SUPPLIED. 569 | _group_to_tab( 570 | EXPORTING 571 | agg = r 572 | col = _next_free_col( e_result ) 573 | IMPORTING 574 | tab = e_result ). 575 | ENDIF. 576 | 577 | ENDMETHOD. 578 | 579 | 580 | METHOD MAD_MEAN. 581 | 582 | r = VALUE #( FOR group IN _group_stats 583 | ( group_by = group-group_by 584 | value = group-stats->mad_mean( col ) ) ). 585 | 586 | IF e_result IS SUPPLIED. 587 | _group_to_tab( 588 | EXPORTING 589 | agg = r 590 | col = _next_free_col( e_result ) 591 | IMPORTING 592 | tab = e_result ). 593 | ENDIF. 594 | 595 | ENDMETHOD. 596 | 597 | 598 | METHOD mad_median. 599 | 600 | r = VALUE #( FOR group IN _group_stats 601 | ( group_by = group-group_by 602 | value = group-stats->mad_median( col ) ) ). 603 | 604 | IF e_result IS SUPPLIED. 605 | _group_to_tab( 606 | EXPORTING 607 | agg = r 608 | col = _next_free_col( e_result ) 609 | IMPORTING 610 | tab = e_result ). 611 | ENDIF. 612 | 613 | ENDMETHOD. 614 | 615 | 616 | METHOD max. 617 | 618 | r = VALUE #( FOR group IN _group_stats 619 | ( group_by = group-group_by 620 | value = group-stats->max( col ) ) ). 621 | 622 | IF e_result IS SUPPLIED. 623 | _group_to_tab( 624 | EXPORTING 625 | agg = r 626 | col = _next_free_col( e_result ) 627 | IMPORTING 628 | tab = e_result ). 629 | ENDIF. 630 | 631 | ENDMETHOD. 632 | 633 | 634 | METHOD mean. 635 | 636 | r = VALUE #( FOR group IN _group_stats 637 | ( group_by = group-group_by 638 | value = group-stats->mean( col ) ) ). 639 | 640 | IF e_result IS SUPPLIED. 641 | _group_to_tab( 642 | EXPORTING 643 | agg = r 644 | col = _next_free_col( e_result ) 645 | IMPORTING 646 | tab = e_result ). 647 | ENDIF. 648 | 649 | ENDMETHOD. 650 | 651 | 652 | METHOD median. 653 | 654 | r = VALUE #( FOR group IN _group_stats 655 | ( group_by = group-group_by 656 | value = group-stats->median( col ) ) ). 657 | 658 | IF e_result IS SUPPLIED. 659 | _group_to_tab( 660 | EXPORTING 661 | agg = r 662 | col = _next_free_col( e_result ) 663 | IMPORTING 664 | tab = e_result ). 665 | ENDIF. 666 | 667 | ENDMETHOD. 668 | 669 | 670 | METHOD min. 671 | 672 | r = VALUE #( FOR group IN _group_stats 673 | ( group_by = group-group_by 674 | value = group-stats->min( col ) ) ). 675 | 676 | IF e_result IS SUPPLIED. 677 | _group_to_tab( 678 | EXPORTING 679 | agg = r 680 | col = _next_free_col( e_result ) 681 | IMPORTING 682 | tab = e_result ). 683 | ENDIF. 684 | 685 | ENDMETHOD. 686 | 687 | 688 | METHOD QUADRATIC_MEAN. 689 | 690 | r = VALUE #( FOR group IN _group_stats 691 | ( group_by = group-group_by 692 | value = group-stats->quadratic_mean( col ) ) ). 693 | 694 | IF e_result IS SUPPLIED. 695 | _group_to_tab( 696 | EXPORTING 697 | agg = r 698 | col = _next_free_col( e_result ) 699 | IMPORTING 700 | tab = e_result ). 701 | ENDIF. 702 | 703 | ENDMETHOD. 704 | 705 | 706 | METHOD range. 707 | 708 | r = VALUE #( FOR group IN _group_stats 709 | ( group_by = group-group_by 710 | value = group-stats->range( col ) ) ). 711 | 712 | IF e_result IS SUPPLIED. 713 | _group_to_tab( 714 | EXPORTING 715 | agg = r 716 | col = _next_free_col( e_result ) 717 | IMPORTING 718 | tab = e_result ). 719 | ENDIF. 720 | 721 | ENDMETHOD. 722 | 723 | 724 | METHOD second_quartile. 725 | 726 | r = VALUE #( FOR group IN _group_stats 727 | ( group_by = group-group_by 728 | value = group-stats->second_quartile( col ) ) ). 729 | 730 | IF e_result IS SUPPLIED. 731 | _group_to_tab( 732 | EXPORTING 733 | agg = r 734 | col = _next_free_col( e_result ) 735 | IMPORTING 736 | tab = e_result ). 737 | ENDIF. 738 | 739 | ENDMETHOD. 740 | 741 | 742 | METHOD skewness. 743 | 744 | r = VALUE #( FOR group IN _group_stats 745 | ( group_by = group-group_by 746 | value = group-stats->skewness( col ) ) ). 747 | 748 | IF e_result IS SUPPLIED. 749 | _group_to_tab( 750 | EXPORTING 751 | agg = r 752 | col = _next_free_col( e_result ) 753 | IMPORTING 754 | tab = e_result ). 755 | ENDIF. 756 | 757 | ENDMETHOD. 758 | 759 | 760 | METHOD standard_deviation. 761 | 762 | r = VALUE #( FOR group IN _group_stats 763 | ( group_by = group-group_by 764 | value = group-stats->standard_deviation( col ) ) ). 765 | 766 | IF e_result IS SUPPLIED. 767 | _group_to_tab( 768 | EXPORTING 769 | agg = r 770 | col = _next_free_col( e_result ) 771 | IMPORTING 772 | tab = e_result ). 773 | ENDIF. 774 | 775 | ENDMETHOD. 776 | 777 | 778 | METHOD sum. 779 | 780 | r = VALUE #( FOR group IN _group_stats 781 | ( group_by = group-group_by 782 | value = group-stats->sum( col ) ) ). 783 | 784 | IF e_result IS SUPPLIED. 785 | _group_to_tab( 786 | EXPORTING 787 | agg = r 788 | col = _next_free_col( e_result ) 789 | IMPORTING 790 | tab = e_result ). 791 | ENDIF. 792 | 793 | ENDMETHOD. 794 | 795 | 796 | METHOD third_quartile. 797 | 798 | r = VALUE #( FOR group IN _group_stats 799 | ( group_by = group-group_by 800 | value = group-stats->third_quartile( col ) ) ). 801 | 802 | IF e_result IS SUPPLIED. 803 | _group_to_tab( 804 | EXPORTING 805 | agg = r 806 | col = _next_free_col( e_result ) 807 | IMPORTING 808 | tab = e_result ). 809 | ENDIF. 810 | 811 | ENDMETHOD. 812 | 813 | 814 | METHOD variance. 815 | 816 | r = VALUE #( FOR group IN _group_stats 817 | ( group_by = group-group_by 818 | value = group-stats->variance( col ) ) ). 819 | 820 | IF e_result IS SUPPLIED. 821 | _group_to_tab( 822 | EXPORTING 823 | agg = r 824 | col = _next_free_col( e_result ) 825 | IMPORTING 826 | tab = e_result ). 827 | ENDIF. 828 | 829 | ENDMETHOD. 830 | 831 | 832 | METHOD _get_group_value. 833 | 834 | LOOP AT _grouping INTO DATA(group_field). 835 | 836 | ASSIGN COMPONENT group_field OF STRUCTURE row TO FIELD-SYMBOL(). 837 | 838 | APPEND VALUE #( 839 | group_field = group_field 840 | group_value = ) TO r. 841 | 842 | ENDLOOP. 843 | 844 | ENDMETHOD. 845 | 846 | 847 | METHOD _group_to_tab. 848 | 849 | CLEAR tab. 850 | 851 | DATA row TYPE REF TO data. 852 | CREATE DATA row LIKE LINE OF tab. 853 | ASSIGN row->* TO FIELD-SYMBOL(). 854 | 855 | LOOP AT agg INTO DATA(row_agg). 856 | 857 | CLEAR . 858 | 859 | LOOP AT row_agg-group_by INTO DATA(group). 860 | ASSIGN COMPONENT group-group_field OF STRUCTURE TO FIELD-SYMBOL(). 861 | = group-group_value. 862 | ENDLOOP. 863 | 864 | ASSIGN COMPONENT col OF STRUCTURE TO . 865 | IF sy-subrc NE 0. 866 | RAISE EXCEPTION TYPE zcx_tbox_stats EXPORTING textid = zcx_tbox_stats=>column_group_not_found column = col. 867 | ENDIF. 868 | = row_agg-value. 869 | 870 | INSERT INTO TABLE tab. 871 | 872 | ENDLOOP. 873 | 874 | ENDMETHOD. 875 | 876 | 877 | METHOD _next_free_col. 878 | 879 | DATA(tab_desc) = CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_data( tab ) ). 880 | DATA(line_desc) = CAST cl_abap_structdescr( tab_desc->get_table_line_type( ) ). 881 | 882 | LOOP AT line_desc->get_included_view( ) INTO DATA(comp). 883 | 884 | CHECK NOT line_exists( _grouping[ table_line = comp-name ] ). 885 | 886 | r = comp-name. 887 | RETURN. 888 | 889 | ENDLOOP. 890 | 891 | ENDMETHOD. 892 | 893 | 894 | METHOD _prepare_stats. 895 | 896 | TYPES: BEGIN OF ty_values, 897 | group_by TYPE ty_group_t, 898 | data TYPE REF TO data, 899 | END OF ty_values. 900 | 901 | DATA groups TYPE TABLE OF ty_values WITH DEFAULT KEY. 902 | DATA group LIKE LINE OF groups. 903 | 904 | FIELD-SYMBOLS TYPE STANDARD TABLE. 905 | FIELD-SYMBOLS TYPE STANDARD TABLE. 906 | ASSIGN _data->* TO . 907 | 908 | LOOP AT ASSIGNING FIELD-SYMBOL(). 909 | 910 | DATA(group_values) = _get_group_value( ). 911 | 912 | IF IS ASSIGNED. 913 | UNASSIGN . 914 | ENDIF. 915 | 916 | READ TABLE groups WITH KEY group_by = group_values ASSIGNING FIELD-SYMBOL(). 917 | 918 | IF sy-subrc EQ 0. 919 | 920 | ASSIGN -data->* TO . 921 | INSERT INTO TABLE . 922 | 923 | ELSE. 924 | 925 | CREATE DATA group-data LIKE . 926 | ASSIGN group-data->* TO . 927 | INSERT INTO TABLE . 928 | group-group_by = group_values. 929 | APPEND group TO groups. 930 | 931 | ENDIF. 932 | 933 | ENDLOOP. 934 | 935 | LOOP AT groups INTO group. 936 | 937 | ASSIGN group-data->* TO . 938 | 939 | APPEND VALUE #( 940 | group_by = group-group_by 941 | stats = NEW #( ) 942 | ) TO _group_stats. 943 | 944 | ENDLOOP. 945 | 946 | ENDMETHOD. 947 | ENDCLASS. 948 | -------------------------------------------------------------------------------- /src/ztbox_cl_stats_group.clas.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | ZTBOX_CL_STATS_GROUP 7 | E 8 | TBOX | Statistics 9 | 1 10 | X 11 | X 12 | X 13 | 14 | 15 | 16 | _GROUPING 17 | E 18 | Table of Strings 19 | 20 | 21 | 22 | 23 | ARE_NORMAL 24 | COL 25 | E 26 | Field name 27 | 28 | 29 | COEFFICIENT_VARIATION 30 | COL 31 | E 32 | Field name 33 | 34 | 35 | COL 36 | COL 37 | E 38 | Field name 39 | 40 | 41 | COL 42 | R 43 | E 44 | TBOX | Statistics 45 | 46 | 47 | COL 48 | ZCX_TBOX_STATS 49 | E 50 | TBOX | Stats Exceptions 51 | 52 | 53 | CONSTRUCTOR 54 | GROUPING 55 | E 56 | Table of Strings 57 | 58 | 59 | CORRELATION 60 | COLS 61 | E 62 | Field name 63 | 64 | 65 | CORRELATION 66 | ZCX_TBOX_STATS 67 | E 68 | TBOX | Stats Exceptions 69 | 70 | 71 | COUNT_DISTINCT 72 | COL 73 | E 74 | Field name 75 | 76 | 77 | COUNT_NOT_INITIAL 78 | COL 79 | E 80 | Field name 81 | 82 | 83 | COVARIANCE 84 | COLS 85 | E 86 | Field name 87 | 88 | 89 | COVARIANCE 90 | ZCX_TBOX_STATS 91 | E 92 | TBOX | Stats Exceptions 93 | 94 | 95 | DISPERSION_INDEX 96 | COL 97 | E 98 | Field name 99 | 100 | 101 | FIRST_QUARTILE 102 | COL 103 | E 104 | Field name 105 | 106 | 107 | GEOMETRIC_MEAN 108 | COL 109 | E 110 | Field name 111 | 112 | 113 | HARMONIC_MEAN 114 | COL 115 | E 116 | Field name 117 | 118 | 119 | INTERQUARTILE_RANGE 120 | COL 121 | E 122 | Field name 123 | 124 | 125 | KURTOSIS 126 | COL 127 | E 128 | Field name 129 | 130 | 131 | MAD_MEAN 132 | COL 133 | E 134 | Field name 135 | 136 | 137 | MAD_MEDIAN 138 | COL 139 | E 140 | Field name 141 | 142 | 143 | MAX 144 | COL 145 | E 146 | Field name 147 | 148 | 149 | MEAN 150 | COL 151 | E 152 | Field name 153 | 154 | 155 | MEDIAN 156 | COL 157 | E 158 | Field name 159 | 160 | 161 | MIN 162 | COL 163 | E 164 | Field name 165 | 166 | 167 | MIN 168 | ZCX_TBOX_STATS 169 | E 170 | TBOX | Stats Exceptions 171 | 172 | 173 | QUADRATIC_MEAN 174 | COL 175 | E 176 | Field name 177 | 178 | 179 | RANGE 180 | COL 181 | E 182 | Field name 183 | 184 | 185 | SECOND_QUARTILE 186 | COL 187 | E 188 | Field name 189 | 190 | 191 | SKEWNESS 192 | COL 193 | E 194 | Field name 195 | 196 | 197 | STANDARD_DEVIATION 198 | COL 199 | E 200 | Field name 201 | 202 | 203 | SUM 204 | COL 205 | E 206 | Field name 207 | 208 | 209 | THIRD_QUARTILE 210 | COL 211 | E 212 | Field name 213 | 214 | 215 | VARIANCE 216 | COL 217 | E 218 | Field name 219 | 220 | 221 | _GROUP_TO_TAB 222 | COL 223 | E 224 | Field name 225 | 226 | 227 | _GROUP_TO_TAB 228 | ZCX_TBOX_STATS 229 | E 230 | TBOX | Stats Exceptions 231 | 232 | 233 | _NEXT_FREE_COL 234 | R 235 | E 236 | Field name 237 | 238 | 239 | 240 | 241 | 242 | --------------------------------------------------------------------------------