问题
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
回答1:
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 parameters a
and b
specified in the rv_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.
import scipy.stats
class rvc(scipy.stats.rv_continuous):
def __init__(self, freqs):
super().__init__(a=0,b=359.9999)
self.WindDir = [0.,45.,90.,135.,180.,225.,270.,315.,360.]
self.Freqs = freqs
def _cdf(self, x):
return [self.do_cdf(i) for i in x]
def do_cdf(self, x):
if x < 0: return 0.0
if x >= 360: return 1.0
v = 0.0
for i in range(9):
if x >= self.WindDir[i]:
v += self.Freqs[i]
else:
v += (self.Freqs[i]-self.Freqs[i-1])*(x-self.WindDir[i-1])/45.
break
return v
rv = rvc([0.01,0.01, 0.01, 0.01, 0.01, 0.01, 0.92, 0.01, 0.01])
print(rv.rvs(size=100))
Running this generates output like
[ 305.76400244 271.49390379 285.5514374 280.4422151 302.52158889
273.85068415 278.8377575 287.05260894 270.66219523 301.97131911
277.77725392 293.29633122 273.60832876 278.90703722 271.12590324
288.38285244 299.89362412 276.68015144 278.46429959 304.33532312
313.42248626 272.10226608 311.00385667 284.48822492 298.134523
300.08266465 272.17850056 270.07995773 359.9999 289.13032076
206.58066169 308.95821915 288.998036 280.93451276 294.33703562
278.02828894 307.96335447 292.50172701 313.67335738 291.99562061
275.98551449 307.72620259 283.11307243 309.6052904 283.65619152
305.80365684 303.43088801 314.74811251 302.8390404 288.16580807
299.84487396 302.34636138 291.09465231 310.74393544 279.23446355
309.83353391 292.62761642 273.22324646 291.16193395 298.51520679
299.87754111 280.29779055 276.76741796 285.5186257 301.7074023
274.9771402 280.6619726 276.53202603 289.50757382 313.61213159
98.86469637 298.41091812 83.62474126 290.42538277 306.70218844
270.81939255 290.25089647 305.3277742 278.03965968 280.5979916
307.9492377 284.30184233 307.14788891 283.33779011 270.86398644
297.70610336 277.48672772 300.35147777 271.07308885 304.41450287
312.5640489 273.54296504 311.62527023 298.95246144 275.199183
302.6063864 359.9999 311.25861396 306.83491033 313.52858514]
来源:https://stackoverflow.com/questions/17676710/create-a-continuous-distribution-in-python