Categories &

Functions List

Class Definition: PiecewiseLinearDistribution

statistics: PiecewiseLinearDistribution

Piecewise linear probability distribution object.

A PiecewiseLinearDistribution object consists of parameters, a model description, and sample data for a piecewise linear probability distribution.

The piecewise linear distribution is a continuous probability distribution that is defined by a set of points where the cumulative distribution function (CDF) changes slope. It is defined by a vector of × values and a corresponding vector of CDF values Fx.

There are several ways to create a PiecewiseLinearDistribution object.

  • Create a distribution with specified parameter values using the makedist function.
  • Use the constructor PiecewiseLinearDistribution (x, Fx) to create a piecewise linear distribution with specified parameter values x and Fx.

It is highly recommended to use fitdist and makedist functions to create probability distribution objects, instead of the class constructor or the aforementioned static method.

Further information about the piecewise linear distribution can be found at https://en.wikipedia.org/wiki/Piecewise_linear_function

See also: makedist, plcdf, plinv, plpdf, plrnd, plstat

Source Code: PiecewiseLinearDistribution

Properties

A numeric vector of × values at which the CDF changes slope. You can access the x property using dot name assignment.

Example: 1

 

 ## Create a Piecewise Linear distribution object by calling its constructor
 pd = PiecewiseLinearDistribution ([0; 1; 2; 3], [0; 0.3; 0.7; 1])

 ## Query parameter 'x'
 pd.x

 ## This demonstrates direct construction with specific x values, useful for
 ## modeling custom empirical distributions or interpolating between known points.

pd =
  PiecewiseLinearDistribution

F(0) = 0
F(1) = 0.3
F(2) = 0.7
F(3) = 1

ans =

   0
   1
   2
   3

                    

A numeric vector of CDF values that correspond to each value in ×. You can access the Fx property using dot name assignment.

Example: 1

 

 ## Create a Piecewise Linear distribution object by calling its constructor
 pd = PiecewiseLinearDistribution ([0; 1; 2; 3], [0; 0.3; 0.7; 1])

 ## Query parameter 'Fx'
 pd.Fx

 ## This shows how to set CDF values directly via the constructor, ideal for
 ## defining piecewise linear approximations of arbitrary distributions.

pd =
  PiecewiseLinearDistribution

F(0) = 0
F(1) = 0.3
F(2) = 0.7
F(3) = 1

ans =

        0
   0.3000
   0.7000
   1.0000

                    

A character vector specifying the name of the probability distribution object. This property is read-only.

A scalar integer value specifying the number of parameters characterizing the probability distribution. This property is read-only.

A 2×1 cell array of character vectors with each element containing the name of a distribution parameter. This property is read-only.

A 2×1 cell array of character vectors with each element containing a short description of a distribution parameter. This property is read-only.

A 2×1 numeric vector containing the values of the distribution parameters. This property is read-only. You can change the distribution parameters by assigning new values to the x and Fx properties.

A 1×2 numeric vector specifying the truncation interval for the probability distribution. First element contains the lower boundary, second element contains the upper boundary. This property is read-only. You can only truncate a probability distribution with the truncate method.

A logical scalar value specifying whether a probability distribution is truncated or not. This property is read-only.

Methods

PiecewiseLinearDistribution: p = cdf (pd, x)
PiecewiseLinearDistribution: p = cdf (pd, x, "upper")

p = cdf (pd, x) computes the CDF of the probability distribution object, pd, evaluated at the values in x.

p = cdf (…, "upper") returns the complement of the CDF of the probability distribution object, pd, evaluated at the values in x.

Example: 1

 

 ## Plot various CDFs from the Piecewise Linear distribution
 load patients
 [f1, x1] = ecdf (Weight);
 [f2, x2] = ecdf (Height);
 pd1 = PiecewiseLinearDistribution (x1(1:10:end), f1(1:10:end));
 pd2 = PiecewiseLinearDistribution (x2(1:10:end), f2(1:10:end));
 vals = 50:0.1:250;
 p1 = cdf (pd1, vals);
 p2 = cdf (pd2, vals);
 plot (vals, p1, "-b", vals, p2, "-r")
 grid on
 legend ({"Weight", "Height"}, "location", "southeast")
 title ("Piecewise Linear CDF")
 xlabel ("values in x")
 ylabel ("Cumulative probability")

 ## Use this to compute and visualize the cumulative distribution function
 ## for different Piecewise Linear distributions, showing how probability
 ## accumulates across the defined segments.

                    
plotted figure

PiecewiseLinearDistribution: x = icdf (pd, p)

x = icdf (pd, p) computes the quantile (the inverse of the CDF) of the probability distribution object, pd, evaluated at the values in p.

