在C中生成幂律分布并使用python进行测试

时间:2017-08-14 22:47:51

标签: python c random data-fitting

我知道,给定一个生成随机数均匀分布的rng,获得类似幂的数据的方法是Wolfram Mathworld以下:让y是一个均匀分布在(0,1)中的随机变量和另一个随机变量分布为P(x)= C * x ** n(对于x in(xmin,xmax))。我们有那个

x=[ (xmax**(n+1) - xmin**(n-1))y+xmin**(n+1)  ]**(1/(n+1))

所以我在C中创建了这个程序,从1到100生成50k数字,应该分配为x ^( - 2)并在文件DATA.txt上打印结果的频率:

void random_powerlike(int *k, int dim,  double degree, int xmin, int xmax, unsigned int *seed)
{
int i; 
double aux;
for(i=0; i<dim; i++)
    {
    aux=(powq(xmax, degree +1 ) - powq(xmin, degree +1 ))*((double)rand_r(seed)/RAND_MAX)+ powq(xmin, degree +1);

    k[i]=(int) powq(aux, 1/(degree+1));

    }
}

int main()
{
    unsigned int seed = 1934123471792583;

    FILE *tmp; 
    char  stringa[50];
    sprintf(stringa, "Data.txt");
    tmp=fopen(stringa, "w");

    int dim=50000;
    int *k;
    k= (int *) malloc(dim*sizeof(int));
    int degree=-2;
    int freq[100];  

    random_powerlike(k,dim, degree, 1,100,&seed);
    fprintf(tmp, "#degree = %d  x=[%d,%d]\n",degree,1,100);
    for(int j=0; j< 100;j++)
    {   
        freq[j]=0;
        for(int i = 0; i< dim; ++i)
        {
            if(k[i]==j+1)
            freq[j]++;
        }
        fprintf(tmp, "%d    %d\n", j+1, freq[j]);
    }
    fflush(tmp);
    fclose(tmp);

return 0;
}

我决定用pylab拟合这些数字,看看适合它们的最佳幂律是否为* x ** b,b = -2。我在python中编写了这个程序:

import numpy
from scipy.optimize import curve_fit
import pylab

num, freq = pylab.loadtxt("Data.txt", unpack=True)
freq=freq/freq[0]

def funzione(num, a,b):
    return a*num**(b)

pars, covm =  curve_fit(funzione, num, freq, absolute_sigma=True)
xx=numpy.linspace(1, 99)
pylab.plot(xx, funzione(xx, pars[0],pars[1]), color='red')
pylab.errorbar(num, freq, linestyle='', marker='.',color='black')
pylab.show()
print pars

问题在于,当我拟合数据时,我得到的指数值为〜-1.65。

我认为我在某个地方犯了一个错误,但我无法弄清楚在哪里。

1 个答案:

答案 0 :(得分:1)

我认为你必须制作直方图。我刚刚重写了你的代码,现在非常适合

<div class="flex">
  
  <!--"row"-->
  <div class="col1">1</div>
  <div class="col2">2</div>
  <div class="col3">3</div>
  <div class="col4">4</div>
  
  <!--"row"-->
  <div class="col1">1</div>
  <div class="col2">2</div>
  <div class="col3">3</div>
  <div class="col4">4</div>
  
  <!--"row"-->
  <div class="col1">1</div>
  <div class="col2">2</div>
  <div class="col3">3</div>
  <div class="col4">4</div>
  
</div>

和拟合代码

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

double rndm() {
    return (double)rand()/(double)RAND_MAX;
}

double power_sample(double xmin, double xmax, int degree) {
    double pmin = pow(xmin, degree + 1);
    double pmax = pow(xmax, degree + 1);
    double v = pmin + (pmax - pmin)*rndm();
    return pow(v, 1.0/(degree + 1));
}

int main() {
    unsigned int seed = 32345U;
    srand(seed);

    int xmin = 1;
    int xmax = 100;

    double* hist = malloc((xmax-xmin + 1)*sizeof(double));
    memset(hist, 0, (xmax-xmin + 1)*sizeof(double));

    // sampling
    int nsamples = 100000000;
    for(int k = 0; k != nsamples; ++k) {
        double v = power_sample(xmin, xmax, 2);
        int idx = (int)v;
        hist[idx] += 1.0;
    }

    // normalization
    for(int k = xmin; k != xmax; ++k) {
        hist[k] /= (double)nsamples;
    }

    // output
    for(int k = xmin; k != xmax; ++k) {
        double x = k + 0.5;
        printf(" %e     %e\n", x, hist[k]);
    }

    free(hist); // cleanup

    return 0;
}

它产生了

import numpy
from scipy.optimize import curve_fit
import pylab

def funzione(x, a,b):
    return a * numpy.power(x, b)

num, freq = pylab.loadtxt("q.dat", unpack=True)

pars, covm =  curve_fit(funzione, num, freq, absolute_sigma=True)
pylab.plot(num, funzione(num, pars[0], pars[1]), color='red')
pylab.errorbar(num, freq, linestyle='', marker='.',color='black')
pylab.show()
print(pars)

非常接近