Exemplo n.º 1
0
def CredIntPlt(sf, kl, kh, ll, lh, house, mk, ml, Title):
    """Given 90 credible values of k and lambda, the mean values of k and lambda, 
	the survival function, the house color scheme to use, and the plot title, this 
	function plots the 90 percent credible interval, the best line, and the data 
	we have"""

    listcol = colordict[house]
    Dark = listcol[0]
    Mid = listcol[1]
    Light = listcol[2]
    arr = np.linspace(0, 7, num=100)
    weibSurv2 = exponweib.cdf(arr, kl, lh)  #Lower bound
    weibSurv4 = exponweib.cdf(arr, kh, ll)  #Upper bound
    weibSurv1 = exponweib.cdf(arr, mk, ml)  #Best line
    p4, = plt.plot(arr, 1 - weibSurv2, color=Dark, linewidth=3)
    p1, = plt.plot(arr, 1 - weibSurv2, color=Light, linewidth=4)
    p2, = plt.plot(arr, 1 - weibSurv1, color=Mid, linewidth=3, linestyle='--')
    p3, = plt.plot(arr, 1 - weibSurv4, color=Light, linewidth=4)
    plt.fill_between(arr,
                     1 - weibSurv2,
                     1 - weibSurv4,
                     facecolor=Light,
                     alpha=.3)
    thinkplot.plot(sf, color=Dark)
    plt.xlabel('Age in Books')
    plt.ylabel('Probability of Survival')
    plt.ylim([0, 1])

    plt.legend([p1, p2, p4],
               ['90 Percent Credible Interval', 'Best Estimate', 'Data'])
    plt.title(Title)
def CredIntPlt(sf,kl,kh,ll,lh,house,mk,ml,Title):
	"""Given 90 credible values of k and lambda, the mean values of k and lambda, 
	the survival function, the house color scheme to use, and the plot title, this 
	function plots the 90 percent credible interval, the best line, and the data 
	we have"""

	listcol=colordict[house]
	Dark=listcol[0]
	Mid=listcol[1]
	Light=listcol[2]
	arr=np.linspace(0,7,num=100)
	weibSurv2 = exponweib.cdf(arr, kl, lh) #Lower bound
	weibSurv4 = exponweib.cdf(arr, kh, ll) #Upper bound
	weibSurv1 = exponweib.cdf(arr, mk, ml) #Best line
	p4,=plt.plot(arr, 1-weibSurv2,color=Dark,linewidth=3)
	p1,=plt.plot(arr, 1-weibSurv2,color=Light,linewidth=4)
	p2,=plt.plot(arr, 1-weibSurv1,color=Mid,linewidth=3,linestyle='--')
	p3,=plt.plot(arr, 1-weibSurv4,color=Light,linewidth=4)
	plt.fill_between(arr,1-weibSurv2,1-weibSurv4, facecolor=Light, alpha=.3)
	thinkplot.plot(sf,color=Dark)
	plt.xlabel('Age in Books')
	plt.ylabel('Probability of Survival')
	plt.ylim([0,1]) 
	
	plt.legend([p1,p2,p4],['90 Percent Credible Interval','Best Estimate','Data'])
	plt.title(Title)
Exemplo n.º 3
0
def CredIntPlt(sf, kl, kh, ll, lh, house, mk, ml, Title):
    listcol = colordict[house]
    Dark = listcol[0]
    Mid = listcol[1]
    Light = listcol[2]
    arr = np.linspace(0, 7, num=100)
    weibSurv2 = exponweib.cdf(arr, kl, lh)
    weibSurv4 = exponweib.cdf(arr, kh, ll)
    weibSurv1 = exponweib.cdf(arr, mk, ml)
    # p4,=plt.plot(arr, 1-weibSurv2,color=Dark,linewidth=3)
    p1, = plt.plot(arr, 1 - weibSurv2, color=Light, linewidth=4)
    # p2,=plt.plot(arr, 1-weibSurv1,color=Mid,linewidth=3,linestyle='--')
    p3, = plt.plot(arr, 1 - weibSurv4, color=Light, linewidth=4)
    plt.fill_between(arr,
                     1 - weibSurv2,
                     1 - weibSurv4,
                     facecolor=Light,
                     alpha=.3)
    # thinkplot.plot(sf,color=Dark)
    plt.xlabel('Age in Books')
    plt.ylabel('Probability of Survival')
    plt.ylim([.0, 1])
    plt.text(6.3, 0.95, 'Theon', color='Khaki')
    plt.text(5.3, 0.4, 'Lord Walder Frey', color='DarkSeaGreen')

    # plt.legend([p1,p2,p4],['90 Percent Credible Interval','Best Estimate','Data'])
    plt.title(Title)
