Skip to content

Commit

Permalink
Adds changes to C++ styling
Browse files Browse the repository at this point in the history
  • Loading branch information
ncordon committed Jan 27, 2019
1 parent faa2922 commit ab9c905
Show file tree
Hide file tree
Showing 8 changed files with 660 additions and 625 deletions.
47 changes: 29 additions & 18 deletions src/0_classesAndMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,79 @@ using namespace Rcpp;
bool isProb(double prob) {
return (prob >= 0 && prob <= 1);
}

// doubt
// [[Rcpp::export(.isGenRcpp)]]
bool isGen(NumericMatrix gen) {
for(int i = 0; i < gen.nrow(); i++)
for(int j = 0; j < gen.ncol(); j++)
if((i == j && gen(i, j) > 0) || (i != j && gen(i, j) < 0))
for (int i = 0; i < gen.nrow(); i++)
for (int j = 0; j < gen.ncol(); j++)
if ((i == j && gen(i, j) > 0) || (i != j && gen(i, j) < 0))
return false;

return true;
}

SEXP commClassesKernel(NumericMatrix P);

// method to convert into canonic form a markovchain object
// [[Rcpp::export(.canonicFormRcpp)]]
SEXP canonicForm (S4 object)
{
SEXP canonicForm(S4 object) {
NumericMatrix P = object.slot("transitionMatrix");
List comclasList = commClassesKernel(P);
LogicalVector vu = comclasList["v"];
NumericVector u, w;
for(int i = 0; i < vu.size(); i ++) {

for (int i = 0; i < vu.size(); i ++) {
if(vu[i]) u.push_back(i);
else w.push_back(i);
}

LogicalMatrix Cmatr = comclasList["C"];
NumericVector R, p;
LogicalVector crow;
while(u.size()>0)
{

while (u.size() > 0) {
R.push_back(u[0]);
crow = Cmatr(u[0], _);
for(int i = 0; i < crow.size(); i++)

for (int i = 0; i < crow.size(); i++)
vu[i] = vu[i] * !crow[i];

u = NumericVector::create();
for(int i = 0; i < vu.size(); i ++)

for (int i = 0; i < vu.size(); i ++)
if(vu[i]) u.push_back(i);
}
for (int i = 0; i < R.size(); i ++)
{

for (int i = 0; i < R.size(); i ++) {
crow = Cmatr(R[i], _);
for(int j = 0; j < crow.size(); j++)

for (int j = 0; j < crow.size(); j++)
if(crow[j]) p.push_back(j);
}
for(NumericVector::iterator it = w.begin(); it != w.end(); it++)

for (NumericVector::iterator it = w.begin(); it != w.end(); it++)
p.push_back(*it);

NumericMatrix Q(p.size());
CharacterVector rnames(P.nrow());
CharacterVector cnames(P.ncol());
CharacterVector r = rownames(P);
CharacterVector c = colnames(P);
for(int i = 0; i < p.size(); i ++) {

for (int i = 0; i < p.size(); i ++) {
rnames[i] = r[p[i]];
for(int j = 0; j < p.size(); j ++) {
for (int j = 0; j < p.size(); j ++) {
Q(i, j) = P(p[i], p[j]);
cnames[j] = c[p[j]];
}
}

Q.attr("dimnames") = List::create(rnames, cnames);
S4 out("markovchain");
out.slot("transitionMatrix") = Q;
out.slot("name") = object.slot("name");

return out;

}
Expand All @@ -83,8 +94,8 @@ SEXP lexicographicalSort(SEXP y) {
NumericMatrix m(y);
std::vector< std::vector<double> > x(m.nrow(), std::vector<double>(m.ncol()));

for(int i=0; i<m.nrow(); i++)
for(int j=0; j<m.ncol(); j++)
for (int i=0; i<m.nrow(); i++)
for (int j=0; j<m.ncol(); j++)
x[i][j] = m(i,j);

sort(x.begin(), x.end());
Expand Down
24 changes: 13 additions & 11 deletions src/0_ctmcClassesAndMethodsSAI.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#include <Rcpp.h>
using namespace Rcpp;

//obtain transition probability matrix from the generator matrix
// [[Rcpp::export]]
NumericMatrix generatorToTransitionMatrix(NumericMatrix gen, bool byrow = true){
NumericMatrix transMatr(gen.nrow());
transMatr.attr("dimnames") = gen.attr("dimnames");

if(byrow)
for(int i = 0; i < gen.nrow(); i++){
for(int j = 0; j < gen.ncol(); j++){
if(i != j)
transMatr(i, j) = -gen(i, j) / gen(i, i);
if (byrow) {
for (int i = 0; i < gen.nrow(); i++){
for (int j = 0; j < gen.ncol(); j++){
if (i != j)
transMatr(i, j) = -gen(i, j) / gen(i, i);
}
}
}
else
for(int j = 0; j < gen.ncol(); j++){
for(int i = 0; i < gen.nrow(); i++){
if(i != j)
transMatr(i, j) = -gen(i, j) / gen(j, j);
} else {
for (int j = 0; j < gen.ncol(); j++){
for (int i = 0; i < gen.nrow(); i++){
if (i != j)
transMatr(i, j) = -gen(i, j) / gen(j, j);
}
}
}

Expand Down
60 changes: 35 additions & 25 deletions src/1_ctmcFunctions4Fitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,44 @@ using namespace Rcpp;

#include <math.h>

List markovchainFit(SEXP data, String method="mle", bool byrow=true, int nboot=10, double laplacian=0
, String name="", bool parallel=false, double confidencelevel=0.95, bool confint = true
, NumericMatrix hyperparam = NumericMatrix(), bool sanitize = false, CharacterVector possibleStates = CharacterVector());
List markovchainFit(SEXP data, String method = "mle", bool byrow = true,
int nboot = 10, double laplacian = 0, String name = "",
bool parallel = false, double confidencelevel = 0.95, bool confint = true,
NumericMatrix hyperparam = NumericMatrix(), bool sanitize = false,
CharacterVector possibleStates = CharacterVector());

// [[Rcpp::export]]
List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel = 0.95)
{
List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel = 0.95) {
CharacterVector stateData(as<CharacterVector>(data[0]).size());
for(int i = 0; i < as<CharacterVector>(data[0]).size(); i++)

for (int i = 0; i < as<CharacterVector>(data[0]).size(); i++)
stateData[i] = as<CharacterVector>(data[0])[i];

NumericVector transData = data[1];
CharacterVector sortedStates = unique(as<CharacterVector>(data[0])).sort();
NumericVector stateCount(sortedStates.size());
NumericVector stateSojournTime(sortedStates.size());

List dtmcData = markovchainFit(stateData, "mle", byrow, 10, 0, name, false, confidencelevel);

for(int i = 0; i < stateData.size() - 1; i++){
int idx = std::find(sortedStates.begin(), sortedStates.end(), stateData[i]) - sortedStates.begin();
for (int i = 0; i < stateData.size() - 1; i++){
int idx = std::find(sortedStates.begin(),
sortedStates.end(),
stateData[i]) - sortedStates.begin();
stateCount[idx]++;
stateSojournTime[idx] += transData[i+1] - transData[i];
}

S4 dtmcEst = dtmcData["estimate"];
NumericMatrix gen = dtmcEst.slot("transitionMatrix");

for(int i = 0; i < gen.nrow(); i++){
for(int j = 0; j < gen.ncol(); j++){
if(stateCount[i] > 0)
for (int i = 0; i < gen.nrow(); i++){
for (int j = 0; j < gen.ncol(); j++){
if (stateCount[i] > 0)
gen(i, j) *= stateCount[i] / stateSojournTime[i];
}
if(stateCount[i] > 0)
if (stateCount[i] > 0)
gen(i, i) = - stateCount[i] / stateSojournTime[i];
else
gen(i, i) = -1;
Expand All @@ -45,12 +51,13 @@ List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel
double zscore = stats::qnorm_0(confidencelevel, 1.0, 0.0);
NumericVector lowerConfVecLambda(sortedStates.size()), upperConfVecLambda(sortedStates.size());

for(int i = 0; i < sortedStates.size(); i++){
if(stateCount[i] > 0){
lowerConfVecLambda(i) = std::max(0., stateCount[i] / stateSojournTime[i] * (1 - zscore / sqrt(stateCount[i])));
upperConfVecLambda(i) = std::min(1., stateCount[i] / stateSojournTime[i] * (1 + zscore / sqrt(stateCount[i])));
}
else{
for (int i = 0; i < sortedStates.size(); i++){

if (stateCount[i] > 0){
auto factor = stateCount[i] / stateSojournTime[i] * (1 - zscore / sqrt(stateCount[i]));
lowerConfVecLambda(i) = std::max(0., factor);
upperConfVecLambda(i) = std::min(1., factor);
} else {
lowerConfVecLambda(i) = 1;
upperConfVecLambda(i) = 1;
}
Expand All @@ -62,10 +69,13 @@ List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel
outCtmc.slot("name") = name;

return List::create(_["estimate"] = outCtmc,
_["errors"] = List::create(_["dtmcConfidenceInterval"] = List::create(
_["confidenceLevel"] = dtmcData["confidenceLevel"],
_["lowerEndpointMatrix"] = dtmcData["lowerEndpointMatrix"],
_["upperEndpointMatrix"] = dtmcData["upperEndpointMatrix"]),
_["lambdaConfidenceInterval"] = List::create(_["lowerEndpointVector"] = lowerConfVecLambda,
_["upperEndpointVector"] = upperConfVecLambda)));
}
_["errors"] = List::create(
_["dtmcConfidenceInterval"] = List::create(
_["confidenceLevel"] = dtmcData["confidenceLevel"],
_["lowerEndpointMatrix"] = dtmcData["lowerEndpointMatrix"],
_["upperEndpointMatrix"] = dtmcData["upperEndpointMatrix"]),
_["lambdaConfidenceInterval"] = List::create(
_["lowerEndpointVector"] = lowerConfVecLambda,
_["upperEndpointVector"] = upperConfVecLambda))
);
}
Loading

0 comments on commit ab9c905

Please sign in to comment.