Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hendri54 committed Jan 5, 2017
1 parent 8023c46 commit e2b350e
Show file tree
Hide file tree
Showing 103 changed files with 15,651 additions and 44 deletions.
1 change: 1 addition & 0 deletions +cellLH/text_table.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function text_table(dataM, rowUnderlineV, fp, dbg)
if isempty(fp)
fp = 1;
end
assert(fp > 0, 'File pointer not valid');

[nRows, nCols] = size(dataM);

Expand Down
56 changes: 56 additions & 0 deletions +configLH/MatlabEditor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
classdef MatlabEditor < handle

properties
% File for saved state
stateFileName char
end

methods
%% Constructor
function meS = MatlabEditor
lhS = const_lh;
meS.stateFileName = fullfile(lhS.dirS.sharedDirV{1}, 'editor_state.mat');
end


%% Close all open files +++++


%% Generate object with editor state
function sS = get_state(meS)
sS = configLH.MatlabEditorState;
end


%% Save editor state
function save_state(meS, fileName)
sS = configLH.MatlabEditorState;
if nargin < 2
fileName = meS.stateFileName;
end
save(fileName, 'sS');
end


%% Load editor state
function sS = load_state(meS, fileName)
if nargin < 2
fileName = meS.stateFileName;
end
sS = load(fileName);
sS = sS.sS;
assert(isa(sS, 'configLH.MatlabEditorState'));
end


%% Restore state
function restore_state(meS, fileName)
if nargin < 2
fileName = meS.stateFileName;
end
sS = meS.load_state(fileName);
sS.restore_state;
end
end

end
61 changes: 61 additions & 0 deletions +configLH/MatlabEditorState.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
classdef MatlabEditorState < handle
%{
This is just a container used by MatlabEditor.
Save and load methods are there
%}

properties
% Paths of open files
openFileV cell
% Line number for each file
lineNumberV double
end

methods
%% Constructor
function sS = MatlabEditorState
sS.get_state;
end


%% Get state
function get_state(sS)
X = matlab.desktop.editor.getAll;
n = length(X);

if n > 0
sS.openFileV = cell(n, 1);
sS.lineNumberV = ones(n, 1);
for i1 = 1 : n
sS.openFileV{i1} = X(i1).Filename;
sS.lineNumberV(i1) = X(i1).Selection(1);
end

else
sS.openFileV = [];
sS.lineNumberV = [];
end
end


%% Restore state
function restore_state(sS)
% Open files
n = length(sS.openFileV);
if n > 0
for i1 = 1 : n
try
edit(sS.openFileV{i1});
%matlab.desktop.editor.openAndGoToLine(sS.openFileV{i1}, sS.lineNumberV(i1))
matlab.desktop.editor.openDocument(sS.openFileV{i1});
activeDoc = matlab.desktop.editor.getActive;
activeDoc.goToPositionInLine(sS.lineNumberV(i1), 1);
catch
fprintf('Cannot open file %s \n', sS.openFileV{i1});
end
end
end
end
end

end
7 changes: 7 additions & 0 deletions +configLH/MatlabEditorStateTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function MatlabEditorStateTest

sS = configLH.MatlabEditorState;

sS.restore_state;

end
17 changes: 17 additions & 0 deletions +configLH/MatlabEditorTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function MatlabEditorTest

lhS = const_lh;
fileName = fullfile(lhS.dirS.testFileDir, 'editor_state');

meS = configLH.MatlabEditor;

sS = meS.get_state;

meS.save_state(fileName);
s2S = meS.load_state(fileName);
assert(isequal(sS, s2S));

meS.restore_state(fileName);


end
23 changes: 23 additions & 0 deletions +configLH/quit_save.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function quit_save(projectSuffix)
% Quit matlab after saving project state
%{
Restore state with `project_start`
%}

assert(nargin == 1);

% Retrieve project info
plS = ProjectListLH;
pS = plS.retrieve_suffix(projectSuffix);
assert(~isempty(pS));

% Save project state
meS = configLH.MatlabEditor;
assert(~isempty(pS.stateFileStr));
meS.save_state(pS.stateFileStr);

% Quit matlab
quit;


end
24 changes: 24 additions & 0 deletions +dataLH/Variable.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
validValueV double
% Missing value codes
missValCodeV double
% These values indicate top codes
topCodeV double

% Other properties, stored as name/value pairs (cell array)
otherV cell
Expand Down Expand Up @@ -60,6 +62,28 @@ function validate(vS)
end
end
end


%% Check that an input has codes consistent with that variable
function [out1, outMsg] = is_valid(vS, inV)
out1 = true;
outMsg = 'valid';

if ~isempty(vS.minVal)
if any(inV < vS.minVal)
out1 = false;
outMsg = 'values below minimum';
return;
end
end
if ~isempty(vS.maxVal)
if any(inV > vS.maxVal)
out1 = false;
outMsg = 'values above maximum';
return;
end
end
end
end