def weibPredict():
    hazard = [claimMth[i]/corrRisk[i] for i in range(len(corrRisk))]
    
    cumulHazard = [0] * len(hazard)
    for hzd in hazard:
        i = hazard.index(hzd)
        cumulHazard[i] = sum(hazard[0:i+1])
    
    reliability = [mt.exp(-x) for x in cumulHazard]
    
    claimCDF = [1-x for x in reliability]
    
    weibY = [mt.log(-mt.log(1-x)) for x in claimCDF[1:]]
    weibX = [mt.log(x) for x in range(len(claimCDF))[1:]]
    
    weibPara = weibullFit(weibX, weibY)
    weibCDF = [ew.cdf(i, a = 1, c = weibPara[0], scale = weibPara[1]) \
                      for i in range(predictPeriodUsr)]
    print weibCDF
    
    claimPercent = [0] * len(weibCDF)
    for cdf in weibCDF:
        i = weibCDF.index(cdf)
        if i == 0:
            claimPercent[i] = cdf
        if i > 0:
            claimPercent[i] = cdf - weibCDF[i - 1]

##    claimPercent = weibPDF
    print claimPercent
    return claimPercent
	def Likelihood(self, data, hypo):
		age, alive = data
		k, lam = hypo
		if alive:
			prob = 1-exponweib.cdf(age, k, lam)
		else:
			prob = exponweib.pdf(age, k, lam)
		return prob
Exemplo n.º 6
0
 def Likelihood(self, data, hypo):
     age, alive = data
     k, lam = hypo
     if alive:
         prob = 1 - exponweib.cdf(age, k, lam)
     else:
         prob = exponweib.pdf(age, k, lam)
     return prob
	def Likelihood(self, data, hypo):
		"""Determines how well a given k and lam predict the life/death of a character """
		age, alive = data
		k, lam = hypo
		if alive:
			prob = 1-exponweib.cdf(age, k, lam)
		else:
			prob = exponweib.pdf(age, k, lam)
		return prob
Exemplo n.º 8
0
 def Likelihood(self, data, hypo):
     """Determines how well a given k and lam predict the life/death of a character """
     age, alive = data
     k, lam = hypo
     if alive:
         prob = 1 - exponweib.cdf(age, k, lam)
     else:
         prob = exponweib.pdf(age, k, lam)
     return prob
    def predict(self, next_n):
        if not self.params:
            pred = [0] * next_n
        elif self.fit_model == "Sampling":
            pred = self.generate_samples(self.params, next_n, self.time_since_last_spike, self.spike_width_avg, self.spike_max)
        elif self.time_since_last_spike== 0:
            return [self.last]*next_n
        elif self.fit_model == "Weibull":
            pred = exponweib.cdf([x for x in range(next_n)], a = self.params[0], c= self.params[1], loc=-self.time_since_last_spike, scale = self.params[3]) * self.spike_avg
        else: # self.fit_model == "Expon":
            pred = expon.cdf([x for x in range(next_n)], -self.time_since_last_spike, self.params[1]) * self.spike_avg

        return self.round_non_negative_int_func(pred)
