Python Forum
something that returns a linear function compatible with lognorm.pdf and lognorm.cdf? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: something that returns a linear function compatible with lognorm.pdf and lognorm.cdf? (/thread-7062.html)



something that returns a linear function compatible with lognorm.pdf and lognorm.cdf? - Afterdarkreader - Dec-19-2017


I'm trying to figure out how to make a lognorm function compatible with a linear function. Any ideas? Blush


RE: something that returns a linear function compatible with lognorm.pdf and lognorm.cdf? - micseydel - Dec-19-2017

This doesn't seem like a generic Python question. Is it a math question? Or a question about (a) specific module(s)?


RE: something that returns a linear function compatible with lognorm.pdf and lognorm.cdf? - Afterdarkreader - Dec-19-2017

It's a question about numpy and scipy. I'm using the lognorm function in scipy.stats, and I need to add a linear growth function. I was wondering if anyone knew a function I could use that would make this work. Here's my code:
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp


def runLognormal(start, current, mean, cumImpact, peakFactor):
    scale = exp(mean)
    return lognorm.pdf(current, peakFactor, start, scale)*cumImpact
    
def runLinear(start, stop, changePerQ, x):
    numIterations = (stop-start)*100
    y = np.array([])
    for x in range(start, stop):
        np.append(y, changePerQ)
    return y

     
x = np.linspace(0, 212, num=21200)
tax1 = runLognormal(92, x, 0.036, 0.071, 0.55)

label1 = runLinear(80, 160, 0.0006875, x)


############################################### GRAPH LABEL 1
fig, lbl1 = plt.subplots(1, 1)
label1X = np.linspace(80, 160, num = 8000)
lbl1.set_title('Text Based Labels')
lbl1.set_xlabel('Quarter')
lbl1.set_ylabel('Cumulative Effect')
lbl1.plot(label1X, runLinear(80, 160, 0.0006875, label1X))
##############################################################

veganGrowth = tax1 + label1

fig, ax = plt.subplots(1, 1)
ax.set_title('Instantaneous Rate of Change in Population')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Population Omnivores$')
ax.plot(x, veganGrowth)
right now neither the graph of the linear function (it's supposed to return a straight line or the ax plot is working. I'm getting a 'x and y must have the same first dimension' error for both.

Error for Label graph:
Error:
File "<ipython-input-92-225e42a16155>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py", line 126, in <module> lbl1.plot(label1X, runLinear(80, 160, 0.0006875, label1X)) File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot for line in self._get_lines(*args, **kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 282, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension
Error for the main graph with label graph commented out (note, in this version I took out about 50 iterations of the lognorm function, I know the problem is in the linear function because it graphs if I take that out)
Error:
Traceback (most recent call last): File "<ipython-input-101-225e42a16155>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py", line 133, in <module> veganGrowth = tax1 + tax2 + tax3 + tax4 + tax5 + tariff + ad1 + ad2 + ad3 + ad4 + ad5 + ad6 + ad7 + news1 + veg + veg2 + label1 + label2 ValueError: operands could not be broadcast together with shapes (21200,) (0,)