0

I am working with some example online. I wanted to run the following values with different values of B0 and D0 but it is assigned by the anonymous function lambda. How can I modify

alpha,beta,loc,scale = stats.beta.fit(value)  

error=(scale/(1.96))**2

gpdf = lambda B0, mu, sigma2: 1/np.sqrt(2*pi*sigma2)*np.exp(-1/2*((B0-mu)**2)/sigma2)
approx_sigma2 = lambda scale: (scale/(1.96))**2
ggpdf_v  = lambda B0, D0, error: gpdf(B0, mu=0.8, sigma2=error) * (D0 < 3) + (D0 >= 3) * gpdf(B0, mu=0.5, sigma2=error)
ggpdf_r  = lambda B0, D0, error: gpdf(B0, mu=0.5, sigma2=error)
ggpdf_c  = lambda B0, D0, error: gpdf(B0, mu=0.7, sigma2=error)
ggpdf_v  = lambda B0, D0, error: gpdf(B0, mu=0.9, sigma2=error)
2
  • 1
    Don't use lambda like this. If you are going to name the function use a def statement. Commented Mar 14, 2019 at 19:29
  • I tried that but I was getting a lot of errors. Can you post your answer below and thank you. Commented Mar 14, 2019 at 19:39

1 Answer 1

1

B0 is a parameter in each of those lambdas. Just like a regular function, you can specify whatever value you want for each parameter when you call it. For example, gpdf(4, 8, 15) sets B0 to 4, mu to 8, and sigma2 to 15.

Sign up to request clarification or add additional context in comments.

2 Comments

Can I assign those values outside of the function?
Depends on what you mean by "the function". If you mean "can I assign values outside of the gpdf function?", then yes, that is exactly what my answer describes. If you mean "The entire code sample in my question is inside a function. Can I access gpdf from outside that function?", you can't unless the function explicitly makes gpdf available somehow, perhaps by returning it or assigning it to a variable/attribute in a higher scope. This is not a special rule pertaining to lambdas; that's how all Python variables work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.