Example: 1

 

 ## Plot various iCDFs from the Piecewise Linear distribution
 load patients
 [f1, x1] = ecdf (Weight);
 [f2, x2] = ecdf (Height);
 pd1 = PiecewiseLinearDistribution (x1(1:10:end), f1(1:10:end));
 pd2 = PiecewiseLinearDistribution (x2(1:10:end), f2(1:10:end));
 p = 0.001:0.001:0.999;
 x_vals1 = icdf (pd1, p);
 x_vals2 = icdf (pd2, p);
 plot (p, x_vals1, "-b", p, x_vals2, "-r")
 grid on
 legend ({"Weight", "Height"}, "location", "northwest")
 title ("Piecewise Linear iCDF")
 xlabel ("Probability")
 ylabel ("values in x")

 ## This demonstrates the inverse CDF (quantiles) for Piecewise Linear
 ## distributions, useful for finding values corresponding to given
 ## probabilities in empirical data.

                    
plotted figure

PiecewiseLinearDistribution: r = iqr (pd)

r = iqr (pd) computes the interquartile range of the probability distribution object, pd.

Example: 1

 

 ## Compute the interquartile range for a Piecewise Linear distribution
 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 iqr_value = iqr (pd)

 ## Use this to calculate the interquartile range, which measures the spread
 ## of the middle 50% of the distribution, helpful for summarizing variability
 ## in empirical datasets.

iqr_value = 50.083
                    
PiecewiseLinearDistribution: m = mean (pd)

m = mean (pd) computes the mean of the probability distribution object, pd.

Example: 1

 

 ## Compute the mean for different Piecewise Linear distributions
 load patients
 [f1, x1] = ecdf (Weight);
 [f2, x2] = ecdf (Height);
 pd1 = PiecewiseLinearDistribution (x1(1:5:end), f1(1:5:end));
 pd2 = PiecewiseLinearDistribution (x2(1:5:end), f2(1:5:end));
 mean1 = mean (pd1)
 mean2 = mean (pd2)

 ## This shows how to compute the expected value for Piecewise Linear
 ## distributions based on empirical data, representing the average value.

mean1 = 153.61
mean2 = 56.500
                    
PiecewiseLinearDistribution: m = median (pd)

m = median (pd) computes the median of the probability distribution object, pd.

Example: 1

 

 ## Compute the median for different Piecewise Linear distributions
 load patients
 [f1, x1] = ecdf (Weight);
 [f2, x2] = ecdf (Height);
 pd1 = PiecewiseLinearDistribution (x1(1:5:end), f1(1:5:end));
 pd2 = PiecewiseLinearDistribution (x2(1:5:end), f2(1:5:end));
 median1 = median (pd1)
 median2 = median (pd2)

 ## Use this to find the median value, which splits the distribution
 ## into two equal probability halves, robust for skewed empirical data.

median1 = 142
median2 = 66.727
                    
PiecewiseLinearDistribution: y = pdf (pd, x)

y = pdf (pd, x) computes the PDF of the probability distribution object, pd, evaluated at the values in x.

Example: 1

 

 ## Plot various PDFs from the Piecewise Linear distribution
 load patients
 [f1, x1] = ecdf (Weight);
 [f2, x2] = ecdf (Height);
 pd1 = PiecewiseLinearDistribution (x1(1:10:end), f1(1:10:end));
 pd2 = PiecewiseLinearDistribution (x2(1:10:end), f2(1:10:end));
 vals = 50:0.1:250;
 y1 = pdf (pd1, vals);
 y2 = pdf (pd2, vals);
 plot (vals, y1, "-b", vals, y2, "-r")
 grid on
 legend ({"Weight", "Height"}, "location", "northeast")
 title ("Piecewise Linear PDF")
 xlabel ("values in x")
 ylabel ("Probability density")

 ## This visualizes the probability density function for Piecewise Linear
 ## distributions, showing the density across segments.

                    
plotted figure

PiecewiseLinearDistribution: plot (pd)
PiecewiseLinearDistribution: plot (pd, Name, Value)
PiecewiseLinearDistribution: h = plot (…)

plot (pd) plots a probability density function (PDF) of the probability distribution object pd. If pd contains data, which have been fitted by fitdist, the PDF is superimposed over a histogram of the data.

plot (pd, Name, Value) specifies additional options with the Name-Value pair arguments listed below.

NameValue
"PlotType"A character vector specifying the plot type. "pdf" plots the probability density function (PDF). When pd is fit to data, the PDF is superimposed on a histogram of the data. "cdf" plots the cumulative density function (CDF). When pd is fit to data, the CDF is superimposed over an empirical CDF. "probability" plots a probability plot using a CDF of the data and a CDF of the fitted probability distribution. This option is available only when pd is fitted to data.
"Discrete"A logical scalar to specify whether to plot the PDF or CDF of a discrete distribution object as a line plot or a stem plot, by specifying false or true, respectively. By default, it is true for discrete distributions and false for continuous distributions. When pd is a continuous distribution object, option is ignored.
"Parent"An axes graphics object for plot. If not specified, the plot function plots into the current axes or creates a new axes object if one does not exist.

