-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwc.m
59 lines (48 loc) · 1.35 KB
/
pwc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% Class pwc
%
% Created by:
% Stephen V. Gliske ([email protected])
% Department of Neurosurgery
% University of Nebraska Medical Center
%
% (c) 2020-2022
% Released under CC BY-NC-SA. No warrenty express or implied.
%
classdef pwc
properties
x(:,1) double {mustBeReal, mustBeFinite}
y(:,1) double {mustBeReal, mustBeFinite}
w(:,1) double {mustBeReal, mustBeFinite}
max_x(1,1) double {mustBeReal}
end
methods
function obj = pwc( x_in, y_in, max_x )
obj.x = x_in;
obj.y = y_in;
obj.w = diff( [x_in; max_x] );
obj.max_x = max_x;
end
function bin = getBin( obj, x )
bin = discretize( x, [obj.x; obj.max_x ] )';
end
function y = getValue( obj, x )
y = obj.y( obj.getBin(x) );
end
function w = getWidth( obj, x )
w = obj.w( obj.getBin(x) );
end
function a = getArea( obj )
a = obj.w' * obj.y;
end
function x_out = genData( obj, N )
%% generate data according to the distribution
a = obj.y .* obj.w;
cdf = [obj.x(1); cumsum( a ) ];
a = a / max(cdf);
cdf = cdf / max(cdf);
r = rand(N,1)*(obj.max_x-obj.x(1)) + obj.x(1);
b = discretize( r, cdf );
x_out = (r - cdf(b)) ./ a(b) .* obj.w(b) + obj.x(b);
end
end
end