-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvarbvsbinupdatemex.c
More file actions
75 lines (65 loc) · 2.64 KB
/
Copy pathvarbvsbinupdatemex.c
File metadata and controls
75 lines (65 loc) · 2.64 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Part of the varbvs package, https://github.com/pcarbo/varbvs
//
// Copyright (C) 2012-2017, Peter Carbonetto
//
// This program is free software: you can redistribute it under the
// terms of the GNU General Public License; either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANY; without even the implied warranty of
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// For a description of this C code, see varbvsbinupdate.m.
#include "types.h"
#include "misc.h"
#include "doublevectormex.h"
#include "singlematrixmex.h"
#include "varbvs.h"
#include "mex.h"
#include "matrix.h"
void mexFunction (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
// (1) GET INPUTS
// --------------
const SingleMatrix X = getSingleMatrix(prhs[0]);
const double sa = *mxGetPr(prhs[1]);
const DoubleVector logodds = getDoubleVector(prhs[2]);
const DoubleVector d = getDoubleVector(mxGetField(prhs[3],0,"d"));
const DoubleVector xdx = getDoubleVector(mxGetField(prhs[3],0,"xdx"));
const DoubleVector xy = getDoubleVector(mxGetField(prhs[3],0,"xy"));
const DoubleVector xd = getDoubleVector(mxGetField(prhs[3],0,"xd"));
const DoubleVector alpha0 = getDoubleVector(prhs[4]);
const DoubleVector mu0 = getDoubleVector(prhs[5]);
const DoubleVector Xr0 = getDoubleVector(prhs[6]);
const DoubleVector I = getDoubleVector(prhs[7]);
// Get the number of samples (n), the number of variables (p), and
// the number of coordinate ascent updates (numiter).
const Size n = X.nr;
const Size p = X.nc;
const Size numiter = I.n;
// (2) INITIALIZE OUTPUTS
// ----------------------
DoubleVector alpha = createMatlabVector(p,&plhs[0]);
DoubleVector mu = createMatlabVector(p,&plhs[1]);
DoubleVector Xr = createMatlabVector(n,&plhs[2]);
copyDoubleVector(alpha0,alpha);
copyDoubleVector(mu0,mu);
copyDoubleVector(Xr0,Xr);
// This is storage for a column of matrix X.
double* x = malloc(sizeof(double)*n);
// (3) RUN COORDINATE ASCENT UPDATES
// ---------------------------------
// Repeat for each coordinate ascent update.
for (Index iter = 0; iter < numiter; iter++) {
Index k = (Index) I.elems[iter];
// Copy the kth column of matrix X.
copyColumn(X.elems,x,k,n);
// Perform the update.
varbvsbinupdate(x,xy.elems[k],xd.elems[k],xdx.elems[k],d.elems,sa,
logodds.elems[k],alpha.elems+k,mu.elems+k,Xr.elems,n);
}
// Free the dynamically allocated memory.
free(x);
}