小编Ran*_*l J的帖子

Cython没有速度提升

我试图定义一个包含内部循环的函数来模拟积分.

问题是速度.在我的机器上评估该功能一次最多可能需要30秒.由于我的最终目标是最小化这个功能,一些额外的速度会很好.

因此,我试图让Cython工作,但我必须犯一个严重的错误(可能很多人!).在Cython文档之后,我尝试输入我的变量.这样做之后,代码就像纯Python一样慢.这看起来很奇怪.

这是我的代码:

import numpy as np 
cimport cython
cimport numpy as np
import minuit

data = np.genfromtxt('q6data.csv', usecols = np.arange(1, 24, 1), delimiter = ',')  

cdef int ns    = 1000                 # Number of simulation draws
cdef int K     = 5                    # Number of observed characteristics, including            constant
cdef int J     = len(data[:,1])       # Number of products, including outside
cdef double tol   = 0.0001            # Inner GMM loop tolerance
nu = np.random.normal(0, 1, (6, ns))  # ns random deviates …
Run Code Online (Sandbox Code Playgroud)

python optimization numpy cython

24
推荐指数
5
解决办法
8220
查看次数

修改函数内的全局变量

我已经定义了以下功能:

def GMM(s1, s2, s3, s4, s5, a):
    """The GMM objective function.

    Arguments
    ---------
        si: float
            standard deviations of preference distribution

        a: float
            marginal utility of residutal income

    Paramters
    ---------
        Px: array (1,ns) 
            projector onto nonprice characteristic space

        xk, z: arrays (J, 5) and (J, 12)
            nonprice char. and instruments

        invW: array (12, 12) 
            GMM weight matrix

    Returns
    -------
        float."""
    delta = invert(s1, s2, s3, s4, s5, a, delta0) # Invert market shares to get mean utility
    bmean = np.dot(Px, …
Run Code Online (Sandbox Code Playgroud)

python

12
推荐指数
2
解决办法
3万
查看次数

在Cython中使用lambda函数时出错

我正在尝试使用Cython来加速一段代码.当我使用lambda函数时,Cython会给出一个错误,上面写着"Expected a identifier或literal".据我所知,lambda函数在Cython 0.13中得到支持.我在这一点上不正确吗?如果它们确实受到支持,我是否需要做一些其他事情而不是我在这里实施它们?

def f(e_1, e_2, rho):
    """Bivariate Normal pdf with mean zero, unit variances, and correlation coefficient rho."""
    return (1.0 / (2.0 * pi * sqrt(1 - rho**2))) * exp(-(1.0 / (2*(1 - rho**2))) * (e_1**2 + e_2**2 - 2*rho*e_1*e_2))

def P_zero(b_10, b_11, b_20, b_21, rho, gamma, x):
    """Returns the probability of observing zero entrants in a market by numerically
    integrating out the unobserved firm-specific profit shocks."""
    h_z = lambda e_1: -inf
    g_z = lambda e_1: -b_10 - …
Run Code Online (Sandbox Code Playgroud)

python cython

6
推荐指数
1
解决办法
2225
查看次数

标签 统计

python ×3

cython ×2

numpy ×1

optimization ×1