Monday, July 4, 2011

string compare in Matlab, strcmp -

http://www.mathworks.com/help/techdoc/ref/strcmp.html
1. Basic Idea
2. full example code



1. Basic Idea
Standard input:
method is a cell
method{1} is the string for the method
method{2} is variable input arguments


method{1} = 'gauss';

 strcmp(method{1}, 'gauss')

2. Full example code

function [y,output] = simResp(method, X, int, beta)
%simResp = simulate response data for regressin models.
%This allows you to simulate regression data from general models.
%NOTES:
%1. It is good to think about the data for observation i
% Here the covariates X_i are the ith row of X (hence 1 x p)
% y_i = X_i beta + ep_i, ep_i \sim N(0,v). Hence:
% y_i \sim N(\mu_i, v), where \mu_i = X_i beta
%2. When you think about it in this form, you see the connection to GLMs
%3. It is best to write a separate function for model and simply have them called inside here;
% it makes error check much easier. e.g. for logistic regression and ordinal logistic regression,
% I'm calling outside functions.
%INPUTS
% *method - 2 x 1 cell. method{1} specifies the regression model to simulate; method{2} will
% contain any additional needed inputs. See methods after Outputs for a description.
% *X - n x p matrix. Design matrix  n replicates and p varibles (does NOT
% include the intercept).
% *beta - p x 1 matrix. covariate vector
% vector.
% *v - positive scalar. The VARIANCE (take square root for standard deviation);
% of the epsilon.
% *int - scalar. the intercept (if you don't want it, set int = 0)
%OUTPUTS
% *y = n x 1 vector. The response vector from the regression (all methods will have this)
% *output - cell. It is an any extra outputs that we might need for certain methods. See the List of methods
% which is given after list of outputs.
%METHODS:
%1. Linear Regression (Gaussian error).
% *method{1} = 'gauss'
% *method{2} = v. positive scalar. The VARIANCE (take square root for standard deviation);
% of the epsilon (in general for linear regression you need to specify the variance of the of errors.
% *output = {}; empty, there is not additional output.
%2. Logistic regression (binary, this with the logistic link)
% *SEE: the function y = simLogReg(int, beta, X)
% *method{1} = 'LR' = logistic regression
% *method{2} = {} (there are no needed extra inputs)
% *output = {}; empty, there is not additional output.
%3. Ordinal logistic regression (right now it only allows for the proportional odds model)
% *SEE: the function [ordata, latdata, newcutoff] =  simOL(Zvector, cutoff)
% *KEY: set cutoff = -1 to generate the cutoff inside the function.
% *method{1} = 'OLR' = Ordinal Logistic Regression
% *method{2}
% *output

if strcmp(method{1}, 'gauss')
    %1. linear regression model
    %remember you need to take square root of v for standard dev.
    y = int + X*beta + normrnd(0,sqrt(method{2}), size(X,1),1);
    output = {};
elseif strcmp(method{1}, 'LR')
    %2. logistic regression
    y = simLogReg(int, beta, X);
    output = {};
elseif strcmp(method{1}, 'OLR')
    %3. ordinal logistic regression
   
    %a) first we need to generate the Zvector in the standard way
    %remember we don't use the intercept here, bc for ordinal the
    %intercept is decided by the cutoff.
    %Zvector = X*beta
       
    %b) now call the function
    [y, latdata, newcutoff] =  simOL(X*beta, cutoff)
    output{1} = latdata;
    output{2} = newcutoff;
else

    error('You have entered an illegal method name; method{1} is not supported by the function')

end

Simulation Set the Seed - Clock the Seed

To clock the seed (remember rand and randn use different seeds, right?)
randn('state', sum(100*clock))
rand('state', sum(100*clock))

But for rand, should you use twister?
http://amath.colorado.edu/computing/Matlab/OldTechDocs/ref/rand.html
rand('state',s) Resets the state to s.
rand('state',0) Resets the generator to its initial state.
rand('state',j) For integer j, resets the generator to its j-th state.
rand('state',sum(100*clock)) Resets it to a different state each time.


Also, see Loren's post on this
http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numbers-part-ii/

Sunday, July 3, 2011

Simulate Discrete Random Variables

Discrete Simulation - simulate from a generic pdf

I should also see my simulation code from Cornell, that I personalized for this problem (if you weren't chosing numbers between 1 and K).

Built in Matlab
1. randi = random integer this is unifrom
2. randsample = weighted

CFX
1. gDiscrPdfRnd - I think this uses mex?
http://www.mathworks.com/matlabcentral/fileexchange/14469-performing-random-numbers-generator-from-a-generic-discrete-distribution
2. randp
http://www.mathworks.com/matlabcentral/fileexchange/8891