如何在PostgreSQL中计算二项分布

kq0g1dla  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(2)|浏览(143)

我有一个简单的计算二项分布的函数

CREATE OR REPLACE FUNCTION random_binomial(n int, p float)
RETURNS int AS $$
DECLARE
    -- Loop variable
    i int;
    -- Result of the function
    result float = 0;
BEGIN
    IF n <= 0 OR p <= 0.0 OR p >= 1.0 THEN
        RETURN NULL;
    END IF;
    FOR i IN 1..n LOOP
        IF random() < p THEN
            result = result + 1;
        END IF;
    END LOOP;
    RETURN result;
END;
$$ LANGUAGE plpgsql STRICT;

在做模拟的时候,我需要调用这个函数上千次,这使得这个过程相当慢。在PostgreSQL中有没有更聪明的方法来计算二项分布?

ct3nt3jp

ct3nt3jp1#

PostgreSQL允许编写C函数,这些函数可以很容易地集成到您的应用程序中(当然,假设您知道用C编写代码):它应该比PL/PGSQL快。
请参阅详细文档:https://www.postgresql.org/docs/current/xfunc-c.html

jmp7cifd

jmp7cifd2#

请尝试以下方法:
1.为创建具有下限和上限的框
1.从表的所需列开始计数,并将每个计数放入收集箱中
1.在执行此操作时,请确保在连接时保留bin的所有数据(下限和上限)(bin的某些值的计数可以为NULL,但反之则不行)。

-- Create Bins with upper and lower bound
WITH bins AS (
      SELECT generate_series(2200, 3050, 50) AS lower,
             generate_series(2250, 3100, 50) AS upper),
     -- Take the counts from desired table
     temp_table AS (
      SELECT question_count 
        FROM table_name) 
-- Select columns for result
SELECT lower, upper, count(question_count) 
  FROM bins  -- Created above
       -- Join to temp_table (created above), 
       -- keeping all rows from the bins table in the join
       LEFT JOIN temp_table 
       -- Compare question_count to lower and upper
         ON question_count >= lower 
        AND question_count < upper
 -- Group by lower and upper to count values in each bin
 GROUP BY lower, upper
 -- Order by lower to put bins in order
 ORDER BY lower;

相关问题