18 #ifndef __itkPSMMixedEffectsShapeMatrixAttribute_hxx 19 #define __itkPSMMixedEffectsShapeMatrixAttribute_hxx 20 #include "itkPSMMixedEffectsShapeMatrixAttribute.h" 26 template <
class T,
unsigned int VDimension>
27 void PSMMixedEffectsShapeMatrixAttribute<T,VDimension>
31 vnl_vector<double> tempvect;
32 int indexSum = 0, group_indx = -1;
33 tempvect.set_size(m_MeanMatrix.rows());
35 for (
unsigned int i = 0; i < m_MeanMatrix.cols(); i++)
48 group_indx = group_indx + 1;
49 indexSum += m_TimeptsPerIndividual(group_indx);
52 tempvect = m_Intercept + m_Slope * m_Expl(i);
53 tempvect = tempvect + m_InterceptRand.get_row(group_indx);
54 tempvect = tempvect + m_SlopeRand.get_row(group_indx) * m_Expl(i);
56 m_MeanMatrix.set_column(i, tempvect);
61 template <
class T,
unsigned int VDimension>
62 void PSMMixedEffectsShapeMatrixAttribute<T,VDimension>::
65 std::cout <<
"Estimating params" << std::endl;
67 vnl_matrix<double> X = *
this + m_MeanMatrix;
70 int num_shapes =
static_cast<double>(X.cols());
75 m_SlopeRand.set_size(m_NumIndividuals, nr);
76 m_InterceptRand.set_size(m_NumIndividuals, nr);
78 vnl_matrix<double> fixed;
79 vnl_matrix<double> random;
80 fixed.set_size(2, nr);
81 random.set_size(2, nr * m_NumIndividuals);
83 vnl_matrix<double> Ds(2, 2);
85 vnl_matrix<double> identity_2;
86 identity_2.set_size(2, 2);
87 identity_2.set_identity();
90 vnl_matrix<double> * Ws = NULL, * Vs = NULL, * identity_n = NULL, * Xp = NULL;
91 Ws =
new vnl_matrix<double>[m_NumIndividuals];
92 Vs =
new vnl_matrix<double>[m_NumIndividuals];
93 identity_n =
new vnl_matrix<double>[m_NumIndividuals];
94 Xp =
new vnl_matrix<double>[m_NumIndividuals];
96 vnl_vector<double> residual, y;
97 y.set_size(m_TimeptsPerIndividual(0));
99 residual.set_size(m_TimeptsPerIndividual(0));
102 for (
int i = 0; i < m_NumIndividuals; i++)
104 Vs[i].set_size(m_TimeptsPerIndividual(i), m_TimeptsPerIndividual(i));
105 Ws[i].set_size(m_TimeptsPerIndividual(i), m_TimeptsPerIndividual(i));
107 identity_n[i].set_size(m_TimeptsPerIndividual(i), m_TimeptsPerIndividual(i));
108 identity_n[i].set_identity();
110 Xp[i].set_size(m_TimeptsPerIndividual(i), 2);
113 vnl_matrix<double> sum_mat1(2, 2, 0);
114 vnl_vector<double> sum_mat2(2); sum_mat2.fill(0.0);
116 double tracevar = 0.0;
117 vnl_matrix<double> bscorr(2, 2, 0.0);
118 vnl_matrix<double> bsvar(2, 2, 0.0);
119 vnl_vector<double> tempvect; tempvect.set_size(2);
120 unsigned int indexSum = 0;
122 for (
int i = 0; i < nr; i++)
127 for (
int j = 0; j < 100; j++)
129 sum_mat1.fill(0.0); sum_mat2.fill(0.0);
132 for (
int k = 0; k < m_NumIndividuals; k++)
135 y.set_size(m_TimeptsPerIndividual(k));
138 for (
int l = 0; l < m_TimeptsPerIndividual(k); l++)
140 Xp[k](l, 0) = m_Expl(indexSum + l);
142 y(l) = X(i, indexSum + l);
145 indexSum += m_TimeptsPerIndividual(k);
147 Vs[k] = (identity_n[k] * sigma2s) + Xp[k] * Ds * vnl_transpose(Xp[k]);
151 Ws[k] = vnl_matrix_inverse<double>(Vs[k]);
155 Ws[k] = vnl_inverse<double>(Vs[k]);
157 sum_mat1 = sum_mat1 + vnl_transpose(Xp[k]) * Ws[k] * Xp[k];
158 sum_mat2 = sum_mat2 + vnl_transpose(Xp[k]) * Ws[k] * y;
160 tempvect = vnl_inverse<double>(sum_mat1) * sum_mat2;
161 fixed.set_column(i, tempvect);
169 for (
int k = 0; k < m_NumIndividuals; k++)
171 residual.clear(); y.clear();
172 residual.set_size(m_TimeptsPerIndividual(k));
173 y.set_size(m_TimeptsPerIndividual(k));
174 residual.fill(0.0); y.fill(0.0);
176 for (
int l = 0; l < m_TimeptsPerIndividual[k]; l++)
178 Xp[k](l,0) = m_Expl(indexSum + l);
180 y(l) = X(i, indexSum + l);
183 indexSum += m_TimeptsPerIndividual(k);
185 tempvect = Ds * vnl_transpose(Xp[k]) * Ws[k] * (y - (Xp[k] * fixed.get_column(i)));
186 random.set_column(i * m_NumIndividuals + k, tempvect);
187 residual = y - (Xp[k] * fixed.get_column(i)) - (Xp[k] * random.get_column(i * m_NumIndividuals + k));
188 ecorr = ecorr + dot_product(residual, residual);
189 tracevar = tracevar + (m_TimeptsPerIndividual(k) - sigma2s * vnl_trace(Ws[k]));
190 bscorr = bscorr + outer_product(random.get_column(i * m_NumIndividuals + k), random.get_column(i * m_NumIndividuals + k));
191 bsvar = bsvar + (identity_2 - (vnl_transpose(Xp[k]) * Ws[k] * Xp[k] * Ds));
195 sigma2s = (ecorr + sigma2s * tracevar) / (num_shapes);
196 Ds = (bscorr + Ds * bsvar) / m_NumIndividuals;
200 m_Slope = fixed.get_row(0);
201 m_Intercept = fixed.get_row(1);
202 for (
int i = 0; i < m_NumIndividuals; i++)
204 for (
int j = 0; j < nr; j++)
206 m_SlopeRand(i, j) = random(0, j * m_NumIndividuals + i);
207 m_InterceptRand(i, j) = random(1, j * m_NumIndividuals + i);
213 delete [] identity_n;