Skip to content

Commit

Permalink
fix truncated random in PiecewiseLinear distribution classdef
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0m1th3as committed Feb 18, 2024
1 parent 54a4a4a commit d95e0b2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion inst/dist_obj/PiecewiseLinearDistribution.m
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,16 @@ function disp (this)
error ("random: requires a scalar probability distribution.");
endif
if (this.IsTruncated)
r = plrnd (this.Truncation(1), this.Truncation(2), varargin{:});
lp = plcdf (this.Truncation(1), this.x, this.Fx);
up = plcdf (this.Truncation(2), this.x, this.Fx);
u = unifrnd (lp, up, varargin{:});
r = zeros (size (u));
[~, bin] = histc (u(:)', this.Fx);
r0 = this.x(bin);
dx = diff (this.x);
dF = diff (this.Fx);
dr = (u(:)' - this.Fx(bin)) .* dx(bin) ./ dF(bin);
r(:) = r0 + dr;
else
r = plrnd (this.x, this.Fx, varargin{:});
endif
Expand Down

0 comments on commit d95e0b2

Please sign in to comment.