I am having trouble creating a continuous distribution in python and its really beginning to annoy me. I have read and re-read this python guide (scipy guide) and it hasn't helped my problem.
My code reads:
import sys
import scipy.stats
import numpy
def CDF_Random(N,NE,E,SE,S,SW,W,NW,Iterations):
WindDir = [0,45,90,135,180,225,270,315]
Freq = N,NE,E,SE,S,SW,W,NW
mydist = scipy.stats.rv_continuous(#My problem is what to write here)
cdf_rand=mydist.rvs(size=Iterations)
return (cdf_rand)
if __name__ == '__main__':
N = float(sys.argv[1])
NE = float(sys.argv[2])
E = float(sys.argv[3])
SE = float(sys.argv[4])
S = float(sys.argv[5])
SW = float(sys.argv[6])
W = float(sys.argv[7])
NW = float(sys.argv[8])
Iterations = float(sys.argv[9])
numpy.set_printoptions(threshold=Iterations)
sys.stdout.write(str(CDF_Random(N,NE,E,SE,S,SW,W,NW,Iterations)))
As you can see if you read the code, my problem is knowing what to put in the brackets to create the continuous distribution. scipy.stats.rv_continuous(#what to put here)
.
I have tried alot of different things, mainly the ones suggested in this document(scipy guide), like setting my upper and lower range values a=,b=
setting it to a pdf
or a ppf
. I have tried [arrays]
using the ones that are entered in the command line or just ones I wrote into the code itself.
From the command line I run this command
python C:\Users\...\CDF.py 0.01 0.01 0.01 0.01 0.01 0.93 0.01 0.01 10
and every time I get;RuntimeError:maximum recursion depth exceeded
I have tried resetting the recursion depth to different values but this didn't work or crashed python.
sys.setrecursionlimit(10000)
So basically what should be entered in the brackets after scipy.stats.rv_continuous()
to create a continuous distribution of the [array]
called WindDir
for a given distribution freq
?
I have honestly had a good look through Google and the stackoverflow website, searching using keywords, keywords with tags and tags alone and couldn't find a solution.
Edit 1-Desired outcome
I would like the output to be a real number between 0,360
or 0,2pi
Alright, so in order to use
rv_continuous
you need to provide a probability density function of some sort. In the example below, I implement a cumulative density function for the given wind direction interval of [0,360). I do this by interpolating the probability density function between the nearest two wind directions specified in the input. Note the parametersa
andb
specified in therv_continuous
base class constructor...these specify the minimum and maximum values of the interval in consideration. Try the code out, and if you have any questions, please ask and I'll try to help clarify.Edit I've modified the code for python 3, as well as updated the cdf to more accurately interpolate between the frequencies given at the cardinal directions.
Running this generates output like