您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中的统计引导程序库?

5b51 2022/1/14 8:20:46 python 字数 3973 阅读 457 来源 www.jb51.cc/python

Python中是否有统计引导程序库?我希望功能类似于R bootstrap中提供的功能:http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm搜索我发现:http://mjtokelly.blogspot.com/2006/04/bootstrap-statistics-in-python.ht

概述

Python中是否有统计引导程序库?

我希望功能类似于R bootstrap中提供的功能

http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm

搜索我发现:

http://mjtokelly.blogspot.com/2006/04/bootstrap-statistics-in-python.html(代码链接被破坏)

http://adorio-research.org/wordpress/?p=9048

https://github.com/cgevans/scikits-bootstrap

但是上面的这些似乎并没有提供所有功能(特别是概率权重).

有什么指针吗?

这最近被添加numpy.random

谢谢

import collections
import random
import bisect

def sample(xs,sample_size = None,replace=False,sample_probabilities = None):
    """Mimics the functionality of http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm sample()"""

    if not isinstance(xs,collections.Iterable):
        xs = range(xs)
    if not sample_size:
        sample_size = len(xs)            

    if not sample_probabilities:
        if replace:
            return [random.choice(xs) for _ in range(sample_size)]
        else:
            return random.sample(xs,sample_size)
    else:
        if replace:
            total,cdf = 0,[]
            for x,p in zip(xs,sample_probabilities):
                total += p
                cdf.append(total)

            return [ xs[ bisect.bisect(cdf,random.uniform(0,total)) ] 
                    for _ in range(sample_size) ]
        else:            
            assert len(sample_probabilities) == len(xs)
            xps = list(zip(xs,sample_probabilities))           
            total = sum(sample_probabilities)
            result = []
            for _ in range(sample_size):
                # choose an item based on weights,and remove it from future iterations.
                # this is slow (N^2),a tree structure for xps would be better (NlogN)
                target = random.uniform(0,total)
                current_total = 0                
                for index,(x,p) in enumerate(xps):
                    current_total += p
                    if current_total > target:
                        xps.pop(index)
                        result.append(x)
                        total -= p
                        break
            return result

总结

以上是编程之家为你收集整理的Python中的统计引导程序库?全部内容,希望文章能够帮你解决Python中的统计引导程序库?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