How do i avoid using for loop?

Issue

This Content is from Stack Overflow. Question asked by Purba Banerjee

I am trying to write a code but my code involves a lot of for loops which makes the computation extremely slow. Below is one such example of a for loop that i have used. Can someone please suggest me how do i modify this to avoid for loop altogether?

K11=np.zeros(5)
W11=np.zeros(5)
for a in range(5):
    #first set of strike points and weights corresponding to u1
    K11[a]=strike*np.exp(x[a]*sigma*np.sqrt(2*(T-u1))+(div-r-(0.5*(sigma**2)))*(T-u1))#formula 1
    d=(np.log(K11[a] / strike) + (r-div + 0.5 * sigma ** 2) * (T-u1)) / (sigma * np.sqrt(T-u1))#formula 2
    wj=((si.norm.pdf(d))/(K11[a]*sigma*np.sqrt(T-u1)))*np.exp((-div)*(T-u1))#formula 3
    W11[a]=w[a]*(wj*K11[a]*sigma*np.sqrt(2*(T-u1)))/(np.exp(-(x[a]**2)))#formula 4

Any help is highly appreciated. Thanks in advance!



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?