Posts: 23
Threads: 10
Joined: Dec 2017
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).
Posts: 33
Threads: 3
Joined: Dec 2017
Dec-09-2017, 08:25 PM
(This post was last modified: Dec-09-2017, 08:25 PM by ezdev.)
Quote:It's only running once and not looping
your return is exiting the loop on the first run-- remove 4 spaces or one tab.
def runVeganuaryMain(x):
testTime=52
while testTime < 400:
testTime = testTime + 4
#-> return Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55)
Posts: 23
Threads: 10
Joined: Dec 2017
Here's what I'm trying now. It's returning a bunch of pretty colors in a graph, but nothing particularly useful. import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp
fig, ax = plt.subplots(1, 1)
initialPop = 326300000
popGrowth = 0.007
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=48
timeVariables = ['a', 'b', 'c', 'd', 'e', 'f' 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj', 'kk', 'll', 'mm', 'nn', 'oo', 'pp', 'qq', 'rr', 'ss', 'tt', 'uu', 'vv', 'ww', 'xx', 'yy', 'zz', 'aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh', 'iii', 'jjj', 'kkk', 'lll', 'mmm', 'nnn', 'ooo', 'ppp', 'qqq', 'rrr', 'sss', 'ttt', 'uuu', 'vvv', 'www', 'xxx', 'yyy', 'zzz','aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff', 'gggg', 'hhhh', 'iiii', 'jjjj', 'kkkk', 'llll', 'mmmm']
iteration = 1
while testTime < 400:
testTime = testTime + 4
timeVariables[iteration] = Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55)
iteration = iteration + 1
return timeVariables[1:87]
x= np.linspace(0, 216, num=86)
ax.set_title('cumulative decrease in consumption')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Percent of the Population$')
#ax.plot(x, (runVeganuaryMain(x)*initialPop * np.exp(popGrowth * (x/4))), 'p-', lw=1, alpha=0.6, label='Major Graph')
ax.plot(x, ((float(initialPop) * np.exp(popGrowth * (x/4)))-(runVeganuaryMain(x)*(float(initialPop) * np.exp(popGrowth * (x/4))))), 'p-', lw=1, alpha=0.6, label='Major Graph')
Posts: 23
Threads: 10
Joined: Dec 2017
I finally made it work! Thanks for giving me some tips!
I ended up creating a list and appending each run of the function to that list. Originally I just returned the list, but then I realized I needed to return the sum of the list so here's what ended up working:
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp
fig, ax = plt.subplots(1, 1)
initialPop = 326300000
popGrowth = 0.007
def Veganuary(StartQ_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):
veg = []
tTime = 48
while tTime < 400:
tTime = tTime + 4
veg.append(Veganuary(tTime, x, 0.006, 0.012, 0.55))
return sum(veg[1:87])
x= np.linspace(0, 400, 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))))
|