Dec-09-2017, 07:44 PM
Okay, so I'm working on a rather complicated project. I'm having trouble creating a loop that returns the same lognorm function with different start times. Can anyone offer some advice on how I could make this work? Even though its not changing I don't really just want to seperate it out into twenty different functions, because that would be really messy and confusing to read. I do concede, though, that this might be the only way to make it work.
This shows what I want it to do:
import numpy as np from scipy.stats import lognorm import matplotlib.pyplot as plt from math import exp def Veganuary(StartQ_A1, EndQ_A1, CurrentQ_A1, Mean_A1, Cum_impact_A1, Peak_factor_A1): Scale_A1=exp(Mean_A1) return lognorm.cdf(CurrentQ_A1, Peak_factor_A1, StartQ_A1, Scale_A1)*Cum_impact_A1 def runVeganuary(): #loop to run Veganuary at 4 quarter intervals Test_time=52 while Test_time < 400: A1_time = np.linspace(Test_time, Test_time+4, num=4000) fig, A1 = plt.subplots(1, 1) A1.set_title('Veganuary') A1.xlabel('$Quarter$') A1.ylabel('Percent Change') A1.plot(A1_time, Veganuary(Test_time, (Test_time + 1), A1_time, 0.006, 0.012, 0.55), 'p-', lw=5, alpha=0.6, label='Veganuary Intervention') Test_time = Test_time + 4 return Veganuary(Test_time, (Test_time + 1), A1_time, 0.006, 0.012, 0.55)But I need to be able to subtract these values in a more complex function. Here's what I have so far:
import numpy as np from scipy.stats import lognorm import matplotlib.pyplot as plt from math import exp fig, ax = plt.subplots(1, 1) def Veganuary(StartQ_A1, EndQ_A1, CurrentQ_A1, Mean_A1, Cum_impact_A1, Peak_factor_A1): Scale_A1=exp(Mean_A1) return lognorm.cdf(CurrentQ_A1, Peak_factor_A1, StartQ_A1, Scale_A1)*Cum_impact_A1 def runVeganuaryMain(x): testTime=52 while testTime < 400: testTime = testTime + 4 return Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55) x= np.linspace(0, 216, num=400) ax.set_title('cumulative decrease in consumption') ax.set_xlabel('$Quarter$') ax.set_ylabel('$Percent of the Population$') ax.plot(x, ((initialPop * np.exp(popGrowth * (x/4)))-(runVeganuaryMain(x)*(initialPop * np.exp(popGrowth * (x/4))))), 'p-', lw=1, alpha=0.6, label='Major Graph')It's only running once and not looping. Is there a way for me to make this loop without having to call the function forty times? (Final note, I also have a bunch of similar functions that don't loop in this equation. They all work correctly).