SQLite的标准偏差 [英] Standard Deviation for SQLite

查看:220
本文介绍了SQLite的标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了SQLite文档并找不到任何东西,但我也在Google上搜索过,并且出现了一些结果。

SQLite有没有内置标准偏差函数?

解决方案

我实现了 Welford的方法(与 extension-functions.c )作为SQLite UDF:

  function(& $ context,$ row,$ data)// step callback 
{
if(isset($ context )!== true)// $ context首先为空
{
$ context = array

'k'=> 0,
'm '=> 0,
's'=> 0,
);
}

if(isset($ data)=== tru e)//标准是非空值仅
{
$ context ['s'] + =($ data - $ context ['m'])*($ data - ($ context ['m'] + =($ data - $ context ['m'])/ ++ $ context ['k']));
}

return $ context;
},
函数(& $ context,$ row)// fini回调函数
{
if($ context ['k']> 0)// return NULL如果不存在非NULL值
{
return sqrt($ context ['s'] / $ context ['k']);
}

返回null;
},
1);

这是PHP( $ db PDO对象),但它应该是微不足道的移植到另一种语言。



SQLite是 soooo 酷。 < 3


I've searched the SQLite docs and couldn't find anything, but I've also searched on Google and a few results appeared.

Does SQLite have any built-in Standard Deviation function?

解决方案

I implemented the Welford's method (the same as extension-functions.c) as a SQLite UDF:

$db->sqliteCreateAggregate('stdev',
    function (&$context, $row, $data) // step callback
    {
        if (isset($context) !== true) // $context is null at first
        {
            $context = array
            (
                'k' => 0,
                'm' => 0,
                's' => 0,
            );
        }

        if (isset($data) === true) // the standard is non-NULL values only
        {
            $context['s'] += ($data - $context['m']) * ($data - ($context['m'] += ($data - $context['m']) / ++$context['k']));
        }

        return $context;
    },
    function (&$context, $row) // fini callback
    {
        if ($context['k'] > 0) // return NULL if no non-NULL values exist
        {
            return sqrt($context['s'] / $context['k']);
        }

        return null;
    },
1);

That's in PHP ($db is the PDO object) but it should be trivial to port to another language.

SQLite is soooo cool. <3

这篇关于SQLite的标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