def CredIntPlt(sf,kl,kh,ll,lh,house,mk,ml,Title):
	listcol=colordict[house]
	Dark=listcol[0]
	Mid=listcol[1]
	Light=listcol[2]
	arr=np.linspace(0,7,num=100)
	weibSurv2 = exponweib.cdf(arr, kl, lh)
	weibSurv4 = exponweib.cdf(arr, kh, ll)
	weibSurv1 = exponweib.cdf(arr, mk, ml)
	# p4,=plt.plot(arr, 1-weibSurv2,color=Dark,linewidth=3)
	p1,=plt.plot(arr, 1-weibSurv2,color=Light,linewidth=4)
	# p2,=plt.plot(arr, 1-weibSurv1,color=Mid,linewidth=3,linestyle='--')
	p3,=plt.plot(arr, 1-weibSurv4,color=Light,linewidth=4)
	plt.fill_between(arr,1-weibSurv2,1-weibSurv4, facecolor=Light, alpha=.3)
	# thinkplot.plot(sf,color=Dark)
	plt.xlabel('Age in Books')
	plt.ylabel('Probability of Survival')
	plt.ylim([.0,1]) 
	plt.text(6.3,0.95,'Theon',color='Khaki')
	plt.text(5.3,0.4,'Lord Walder Frey',color='DarkSeaGreen')
	
	# plt.legend([p1,p2,p4],['90 Percent Credible Interval','Best Estimate','Data'])
	plt.title(Title)
Exemplo n.º 11
0
def fit_distribution(data, fit_type, x_min, x_max, n_points=1000):
    # Initialization of the variables
    param, x, cdf, pdf = [-1, -1, -1, -1]

    if fit_type == 'exponweib':
        x = np.linspace(x_min, x_max, n_points)

        # Fit data to the theoretical distribution
        param = exponweib.fit(data, 1, 1, scale=02, loc=0)
        # param = exponweib.fit(data, fa=1, floc=0)
        # param = exponweib.fit(data)

        cdf = exponweib.cdf(x, param[0], param[1], param[2], param[3])
        pdf = exponweib.pdf(x, param[0], param[1], param[2], param[3])

    elif fit_type == 'lognorm':
        x = np.linspace(x_min, x_max, n_points)

        # Fit data to the theoretical distribution
        param = lognorm.fit(data, loc=0)

        cdf = lognorm.cdf(x, param[0], param[1], param[2])
        pdf = lognorm.pdf(x, param[0], param[1], param[2])

    elif fit_type == 'norm':
        x = np.linspace(x_min, x_max, n_points)

        # Fit data to the theoretical distribution
        param = norm.fit(data, loc=0)

        cdf = norm.cdf(x, param[0], param[1])
        pdf = norm.pdf(x, param[0], param[1])

    elif fit_type == 'weibull_min':
        x = np.linspace(x_min, x_max, n_points)

        # Fit data to the theoretical distribution
        param = weibull_min.fit(data, floc=0)

        cdf = weibull_min.cdf(x, param[0], param[1], param[2])
        pdf = weibull_min.pdf(x, param[0], param[1], param[2])

    return param, x, cdf, pdf
Exemplo n.º 12
0
from scipy.stats import exponweib
print(exponweib.cdf(2,1,3,0,4))
Exemplo n.º 13
0
        exponweib.pdf(x, a, c),
        'r-',
        lw=5,
        alpha=0.6,
        label='exponweib pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = exponweib(a, c)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = exponweib.ppf([0.001, 0.5, 0.999], a, c)
np.allclose([0.001, 0.5, 0.999], exponweib.cdf(vals, a, c))
# True

# Generate random numbers:

r = exponweib.rvs(a, c, size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
Exemplo n.º 14
0
# -*- coding: utf-8 -*-
import os
import sys
import numpy
import random
import math
from scipy.stats import exponweib

file1 = open("weibullcdf.txt", "w")
x = 1e-06

num = 1
while x < 0.1:
    num += 1
    if num % 100 == 0:
        file1.write(str(x) + '\t')
        file1.write(str(exponweib.cdf(x, 100, 0.4, 0, 0.0001)) + '\n')
    x += 1e-06
file1.close()
Exemplo n.º 15
0
from scipy.stats import exponweib
print(exponweib.cdf(2, 1, 3, 0, 4))