Files
dash-api/rel.ipynb

123 lines
4.5 KiB
Plaintext
Raw Normal View History

2025-09-10 22:00:21 +02:00
```python
# Cell 1: Install the reliability library (if not already installed)
# You only need to run this cell once.
# !pip install reliability
```
```python
# Cell 2: Import necessary libraries
import pandas as pd
from reliability.Fitters import Fit_Weibull_2P
from reliability.Distributions import Weibull_Distribution
import numpy as np
from math import gamma
import matplotlib.pyplot as plt # Often useful for custom plotting
```
```python
# Cell 3: Generate Sample Data (Simulated for demonstration)
# In a real scenario, you would load your own data.
# 'data' represents the observed failure times
# 'censored' is a boolean array: True if censored, False if failed
# Example 1: Some failures, some right-censored data
data_1 = [90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240]
censored_1 = [False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True] # Last 8 are censored
# Example 2: More failures, fewer censors (uncomment to use this data)
# data_2 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]
# censored_2 = [False, False, False, False, False, False, False, False, False, False, True, True, True, True, True]
# Choose which dataset to use
data = data_1
censored = censored_1
print("Sample Data:")
print(f"Observed Times: {data}")
print(f"Censored Status (True if censored): {censored}")
```
```python
# Cell 4: Fit Weibull Distribution to Data
# The Fit_Weibull_2P function handles censored data automatically
print("--- Fitting Weibull Distribution ---")
weibull_fit = Fit_Weibull_2P(failures=np.array(data), right_censored=np.array(censored), show_probability_plot=True, print_results=True)
# The fit_weibull_2P object now contains the fitted parameters (alpha and beta)
alpha = weibull_fit.alpha
beta = weibull_fit.beta
print(f"\nFitted Weibull Parameters:")
print(f"Shape parameter (beta): {beta:.4f}")
print(f"Scale parameter (alpha): {alpha:.4f}")
```
```python
# Cell 5: Calculate Mean Time To Failure (MTTF) for the fitted Weibull distribution
# MTTF for a Weibull distribution is given by: alpha * gamma(1 + 1/beta)
# where gamma is the Gamma function.
mttf = alpha * gamma(1 + 1/beta)
print(f"Calculated Mean Time To Failure (MTTF) for the fitted Weibull distribution: {mttf:.2f}")
```
```python
# Cell 6: Plotting the PDF, CDF, and Reliability Function
# Create a Weibull distribution object with the fitted parameters
weibull_dist = Weibull_Distribution(alpha=alpha, beta=beta)
plt.figure(figsize=(12, 8))
# Plot PDF
plt.subplot(3, 1, 1) # 3 rows, 1 column, 1st plot
weibull_dist.PDF(dashes=True, label='Weibull PDF', plot_type='plt', show_plot=False)
plt.title('Weibull Probability Density Function')
plt.xlabel('Time')
plt.ylabel('Probability Density')
plt.legend()
plt.grid(True)
# Plot CDF
plt.subplot(3, 1, 2) # 3 rows, 1 column, 2nd plot
weibull_dist.CDF(dashes=True, label='Weibull CDF', plot_type='plt', show_plot=False)
plt.title('Weibull Cumulative Distribution Function')
plt.xlabel('Time')
plt.ylabel('Cumulative Probability')
plt.legend()
plt.grid(True)
# Plot Reliability Function (Survival Function)
plt.subplot(3, 1, 3) # 3 rows, 1 column, 3rd plot
weibull_dist.SF(dashes=True, label='Weibull Reliability Function (SF)', plot_type='plt', show_plot=False)
plt.title('Weibull Reliability (Survival) Function')
plt.xlabel('Time')
plt.ylabel('Reliability')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show() # Display all plots
```
```python
# Cell 7: Extracting other useful information (e.g., B10 life, reliability at time)
# B10 life is the time at which 10% of the population has failed (90% reliability)
b10_life = weibull_dist.b_n(n=10)
print(f"B10 Life (time at which 10% have failed): {b10_life:.2f}")
# Example: Reliability at a specific time (e.g., 150 units of time)
time_point = 150
reliability_at_time = weibull_dist.SF(t=time_point)
print(f"Reliability at {time_point} units of time: {reliability_at_time:.4f}")
# Example: Time at a specific reliability (e.g., 70% reliability)
target_reliability = 0.70
time_at_reliability = weibull_dist.quantile(q=1 - target_reliability) # Quantile takes probability of failure (1-Reliability)
print(f"Time at {target_reliability*100}% reliability: {time_at_reliability:.2f}")
```
When you run this in a Jupyter Notebook, the probability plot from `Fit_Weibull_2P` will appear directly below the cell where it's called, and the PDF, CDF, and Reliability plots will appear below Cell 6. The text outputs will appear in their respective cells.