Matlab 中有像 wblplot() 这样生成威布尔概率图的 Python 函数吗?

问题描述 投票:0回答:1

我想重现与 Matlab 的 wblplot() 文档中所示的相同的图表: Matlab wblplot() 文档

我想知道是否有一种方法可以在 Python 上生成它,无论是使用现有的库/函数还是用户定义的。

我不知道这个图需要什么参数。我可以在 JMP 或 Origin 等统计软件上手动调整形状/比例,但我不知道如何以数学或编程方式获取这些值。 最初,我尝试将 CDF 拟合到数据,但这并没有给我带来我想要的结果。 我可以编辑 y 轴的刻度来表示概率分位数,但在较低值(例如 0.01、0.02、0.05)时它会变得拥挤,并且我不确定如何增加间距。

使用 scipy.stats.weibull_min.cdf() 生成的图

数据

enter code here
python weibull transient-failure
1个回答
1
投票

这里有两个问题:威布尔参数的拟合以及在适当比例的图表上绘制(我认为以前称为“概率纸”)。

我在上一篇文章中处理了威布尔参数:如何找到威布尔分布的 x 截距

基本上,您可以将

log[-log(1-F)]
log(X)
进行比较,其中
X
是随机变量,
F
是其累积概率。威布尔分布应给出一条斜率为
k
和 y 轴截距
-k.log(lambda)
的直线。

对于缩放图,您可以使用 matplotlib 中的 Axis Scales 功能:请参阅 https://matplotlib.org/stable/users/explain/axes/axes_scales.html

在这里,您必须定义缩放函数及其反函数。在下面的代码中,我对 y 轴(需要时)和 x 轴(不需要时)执行了此操作。对于对数刻度,我还必须采取一些规避措施来防止无效值达到对数;它并不完美。

您没有提供任何数据,所以我根据我之前的帖子做了一些数据。

import numpy as np
import matplotlib.pyplot as plt

# Data
x = np.array([0.1, 0.2, 0.4, 0.5, 0.8, 1.0, 1.1, 1.2]) 
n = len( x )
i = np.arange(1, n + 1)
F = (i - 0.5) / (n + 0.25)

# Fit a linear regression to scaled variables to estimate the parameters of the Weibull distribution
scaled_x = np.log( x )
scaled_F = np.log( -np.log( 1 - F ) )
slope, intercept = np.polyfit( scaled_x, scaled_F, 1)
k = slope
lam = np.exp( -intercept / k )             # -k ln( lambda ) = intercept
print( "Shape parameter (k):     ", k   )
print( "Scale parameter (lambda):", lam )


# Scaling functions for the two axes
SMALL = 1.0e-20    # attempt to protect against invalid arguments to log

def forwardY( p ):
    return np.log( SMALL - np.log( 1 - p + SMALL ) )

def inverseY( q ):
    return np.log( 1 - np.exp( - np.exp( q ) ) )

def forwardX(x):
    return np.log( SMALL + x )

def inverseX(y):
    return np.exp(y)


# Weibull CDF
def CDF( x, k, L ):
    return 1 - np.exp( - ( x / L) ** k )

# Plot data and exact distribution
x1, x2 = min( x ), max( x )
y1, y2 = CDF( x1, k, lam ), CDF( x2, k, lam )

probs = [ 0.01, 0.02, 0.05, 0.1, 0.25, 0.50, 0.75, 0.90, 0.96, 0.99 ]

fig, ax = plt.subplots( layout='constrained' )
ax.plot( x, F, 'bx', clip_on=False )
ax.plot( [x1,x2], [y1,y2], 'r-' )
ax.set_xlim( left=0.1 )
ax.set_xscale( 'function', functions=(forwardX,inverseX) )
ax.set_yscale( 'function', functions=(forwardY,inverseY) )

ax.set_xlabel( 'Time to Breakdown (TBD)' )
ax.set_ylabel( 'Cumulative distribution function' )
ax.set_yticks( probs )
ax.grid()
plt.show()

输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.