Create a continuous distribution in python

前端 未结 1 763
忘掉有多难
忘掉有多难 2021-01-19 14:31

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

1条回答
  •  孤城傲影
    2021-01-19 15:30

    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]
    

    0 讨论(0)
提交回复
热议问题