Advertisement
x89codered89x

Density.m

Dec 14th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.86 KB | None | 0 0
  1. classdef Density <handle
  2.     %The purpose of this class is to empirically estimate
  3.     %one dimensional probability densities using linear interpolation
  4.     %
  5.     %Dependent files:
  6.     %
  7.     %   XDomainFunction.m , http://pastebin.com/qY5DRj3i    
  8.     %   XDomainStatistics.m , http://pastebin.com/h42ZWauh
  9.     %
  10.    
  11.    properties (GetAccess = 'private', SetAccess = 'private')
  12.       Function;      
  13.       Statistics;      
  14.    end
  15.    methods
  16.        function D = Density(D,dx)
  17.            if nargin == 1
  18.                dx = 1;
  19.            end
  20.  
  21.            %add function properties
  22.            D.Statistics = XDomainStatistics();
  23.            D.Function = XDomainFunction(-dx/2,dx/2);          
  24.        end
  25.        function [n_float, supportPointsAdded] = addPoint(D,x,weight)
  26.            if nargin == 2
  27.                weight = 1;
  28.            end
  29.                                  
  30.            %update density
  31.            D.Function.addPoint(x,weight);
  32.            
  33.            %add linear interpolated weight to function
  34.            D.Statistics.addPoint(x,weight);
  35.            
  36.            n_float = D.Function.getNFloat(x);
  37.            supportPointsAdded = D.Function.supportPointsAdded;
  38.        end
  39.        function p = prob(D,x)
  40.           %x is scalar
  41.           p = D.Function.linearInterpolatedValue(x)/D.Statistics.cumWeight;
  42.        end              
  43.        
  44.        %Extra Statistics member functions
  45.        function n_MaxP = nMaxP(D)
  46.            n_MaxP = D.Function.nMaxValue;
  47.        end
  48.        function n_MinP = nMinP(D)
  49.            n_MinP = D.Function.nMinValue;
  50.        end
  51.        function minP = minP(D)
  52.            minP = D.Function.minValue()/D.Statistics.cumWeight;
  53.        end
  54.        function maxP = maxP(D)
  55.            maxP = D.Function.maxValue()/D.Statistics.cumWeight;
  56.        end
  57.        
  58.        %Overloaded Statistics
  59.        function ExpectedValue = Ex(D)
  60.            ExpectedValue = D.Statistics.Ex;      
  61.        end
  62.        function ExpectedValueSquared = Exx(D)
  63.            ExpectedValueSquared = D.Statistics.Exx;      
  64.        end      
  65.        function Variance = Var(D)
  66.            Variance = D.Statistics.Var;
  67.        end
  68.        function functionX = fX(D,nn)
  69.            if nargin == 2
  70.                functionX = D.Function.fX(nn)/D.Statistics.cumWeight;
  71.            else
  72.                functionX = D.Function.fX/D.Statistics.cumWeight;      
  73.            end                  
  74.        end
  75.        function domainX = X(D,nn)
  76.            if nargin == 2
  77.                domainX = D.Function.X(nn);            
  78.            else
  79.                domainX = D.Function.X;
  80.            end
  81.        end
  82.        function differentialX = dX(D)
  83.            differentialX = D.Function.dX;            
  84.        end      
  85.        function cumulativeSum = cumWeight(D)
  86.            cumulativeSum = D.Statistics.cumWeight;            
  87.        end              
  88.    end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement