No speed gains from Cython

后端 未结 7 844
不思量自难忘°
不思量自难忘° 2021-01-30 14:05

I am trying to define a function that contains an inner loop for simulating an integral.

The problem is speed. Evaluating the function once can take up to 30 seconds on

7条回答
  •  难免孤独
    2021-01-30 14:45

    Taking the advice given here, I have spent more time profiling the above code. To hopefully clean things up a bit I defined

    I have profiled the code a bit more and have a better idea of which pieces are the slowest. I additionally defined

    X = data[:, 2:7]
    m_y = data[:, 21].reshape(J,1)
    sigma_y = 1.0
    price = data[:, 7].reshape(J, 1)
    shares_data = data[:,8]
    

    Then it is the following lines that are eating up most of the total time.

    mu_ij = np.dot((X*np.array([s1, s2, s3, s4, s5])), nu[1:K+1,:])
    mu_y  = a * np.log(np.exp(m_y + sigma_y*nu[0,:].reshape(1,ns)) - price)
    V = delta.reshape(J,1) + mu_ij + mu_y
    exp_vi = np.exp(V)
    P_i = (1.0 / np.sum(exp_vi[np.where(data[:,1]==71)], 0)) *  exp_vi[np.where(data[:,1]==71)] 
    for yr in xarange(19):
        P_yr = (1.0 / np.sum(exp_vi[np.where(data[:,1]==yr)], 0)) * exp_vi[np.where(data[:,1]==yr)]
    P_i  = np.concatenate((P_i, P_yr))
    

    I get the impression this is an overly cumbersome way to achieve my goal. I was hoping somebody might be able to provide some advice on how to speed these lines up. Maybe there are Numpy capabilities I am missing? If this problem is not sufficiently well specified for you to be helpful, I would be happy to provide more details on the context of my problem. Thanks!

提交回复
热议问题