end
13 changes: 9 additions & 4 deletions +distribLH/class_assign.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
IN:
xV
matrices are flattened into vectors
wtV
Need not sum to 1
May be [] for unweighted data
Expand All @@ -29,6 +30,8 @@ The smallest clUbV(1) pct of the xV get xClV = 1 etc.
AUTHOR: Lutz Hendricks, 1999
Checked: 2015-Feb-25
Was previously called just class_assign
%}


Expand All @@ -41,7 +44,7 @@ The smallest clUbV(1) pct of the xV get xClV = 1 etc.
error('Invalid nargin');
end

n = length(xV);
n = numel(xV);
nc = length(clUbV);
clUbV = double(clUbV(:));

Expand All @@ -58,6 +61,7 @@ The smallest clUbV(1) pct of the xV get xClV = 1 etc.


if dbg > 10
validateattributes(xV, {'numeric'}, {'finite', 'nonnan', 'nonempty', 'real'})
validateattributes(clUbV(:), {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'size', [nc, 1], ...
'>=', 0, '<=', 1})
% Upper bounds must be increasing (weakly)
Expand Down Expand Up @@ -86,7 +90,8 @@ The smallest clUbV(1) pct of the xV get xClV = 1 etc.
% cumWtV(end) = 1;
%
%
% %% Alternative algorithm

%% Alternative algorithm
%
% % % Upper bounds for all classes
% % xUbV = repmat(tmp(1,1) - 1, [nc, 1]);
Expand All @@ -103,9 +108,9 @@ The smallest clUbV(1) pct of the xV get xClV = 1 etc.
% % Find class upper bounds by interpolating cumulative weights
% xUbV = interp1(cumWtV, tmp(:,1), clUbV, 'linear', 'extrap');

xUbV = distribLH.cl_bounds_w(xV(:), wtV(:), clUbV, dbg);
xUbV = distribLH.cl_bounds_w(double(xV(:)), double(wtV(:)), clUbV, dbg);
% For numerical reasons, push right edges up a bit
xClV = discretize(double(xV(:)), [min(xV)-1; xUbV + 1e-8], 'IncludedEdge', 'left');
xClV = discretize(double(xV(:)), [min(xV(:))-1; xUbV + 1e-8], 'IncludedEdge', 'left');


%% Assign class to each obs
Expand Down
3 changes: 3 additions & 0 deletions +distribLH/class_assign_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
dbg = 111;
rng(21);

% Test with matrix input
distribLH.class_assign(rand(10, 8), [], linspace(0.3, 1, 3), dbg);


%% Uniform random numbers
if 1
Expand Down
65 changes: 65 additions & 0 deletions +econLH/GridBounded.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
% Bounded grid, calibrated from bounded parameters
%{
Example:
A model with a capital grid between kMin and kMax
Calibrate the grid points
Calibrated parameters are
k1Ratio = (k1 - kMin) / (kMax - kMin)
in [0, 1)
dkV(1 : (n-1))
dkV(j) = (k(j+1) - k(j)) / (kMax - k(j))
in (0, 1)
When calling this, bound all inputs to ensure that grid points do not become identical
%}
classdef GridBounded

properties
% No of grid points
n double
% Bounds
xMin double
xMax double
% % Minimum gap between grid points (optional)
% minGap double = 1e-8;
end


methods
%% Constructor
function gS = GridBounded(n, xMin, xMax)
gS.n = n;
gS.xMin = xMin;
gS.xMax = xMax;

gS.validate;
end


%% Validate
function validate(gS)
validateattributes(gS.xMax, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', ...
'>', gS.xMin + 1e-6})
end


%% Make the grid from calibrated parameters
function gridV = make_grid(gS, x1Ratio, dxV)
validateattributes(dxV(:), {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'positive', '<', 1, ...
'size', [gS.n - 1, 1]})
validateattributes(x1Ratio, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'scalar', '>=', gS.xMin, ...
'<', 1 - 1e-6})

gridV = zeros(gS.n, 1);
gridV(1) = gS.xMin + x1Ratio .* (gS.xMax - gS.xMin);
for ig = 1 : (gS.n - 1)
gridV(ig + 1) = gridV(ig) + dxV(ig) .* (gS.xMax - gridV(ig));
end

validateattributes(gridV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'increasing', ...
'>=', gS.xMin, '<=', gS.xMax, 'size', [gS.n, 1]})
end
end


end
16 changes: 16 additions & 0 deletions +econLH/GridBoundedTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function GridBoundedTest

n = 9;
xMin = -0.9;
xMax = 0.3;

gS = econLH.GridBounded(n, xMin, xMax);

rng(904);
x1Ratio = rand(1);
dxV = rand(n-1, 1);
gridV = gS.make_grid(x1Ratio, dxV)

%keyboard;

end
Loading

0 comments on commit e2b350e

Please sign in to comment.