h = plot (…) returns a graphics handle to the plotted objects.

Example: 1

 

 ## Create a Piecewise Linear distribution and plot its PDF.
 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 plot (pd)
 title ("Piecewise Linear distribution from Weight data")

                    
plotted figure

Example: 2

 

 ## Create a Piecewise Linear distribution from data and plot its CDF.
 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 plot (pd, "PlotType", "cdf")
 title ("CDF of Piecewise Linear distribution from Weight data")

 ## Use this to visualize the CDF of the Piecewise Linear distribution,
 ## useful for comparing to empirical CDF.

                    
plotted figure

PiecewiseLinearDistribution: r = random (pd)
PiecewiseLinearDistribution: r = random (pd, rows)
PiecewiseLinearDistribution: r = random (pd, rows, cols, …)
PiecewiseLinearDistribution: r = random (pd, [sz])

r = random (pd) returns a random number from the distribution object pd.

When called with a single size argument, betarnd returns a square matrix with the dimension specified. When called with more than one scalar argument, the first two arguments are taken as the number of rows and columns and any further arguments specify additional matrix dimensions. The size may also be specified with a row vector of dimensions, sz.

Example: 1

 

 ## Generate random samples from a Piecewise Linear distribution
 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 samples = random (pd, 500, 1);
 hist (samples, 50)
 title ("Histogram of 500 random samples from Piecewise Linear (Weight data)")
 xlabel ("values in x")
 ylabel ("Frequency")

 ## This generates random samples from a Piecewise Linear distribution,
 ## useful for simulating data based on empirical distributions.

                    
plotted figure

PiecewiseLinearDistribution: s = std (pd)

s = std (pd) computes the standard deviation of the probability distribution object, pd.

Example: 1

 

 ## Compute the standard deviation for a Piecewise Linear distribution
 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 std_value = std (pd)

 ## Use this to calculate the standard deviation, measuring variability
 ## in the empirical distribution.

std_value = 26.520
                    
PiecewiseLinearDistribution: t = truncate (pd, lower, upper)

t = truncate (pd) returns a probability distribution t, which is the probability distribution pd truncated to the specified interval with lower limit, lower, and upper limit, upper. If pd is fitted to data with fitdist, the returned probability distribution t is not fitted, does not contain any data or estimated values, and it is as it has been created with the makedist function, but it includes the truncation interval.

Example: 1

 

 ## Plot the PDF of a Piecewise Linear distribution truncated at [130, 180].
 ## Generate 10000 random samples from this truncated distribution and
 ## superimpose a histogram.

 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 t = truncate (pd, 130, 180);
 data = random (t, 10000, 1);

 ## Plot histogram and truncated PDF
 [counts, centers] = hist (data, 20);
 bin_width = centers(2) - centers(1);
 bar (centers, counts / (sum (counts) * bin_width), 1);
 hold on
 plot (t)
 hold off
 title ("Piecewise Linear distribution truncated at [130, 180]")
 legend ("Truncated PDF", "Histogram")

 ## This demonstrates truncating a Piecewise Linear distribution to a specific
 ## range and visualizing the result with random samples.

                    
plotted figure

PiecewiseLinearDistribution: v = var (pd)

v = var (pd) computes the standard deviation of the probability distribution object, pd.

Example: 1

 

 ## Compute the variance for a Piecewise Linear distribution
 load patients
 [f, x] = ecdf (Weight);
 pd = PiecewiseLinearDistribution (x(1:5:end), f(1:5:end));
 var_value = var (pd)

 ## Use this to calculate the variance, quantifying the spread in the
 ## empirical distribution.

var_value = 703.29
                    

Examples

 
 randg ("seed", 2);
 data = betarnd (2, 5, 5000, 1) * 10;
 [f, x] = ecdf (data);
 f = f(1:5:end);
 x = x(1:5:end);
 pd = PiecewiseLinearDistribution (x, f);
 [counts, centers] = hist (data, 50);
 bin_width = centers(2) - centers(1);
 bar (centers, counts / (sum (counts) * bin_width), 1);
 hold on
 vals = min (data):0.1:max (data);
 y = pdf (pd, vals);
 plot (vals, y, "-r", "LineWidth", 2)
 hold off
 title ("Piecewise Linear approximation to scaled Beta(2,5) data")
 legend ("Histogram", "Piecewise PDF")
 
plotted figure