二进制Markov-K随机发生器

时间:2018-12-16 15:33:40

标签: python numpy random scipy generator

Hello Stackoverflow社区,

目前,我正在研究熵编码器(MQ编码器)实现(cython包装器和内部c源代码)。要创建测试设置,我想使用二进制markov-k随机生成器,该生成器输出numpy数组作为编码器的输入。在python,numpy,scipy或tensorflow中实现这种生成器的最简单方法是什么?转移概率p(x | x1,...,xk)和阶数k应该可以指定。

预先感谢, 子午线

1 个答案:

答案 0 :(得分:0)

FYI:这会根据输入的[ascii]位生成一个概率表[256]。

用法:cat*.c| ./a.out

;-)


#include <stdio.h>

struct cell     {
        unsigned nhit;
        unsigned ones;
        } cells[256] ={{0,0},};

int main(void)
{
unsigned state, newstate, bit,val;

int ch;

for(state=0;    1; ){
        ch=getc(stdin);
        if(ch==EOF)break;
        for(bit=8; bit-- ;state=newstate & 0xff ){
                val= ch & bit? 1: 0;
                if(val) cells[state].ones += 1;
                cells[state].nhit += 1;
                newstate= (state <<1) | val;
                }
        }

for(state=0; state< 256; state++ ){
        if (    cells[state].nhit < 1)continue;
        fprintf(stdout, "%2x: %u / %u (%lf)\n"
                , state
                , cells[state].ones , cells[state].nhit
                , (0.0+cells[state].ones) / cells[state].nhit
                );
        }

return 0;
}

很高兴看到大多数值为0或1;或接近它;或接近0.5。