aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/AggregateFunctions/AggregateFunctionCramersVBiasCorrected.cpp
blob: 917869dcd9ffdb77fa0c3b7d1283d12cfe81bcbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/CrossTab.h>
#include <AggregateFunctions/FactoryHelpers.h>
#include <memory>
#include <cmath>


namespace DB
{

namespace
{

struct CramersVBiasCorrectedData : CrossTabData
{
    static const char * getName()
    {
        return "cramersVBiasCorrected";
    }

    Float64 getResult() const
    {
        if (count < 2)
            return std::numeric_limits<Float64>::quiet_NaN();

        Float64 phi = getPhiSquared();

        Float64 a_size_adjusted = count_a.size() - 1;
        Float64 b_size_adjusted = count_b.size() - 1;
        Float64 count_adjusted = count - 1;

        Float64 res = std::max(0.0, phi - a_size_adjusted * b_size_adjusted / count_adjusted);
        Float64 correction_a = count_a.size() - a_size_adjusted * a_size_adjusted / count_adjusted;
        Float64 correction_b = count_b.size() - b_size_adjusted * b_size_adjusted / count_adjusted;

        res /= std::min(correction_a, correction_b) - 1;
        return sqrt(res);
    }
};

}

void registerAggregateFunctionCramersVBiasCorrected(AggregateFunctionFactory & factory)
{
    factory.registerFunction(CramersVBiasCorrectedData::getName(),
        [](const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings *)
        {
            assertBinary(name, argument_types);
            assertNoParameters(name, parameters);
            return std::make_shared<AggregateFunctionCrossTab<CramersVBiasCorrectedData>>(argument_types);
        });
}

}