sympy.stats.Triangular() in Python

sympy.stats.Triangular() in Python

In the sympy.stats module, the Triangular class represents a continuous random variable following a triangular distribution.

The triangular distribution is defined with three parameters:

  • lower (or a): the minimum value
  • upper (or c): the maximum value
  • mode (or b): the value where the peak of the probability distribution occurs (it must be between lower and upper)

Here's how to use Triangular in sympy:

  • First, make sure you have sympy installed:
pip install sympy 
  • Then, you can create a triangular random variable and compute various properties:
from sympy.stats import Triangular, density, cdf, E, variance from sympy import Symbol, plot # Define the parameters a = 0 # lower limit c = 10 # upper limit b = 5 # mode # Create the triangular random variable X = Triangular('X', a, b, c) # Plot the probability density function (PDF) plot(density(X)(Symbol('x')), (Symbol('x'), a, c), title='PDF of Triangular Distribution', ylabel='Density') # Compute the cumulative distribution function (CDF) at a specific value value = 7 prob = cdf(X)(value) print(f"CDF at x = {value}: {prob}") # Compute the mean and variance mean_val = E(X) var_val = variance(X) print(f"Mean: {mean_val}") print(f"Variance: {var_val}") 

This code sets up a triangular random variable X with a range from 0 to 10 and a mode (peak) at 5. It then plots the probability density function, computes the cumulative distribution function at x = 7, and prints out the mean and variance of the distribution.


More Tags

netmask integer whatsapi data-visualization celerybeat touch-event getelementsbyclassname fibonacci allure nhibernate

More Programming Guides

Other Guides

More Programming Examples