Cython中的结构指针与GSL蒙特卡洛最小化

时间:2017-01-10 19:25:32

标签: python pointers struct cython gsl

我坚持这个练习,并不足以解决它。基本上我正在为伯努利分布编写蒙特卡罗最大似然算法。问题是我必须将数据作为参数传递给GSL最小化(one-dim)算法,并且还需要传递数据的大小(因为外部循环是&#34的不同样本大小;观察"数据)。所以我试图将这些参数作为结构传递。但是,我遇到了seg错误,我确定它来自与结构有关的代码部分并将其视为指针。

[编辑:我已更正结构及其组件的分配]

%%cython

#!python
#cython: boundscheck=False, wraparound=False, nonecheck=False, cdivision=True   

from libc.stdlib cimport rand, RAND_MAX, calloc, malloc, realloc, free, abort
from libc.math cimport log

#Use the CythonGSL package to get the low-level routines
from cython_gsl cimport *

######################### Define the Data Structure ############################

cdef struct Parameters:
    #Pointer for Y data array
    double* Y
    #size of the array
    int* Size

################ Support Functions for Monte-Carlo Function ##################

#Create a function that allocates the memory and verifies integrity
cdef void alloc_struct(Parameters* data, int N, unsigned int flag) nogil:

    #allocate the data array initially
    if flag==1:
        data.Y = <double*> malloc(N * sizeof(double))
    #reallocate the data array
    else:
        data.Y = <double*> realloc(data.Y, N * sizeof(double))

    #If the elements of the struct are not properly allocated, destory it and return null
    if N!=0 and data.Y==NULL:
        destroy_struct(data)
        data = NULL     

#Create the destructor of the struct to return memory to system
cdef void destroy_struct(Parameters* data) nogil:
    free(data.Y)
    free(data)

#This function fills in the Y observed variable with discreet 0/1
cdef void Y_fill(Parameters* data, double p_true, int* N) nogil:

    cdef:
        Py_ssize_t i
        double y

    for i in range(N[0]):

        y = rand()/<double>RAND_MAX

        if y <= p_true:
            data.Y[i] = 1 
        else:
            data.Y[i] = 0
#Definition of the function to be maximized: LLF of Bernoulli
cdef double LLF(double p, void* data) nogil:

    cdef:
        #the sample structure (considered the parameter here)
        Parameters* sample

        #the total of the LLF
        double Sum = 0

        #the loop iterator
        Py_ssize_t i, n

    sample = <Parameters*> data

    n = sample.Size[0]

    for i in range(n):

        Sum += sample.Y[i]*log(p) + (1-sample.Y[i])*log(1-p)

    return (-(Sum/n))

########################## Monte-Carlo Function ##############################

def Monte_Carlo(int[::1] Samples, double[:,::1] p_hat, 
                Py_ssize_t Sims, double p_true):

    #Define variables and pointers
    cdef:
        #Data Structure
        Parameters* Data

        #iterators
        Py_ssize_t i, j
        int status, GSL_CONTINUE, Iter = 0, max_Iter = 100 

        #Variables
        int N = Samples.shape[0] 
        double start_val, a, b, tol = 1e-6

        #GSL objects and pointer
        const gsl_min_fminimizer_type* T
        gsl_min_fminimizer* s
        gsl_function F

    #Set the GSL function
    F.function = &LLF

    #Allocate the minimization routine
    T = gsl_min_fminimizer_brent
    s = gsl_min_fminimizer_alloc(T)

    #allocate the struct
    Data = <Parameters*> malloc(sizeof(Parameters))

    #verify memory integrity
    if Data==NULL: abort()

    #set the starting value
    start_val = rand()/<double>RAND_MAX

    try:

        for i in range(N):

            if i==0:
                #allocate memory to the data array
                alloc_struct(Data, Samples[i], 1)
            else:
                #reallocate the data array in the struct if 
                #we are past the first run of outer loop
                alloc_struct(Data, Samples[i], 2)

            #verify memory integrity
            if Data==NULL: abort()

            #pass the data size into the struct
            Data.Size = &Samples[i]

            for j in range(Sims):

                #fill in the struct
                Y_fill(Data, p_true, Data.Size)

                #set the parameters for the GSL function (the samples)
                F.params = <void*> Data
                a = tol
                b = 1

                #set the minimizer
                gsl_min_fminimizer_set(s, &F, start_val, a, b)

                #initialize conditions
                GSL_CONTINUE = -2
                status = -2

                while (status == GSL_CONTINUE and Iter < max_Iter):

                    Iter += 1
                    status = gsl_min_fminimizer_iterate(s)

                    start_val = gsl_min_fminimizer_x_minimum(s)
                    a = gsl_min_fminimizer_x_lower(s)
                    b = gsl_min_fminimizer_x_upper(s)

                    status = gsl_min_test_interval(a, b, tol, 0.0)

                    if (status == GSL_SUCCESS):
                        print ("Converged:\n")
                        p_hat[i,j] = start_val

    finally:
        destroy_struct(Data)
        gsl_min_fminimizer_free(s)

使用以下python代码运行上述函数:

import numpy as np

#Sample Sizes
N = np.array([5,50,500,5000], dtype='i')

#Parameters for MC
T = 1000
p_true = 0.2

#Array of the outputs from the MC
p_hat = np.empty((N.size,T), dtype='d')
p_hat.fill(np.nan)

Monte_Carlo(N, p_hat, T, p_true)

我已经分别测试了结构分配,它可以工作,做它应该做的事情。然而,当蒙特卡洛开心时,内核会被中止调用(根据我的Mac上的输出)杀死,而我的控制台上的Jupyter输出如下:

gsl: fsolver.c:39: ERROR: computed function value is infinite or NaN

调用默认GSL错误处理程序。

现在似乎解算器无法正常工作。我不熟悉GSL包,只使用它一次从gumbel发行版生成随机数(绕过scipy命令)。

我将不胜感激任何帮助!感谢

[编辑:改变a的下限]

使用指数分布重做练习,其对数似然函数仅包含一个日志我已经研究了gsl_min_fminimizer_set最初在0的下限处得到-INF的问题结果(因为它在求解生成f(下)之前评估问题,f(上)其中f是我的优化函数)。当我将下限设置为0以外的其他值但非常小(比如我定义的容差的tol变量)时,求解算法会起作用并产生正确的结果。

非常感谢@DavidW提示让我到达我需要去的地方。

1 个答案:

答案 0 :(得分:0)

这是一个有点推测性的答案,因为我没有安装GSL,所以很难对它进行测试(如果它错了就道歉!)

我认为问题在于

Sum += sample.Y[i]*log(p) + (1-sample.Y[i])*log(1-p)

看起来Y[i]可以是0或1.当p位于0-1范围的任一端时,它会给出0*-inf = nan。在只有所有&#39; Y'相同的情况下,该点是最小值(因此求解器将可靠地结束于无效点)。幸运的是,您应该能够重写该行以避免获得nan

if sample.Y[i]:
   Sum += log(p)
else:
   Sum += log(1-p)

(将生成nan的情况是未执行的情况)。

我发现了第二个小问题:如果出现错误,alloc_struct data = NULLNULL。这只会影响本地指针,因此Monte_Carlo中对alloc_struct的测试毫无意义。你最好从A log(p) + (1-A) log (1-p)返回真假标志并检查一下。我怀疑你是否会遇到这个错误。

修改:另一个更好的选择是分析性地找到最小值:A/p - (1-A)/(1-p)的导数是sample.Y。找到A的所有p=A的平均值。找到导数为0的位置给出Foo(你想要仔细检查我的工作!)。有了这个,您可以避免使用GSL最小化例程。