[Rsiena-commits] r62 - in pkg/RSienaTest: . doc inst/examples src/data src/model src/model/filters src/model/ml src/model/variables src/utils

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Mar 15 15:56:31 CET 2010


Author: ripleyrm
Date: 2010-03-15 15:56:31 +0100 (Mon, 15 Mar 2010)
New Revision: 62

Modified:
   pkg/RSienaTest/changeLog
   pkg/RSienaTest/doc/RSienaDeveloper.tex
   pkg/RSienaTest/doc/Siena_algorithms2.tex
   pkg/RSienaTest/inst/examples/baerveldt3.csv
   pkg/RSienaTest/inst/examples/baerveldt4.csv
   pkg/RSienaTest/src/data/BehaviorLongitudinalData.cpp
   pkg/RSienaTest/src/data/BehaviorLongitudinalData.h
   pkg/RSienaTest/src/model/EpochSimulation.cpp
   pkg/RSienaTest/src/model/EpochSimulation.h
   pkg/RSienaTest/src/model/filters/AtLeastOneFilter.cpp
   pkg/RSienaTest/src/model/filters/AtLeastOneFilter.h
   pkg/RSienaTest/src/model/filters/DisjointFilter.cpp
   pkg/RSienaTest/src/model/filters/DisjointFilter.h
   pkg/RSienaTest/src/model/filters/HigherFilter.cpp
   pkg/RSienaTest/src/model/filters/HigherFilter.h
   pkg/RSienaTest/src/model/filters/LowerFilter.cpp
   pkg/RSienaTest/src/model/filters/LowerFilter.h
   pkg/RSienaTest/src/model/filters/PermittedChangeFilter.h
   pkg/RSienaTest/src/model/ml/BehaviorChange.cpp
   pkg/RSienaTest/src/model/ml/BehaviorChange.h
   pkg/RSienaTest/src/model/ml/Chain.cpp
   pkg/RSienaTest/src/model/ml/Chain.h
   pkg/RSienaTest/src/model/ml/MiniStep.cpp
   pkg/RSienaTest/src/model/ml/MiniStep.h
   pkg/RSienaTest/src/model/ml/NetworkChange.cpp
   pkg/RSienaTest/src/model/ml/NetworkChange.h
   pkg/RSienaTest/src/model/variables/BehaviorVariable.cpp
   pkg/RSienaTest/src/model/variables/BehaviorVariable.h
   pkg/RSienaTest/src/model/variables/DependentVariable.cpp
   pkg/RSienaTest/src/model/variables/DependentVariable.h
   pkg/RSienaTest/src/model/variables/NetworkVariable.cpp
   pkg/RSienaTest/src/model/variables/NetworkVariable.h
   pkg/RSienaTest/src/utils/Random.cpp
   pkg/RSienaTest/src/utils/Random.h
Log:
Changes to documentation. Example files with changing covariates and 2 waves changed to constant covariates. 

Modified: pkg/RSienaTest/changeLog
===================================================================
--- pkg/RSienaTest/changeLog	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/changeLog	2010-03-15 14:56:31 UTC (rev 62)
@@ -1,15 +1,29 @@
-2010-02-12 R-forge revision 58
+2010-02-16 R-forge revision 60
 
+	* R/printInitialDescription.r, R/sienaDataCreate.r,
+	src/data/NetworkLongitudinalData.h,
+	src/data/NetworkLongitudinalData.cpp, src/siena07.cpp: average
+	indegrees and outdegrees added to data objects, reports and passed
+	into to C++
+
+	* R/print01Report.r: report on constraints, reformat list of
+	inactive actors.
+
+	* R/simstatsc.r, R/phase2.r, R/maxlike.r: fixes for maxlike
+	routine.
+
+2010-02-12 R-forge revision 59
+
 	* DESCRIPTION, man/RSiena-package.Rd: new version: 1.0.11
 	* R/siena07.r, R/zzz.R, R/siena01.r, R/sienaRDocumentation.r,
-	R/robmon.r, R/print01Report.r, R/getTargets.r, R/simstatsc.r: 
-	replace string "RSiena" or "RSienaTest" by variable containing 
+	R/robmon.r, R/print01Report.r, R/getTargets.r, R/simstatsc.r:
+	bug fix: replace string "RSiena" or "RSienaTest" by variable containing
 	package name
-	* R/makelike.r, R/maxlikecalc.r, man/maxlikefn.Rd, 
+	* R/makelike.r, R/maxlikecalc.r, man/maxlikefn.Rd,
 	man/sienaModelCreate.rd, R/phase2.r, R/phase3.r,
 	R/sienaModelCreate.r, R/sienaprint.r, NAMESPACE: simple MCMC
 	routine intended for algorithm testing only.
-		
+
 2010-02-11 R-forge revision 57
 
 	* R/siena01.r: fix bug which stopped Siena update of effects

Modified: pkg/RSienaTest/doc/RSienaDeveloper.tex
===================================================================
--- pkg/RSienaTest/doc/RSienaDeveloper.tex	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/doc/RSienaDeveloper.tex	2010-03-15 14:56:31 UTC (rev 62)
@@ -303,6 +303,6 @@
 an embarrassingly large memory usage, which will fail sooner or later.
 
 If \sfn{valgrind} reports use of uninitialised variables, use the option
-\sfn{=--track-origins=yes} to find out where the problem started. It can be
+\sfn{--track-origins=yes} to find out where the problem started. It can be
 surprisingly difficult to track these down!
 \end{document}

Modified: pkg/RSienaTest/doc/Siena_algorithms2.tex
===================================================================
--- pkg/RSienaTest/doc/Siena_algorithms2.tex	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/doc/Siena_algorithms2.tex	2010-03-15 14:56:31 UTC (rev 62)
@@ -128,10 +128,10 @@
 \newcommand{\addc}[1]{{\textcolor[named]{Black}{#1}}}  % clarifications
 \newcommand{\addaa}[1]{{\textcolor[named]{Black}{#1}}}  % additions
 \newcommand{\addca}[1]{{\textcolor[named]{Black}{#1}}}  % clarifications
-\newcommand{\addab}[1]{{\textcolor[named]{Red}{#1}}}  % additions
-\newcommand{\addcb}[1]{{\textcolor[named]{MidnightBlue}{#1}}}  % clarifications
+\newcommand{\addab}[1]{{\textcolor[named]{Black}{#1}}}  % additions
+\newcommand{\addcb}[1]{{\textcolor[named]{Black}{#1}}}  % clarifications
+\newcommand{\addcc}[1]{{\textcolor[named]{Red}{#1  10/03/2010.}}}
 
-
 \hyphenation{mini-step mini-steps reci-pro-city}
 \setlength{\parskip}{1.5ex plus0.5ex minus0.5ex}
 \setlength{\parindent}{0ex}
@@ -1752,6 +1752,8 @@
 in Siena 3 it is determined adaptively with a maximum value of 40.
 
 First a rough description is given.
+\medskip
+
 First a vector of coordinates $(w_0,i_0,j_0,r_0,d_0)$ is selected.
 The output variable \textit{misdat} indicates whether option
 $(w_0,i_0,j_0,r_0)$ is missing (see function \textit{mis} defined above).
@@ -1765,10 +1767,12 @@
 This requires the following:\\
 $\blacktriangleright \Big((w_0 = N) \Rightarrow i_0 \neq j_0\Big)$ and
      $\Big((w_0 = B) \Rightarrow d_0 \neq 0\Big)$;\\
-$\blacktriangleright $ there is at least one ministep between $\ms_a$ and $\ms_b$
-(i.e., $\textit{length}(\ms_a,\ms_b) \geq 3$);\\
-$\blacktriangleright $ there are no ministeps of  the type  $(w,i,j,r)$ in the
+%$\blacktriangleright $ there is at least one ministep between $\ms_a$ and $\ms_b$
+%(i.e., $\textit{length}(\ms_a,\ms_b) \geq 3$);\\ omitted 10-03-10
+     \addcc{Condition omitted }\\
+$\blacktriangleright $ there are no ministeps of  the type  $(w_0,i_0,j_0,r_0)$ in the
 interval  $[\ms_a, \ms_b.\nnm{pred}]$.
+\     \addcc{ changed }
 
 The number $c$ is $\min\{c_0, \textit{length}(\ms_a,\ms_b)-1\}$.
 If $c \geq 2$,
@@ -1791,7 +1795,8 @@
 Part \emph{A}:
 \begin{enumerate}
 \item Repeat $\ms_a \leftarrow $ \textit{RandomElement} \ until
-       $\ms_a.\nnm{succ} < \nnm{last}$.
+%       $\ms_a.\nnm{succ} < \nnm{last}$.  changed 10-03-10
+       $\ms_a < \nnm{last}$. \ \addcc{Changed }
 \item $y \leftarrow  \textit{StateBefore}(\ms_a) $
 \item \textit{StepProb1}$(y, *;  rr, lospr*)$ \\
        With probabilities defined by $\exp(lospr*)$ choose \textit{OptionSet} $(w_0,i_0,r_0)$.\\
@@ -1818,15 +1823,19 @@
                 or the \nnm{last} ministep
                 if there is no ministep after $\ms_a$ of this \textit{Option}.\\
                 This can be determined using the pointer \nnm{succOption}.
-             \item    $\textit{ChoiceLength} \leftarrow \textit{length}(\ms_a, \ms_d ) - 2$
-             \item             If $\textit{ChoiceLength} \leq 0$ then exit.
+             \item    $\textit{ChoiceLength} \leftarrow \textit{length}(\ms_a, \ms_d ) - 1$
+                \   \addcc{Changed }
+                \\ \addcc{Condition (iii) omitted }
+  %           \item             If $\textit{ChoiceLength} \leq 0$ then exit.
              \end{enumerate}
       \item If \textit{misdat}:\\
             $\textit{ChoiceLength} \leftarrow 1 $
       \end{enumerate}
 \item \begin{enumerate}
        \item  If (not \textit{misdat}):\\
-              Let $\ms_b$ a random ministep in the interval $[\ms_a.\nnm{succ}^2, \ms_d ]$.\\
+%              Let $\ms_b$ a random ministep in the interval $[\ms_a.\nnm{succ}^2, \ms_d ]$.\\
+               Let $\ms_b$ a random ministep in the interval $[\ms_a.\nnm{succ}, \ms_d ]$.
+               \  \addcc{Changed } \\
               Note that the number of choices here is \textit{ChoiceLength}.
        \item  If \textit{misdat}:\\
               $\ms_b \leftarrow \nnm{last}$
@@ -1849,8 +1858,10 @@
 \end{enumerate}
 The proposal probability is
 \[
-  \frac{\exp{(lospr + lcpr)}}{(\textit{TotNumber}-2)\ttimes \textit{ChoiceLength} \ttimes (c!)}
+%  \frac{\exp{(lospr + lcpr)}}{(\textit{TotNumber}-2)\ttimes \textit{ChoiceLength} \ttimes (c!)}
+  \frac{\exp{(lospr + lcpr)}}{(\textit{TotNumber}-1)\ttimes \textit{ChoiceLength} \ttimes (c!)}
 \]
+ \addcc{Changed } \\
 This is used in \textit{pra} below.
 We also need the probability of the inverse proposal
 (except for the factor $c! $ which cancels),
@@ -1956,9 +1967,11 @@
      \textit{pra}  \leftarrow &
         \textit{KappaFactor} \ttimes \exp(\textit{sumlprob\_new} - \textit{sumlprob}) \nonumber \\
   \nonumber
-     & \ttimes  \frac{prdpr \ttimes pr_1 \ttimes (\textit{TotNumber}-2)\ttimes \textit{ChoiceLength} }
+%     & \ttimes  \frac{prdpr \ttimes pr_1 \ttimes (\textit{TotNumber}-2)\ttimes \textit{ChoiceLength} }
+      & \ttimes  \frac{prdpr \ttimes pr_1 \ttimes (\textit{TotNumber}-1)\ttimes \textit{ChoiceLength} }
                      {pripr \ttimes \exp{(lospr + lcpr)}}
     \end{align}
+ \addcc{Changed } \\
     if (\textit{pra} $ > 1$), then  \textit{pra} $ \leftarrow 1 $.
 \end{enumerate}
 Part \emph{C}:
@@ -2177,9 +2190,12 @@
 \item \begin{align}
      \textit{pra}  \leftarrow \ &
         \textit{KappaFactor} \ttimes \exp(\textit{sumlprob\_new} - \textit{sumlprob}) \nonumber \\
-          & \ \ttimes  \frac{pripr \ttimes \exp(lpr_0)}{prdpr \ttimes pr_1 \ttimes \textit{TotNumber}
+%          & \ \ttimes  \frac{pripr \ttimes \exp(lpr_0)}{prdpr \ttimes pr_1 \ttimes \textit{TotNumber}
+           & \ \ttimes  \frac{pripr \ttimes \exp(lpr_0)}{prdpr \ttimes pr_1 \ttimes
+                                                  \textit{(TotNumber+1)}
                                            \ttimes \textit{ChoiceLength} }
     \end{align}
+ \addcc{Changed } \\
     if (\textit{pra} $ > 1$), then  \textit{pra} $ \leftarrow 1 $.
 \end{enumerate}
 Part \emph{C}:
@@ -2196,9 +2212,8 @@
 \end{enumerate}
 
 \subsection{Randomize Missings}
+%\addab{This section is new in the version of February 8, 2010.}
 
-\addab{This section is new in the version of February 8, 2010.}
-
 The function
 \textit{MH\_RandomMis}
 $( \textsf{output}\  w_0,i_0,j_0,r_0,\, \textit{pr})$

Modified: pkg/RSienaTest/inst/examples/baerveldt3.csv
===================================================================
--- pkg/RSienaTest/inst/examples/baerveldt3.csv	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/inst/examples/baerveldt3.csv	2010-03-15 14:56:31 UTC (rev 62)
@@ -1,6 +1,6 @@
-"Group","Name","Filename","Format","Period","ActorSet","Type","Selected","MissingValues","NonZeroCode"
-"data1","friendship","N34_3.DAT","matrix","1","Actors","network","Yes","9","1"
-"data1","friendship","HN34_3.DAT","matrix","2","Actors","network","Yes","9","1"
-"data1","delinquency","cbc3.dat","matrix","1","Actors","changing covariate","Yes","9","1"
-"data1","gender cri impsf","CBE3.DAT","matrix","1","Actors","constant covariate","Yes","9","1"
-"data1","coethnic","cbe3.sim","matrix","1","Actors","constant dyadic covariate","Yes","9","1"
+Group,Name,Filename,Format,Period,ActorSet,Type,Selected,MissingValues,NonZeroCode
+data1,friendship,N34_3.DAT,matrix,1,Actors,network,Yes,9,1
+data1,friendship,HN34_3.DAT,matrix,2,Actors,network,Yes,9,1
+data1,delinquency,cbc3.dat,matrix,1,Actors,constant covariate,Yes,9,1
+data1,gender cri impsf,CBE3.DAT,matrix,1,Actors,constant covariate,Yes,9,1
+data1,coethnic,cbe3.sim,matrix,1,Actors,constant dyadic covariate,Yes,9,1

Modified: pkg/RSienaTest/inst/examples/baerveldt4.csv
===================================================================
--- pkg/RSienaTest/inst/examples/baerveldt4.csv	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/inst/examples/baerveldt4.csv	2010-03-15 14:56:31 UTC (rev 62)
@@ -1,6 +1,6 @@
-"Group","Name","Filename","Format","Period","ActorSet","Type","Selected","MissingValues","NonZeroCode"
-"data1","friendship","N34_4.DAT","matrix","1","Actors","network","Yes","9","1"
-"data1","friendship","HN34_4.DAT","matrix","2","Actors","network","Yes","9","1"
-"data1","delin1 delin2","cbc4.dat","matrix","1","Actors","changing covariate","Yes","9","1"
-"data1","gender cri impsf","CBE4.DAT","matrix","1","Actors","constant covariate","Yes","9","1"
-"data1","coethnic","cbe4.sim","matrix","1","Actors","constant dyadic covariate","Yes","9","1"
+Group,Name,Filename,Format,Period,ActorSet,Type,Selected,MissingValues,NonZeroCode
+data1,friendship,N34_4.DAT,matrix,1,Actors,network,Yes,9,1
+data1,friendship,HN34_4.DAT,matrix,2,Actors,network,Yes,9,1
+data1,delin1 delin2,cbc4.dat,matrix,1,Actors,constant covariate,Yes,9,1
+data1,gender cri impsf,CBE4.DAT,matrix,1,Actors,constant covariate,Yes,9,1
+data1,coethnic,cbe4.sim,matrix,1,Actors,constant dyadic covariate,Yes,9,1

Modified: pkg/RSienaTest/src/data/BehaviorLongitudinalData.cpp
===================================================================
--- pkg/RSienaTest/src/data/BehaviorLongitudinalData.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/data/BehaviorLongitudinalData.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -34,16 +34,19 @@
 {
 	this->lvalues = new int * [observationCount];
 	this->lmissing = new bool * [observationCount];
+	this->lstructural = new bool * [observationCount];
 
 	for (int i = 0; i < observationCount; i++)
 	{
 		this->lvalues[i] = new int[pActorSet->n()];
 		this->lmissing[i] = new bool[pActorSet->n()];
+		this->lstructural[i] = new bool[pActorSet->n()];
 
 		for (int actor = 0; actor < pActorSet->n(); actor++)
 		{
 			this->lvalues[i][actor] = 0;
 			this->lmissing[i][actor] = false;
+			this->lstructural[i][actor] = false;
 		}
 	}
 }
@@ -58,12 +61,15 @@
 	{
 		delete[] this->lvalues[i];
 		delete[] this->lmissing[i];
+		delete[] this->lstructural[i];
 	}
 
 	delete[] this->lvalues;
 	delete[] this->lmissing;
+	delete[] this->lstructural;
 	this->lvalues = 0;
 	this->lmissing = 0;
+	this->lstructural = 0;
 }
 
 
@@ -120,6 +126,28 @@
 }
 
 
+/**
+ * Returns if the value of the behavioral variable is structurally
+ * determined for the given actor at the specified observation.
+ */
+bool BehaviorLongitudinalData::structural(int observation, int actor) const
+{
+	return this->lstructural[observation][actor];
+}
+
+
+/**
+ * Stores if the value of the behavioral variable is structurally
+ * determined for the given actor at the specified observation.
+ */
+void BehaviorLongitudinalData::structural(int observation,
+	int actor,
+	bool structural)
+{
+	this->lstructural[observation][actor] = structural;
+}
+
+
 // ----------------------------------------------------------------------------
 // Section: Various statistics on the observed values
 // ----------------------------------------------------------------------------

Modified: pkg/RSienaTest/src/data/BehaviorLongitudinalData.h
===================================================================
--- pkg/RSienaTest/src/data/BehaviorLongitudinalData.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/data/BehaviorLongitudinalData.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -32,6 +32,8 @@
 	void value(int observation, int actor, int value);
 	bool missing(int observation, int actor) const;
 	void missing(int observation, int actor, bool missing);
+	bool structural(int observation, int actor) const;
+	void structural(int observation, int actor, bool flag);
 	const int * values(int observation) const;
 
 	int min() const;
@@ -50,6 +52,9 @@
 	// Missingness indicators
 	bool ** lmissing;
 
+	// Structural value indicators
+	bool ** lstructural;
+
 	// The smallest non-missing value
 	int lmin;
 

Modified: pkg/RSienaTest/src/model/EpochSimulation.cpp
===================================================================
--- pkg/RSienaTest/src/model/EpochSimulation.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/EpochSimulation.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -28,8 +28,6 @@
 #include "model/State.h"
 #include "model/effects/Effect.h"
 #include "model/tables/Cache.h"
-#include "model/ml/Chain.h"
-#include "model/ml/MiniStep.h"
 #include "model/filters/AtLeastOneFilter.h"
 #include "model/filters/DisjointFilter.h"
 #include "model/filters/HigherFilter.h"
@@ -717,59 +715,12 @@
 }
 
 
-// ----------------------------------------------------------------------------
-// Section: Maximum likelihood related methods
-// ----------------------------------------------------------------------------
-
 /**
- * Updates the probabilities for a range of ministeps of the given chain
- * (including the end-points of the range).
+ * Returns the total rate over all dependent variables.
  */
-void EpochSimulation::updateProbabilities(Chain * pChain,
-	MiniStep * pFirstMiniStep,
-	MiniStep * pLastMiniStep)
+double EpochSimulation::totalRate() const
 {
-	// Initialize the variables as of the beginning of the period
-    this->initialize(pChain->period());
-
-    // Apply the ministeps before the first ministep of the required range
-    // to derive the correct state before that ministep.
-
-    MiniStep * pMiniStep = pChain->pFirst()->pNext();
-
-    while (pMiniStep != pFirstMiniStep)
-    {
-    	DependentVariable * pVariable =
-    		this->lvariableMap[pMiniStep->variableName()];
-    	pMiniStep->makeChange(pVariable);
-
-    	pMiniStep = pMiniStep->pNext();
-    }
-
-    bool done = false;
-
-    while (!done)
-    {
-    	DependentVariable * pVariable =
-    		this->lvariableMap[pMiniStep->variableName()];
-    	this->calculateRates();
-    	double rate = pVariable->rate(pMiniStep->ego());
-    	double probability = pVariable->probability(pMiniStep);
-    	double reciprocalTotalRate = 1 / this->ltotalRate;
-
-    	pMiniStep->reciprocalRate(reciprocalTotalRate);
-    	pMiniStep->logProbability(log(probability * rate * reciprocalTotalRate));
-    	pMiniStep->makeChange(pVariable);
-
-    	if (pMiniStep == pLastMiniStep)
-    	{
-    		done = true;
-    	}
-    	else
-    	{
-    		pMiniStep = pMiniStep->pNext();
-    	}
-    }
+	return this->ltotalRate;
 }
 
 }

Modified: pkg/RSienaTest/src/model/EpochSimulation.h
===================================================================
--- pkg/RSienaTest/src/model/EpochSimulation.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/EpochSimulation.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -33,8 +33,6 @@
 class SimulationActorSet;
 class State;
 class Cache;
-class Chain;
-class MiniStep;
 
 
 // ----------------------------------------------------------------------------
@@ -56,12 +54,6 @@
     // Method of moments related
     void runEpoch(int period);
 
-    // Maximum likelihood related
-
-    void updateProbabilities(Chain * pChain,
-    	MiniStep * pFirstMiniStep,
-    	MiniStep * pLastMiniStep);
-
     // Accessors
 
     const Data * pData() const;
@@ -79,15 +71,21 @@
     double score(const EffectInfo * pEffect) const;
     void score(const EffectInfo * pEffect, double value);
 
+protected:
+    void calculateRates();
+    double totalRate() const;
+    DependentVariable * chooseVariable() const;
+    int chooseActor(const DependentVariable * pVariable) const;
+
+    // The dependent variable for look-ups by name
+    map<string, DependentVariable *> lvariableMap;
+
 private:
     void runStep();
-    void calculateRates();
     void drawTimeIncrement();
     bool reachedCompositionChange() const;
     void makeNextCompositionChange();
 	void setLeaversBack();
-    DependentVariable * chooseVariable() const;
-    int chooseActor(const DependentVariable * pVariable) const;
     void accumulateRateScores(double tau,
     	const DependentVariable * pSelectedVariable = 0,
     	int selectedActor = 0);
@@ -107,9 +105,6 @@
     // A vector of dependent variables with their current values
     vector<DependentVariable *> lvariables;
 
-    // The dependent variable for look-ups by name
-    map<string, DependentVariable *> lvariableMap;
-
     // The current period to be simulated
     int lperiod;
 

Modified: pkg/RSienaTest/src/model/filters/AtLeastOneFilter.cpp
===================================================================
--- pkg/RSienaTest/src/model/filters/AtLeastOneFilter.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/AtLeastOneFilter.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -13,6 +13,7 @@
 #include "network/IncidentTieIterator.h"
 #include "network/Network.h"
 #include "model/variables/NetworkVariable.h"
+#include "model/ml/NetworkChange.h"
 
 namespace siena
 {
@@ -57,4 +58,22 @@
 	}
 }
 
+
+/**
+ * Returns if applying the given ministep on the current state of the
+ * network would be valid with respect to this filter.
+ */
+bool AtLeastOneFilter::validMiniStep(const NetworkChange * pMiniStep)
+{
+	const Network * pNetwork1 = this->pVariable()->pNetwork();
+	const Network * pNetwork2 = this->pOtherVariable()->pNetwork();
+
+	// We shouldn't withdraw a tie if it is not present in the other network.
+
+	int i = pMiniStep->ego();
+	int j = pMiniStep->alter();
+
+	return !pNetwork1->tieValue(i, j) || pNetwork2->tieValue(i, j);
 }
+
+}

Modified: pkg/RSienaTest/src/model/filters/AtLeastOneFilter.h
===================================================================
--- pkg/RSienaTest/src/model/filters/AtLeastOneFilter.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/AtLeastOneFilter.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -28,6 +28,7 @@
 		const NetworkVariable * pOtherVariable);
 
 	virtual void filterPermittedChanges(int ego, bool * permitted);
+	virtual bool validMiniStep(const NetworkChange * pMiniStep);
 };
 
 }

Modified: pkg/RSienaTest/src/model/filters/DisjointFilter.cpp
===================================================================
--- pkg/RSienaTest/src/model/filters/DisjointFilter.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/DisjointFilter.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -13,6 +13,7 @@
 #include "network/IncidentTieIterator.h"
 #include "network/Network.h"
 #include "model/variables/NetworkVariable.h"
+#include "model/ml/NetworkChange.h"
 
 namespace siena
 {
@@ -57,4 +58,22 @@
 	}
 }
 
+
+/**
+ * Returns if applying the given ministep on the current state of the
+ * network would be valid with respect to this filter.
+ */
+bool DisjointFilter::validMiniStep(const NetworkChange * pMiniStep)
+{
+	const Network * pNetwork1 = this->pVariable()->pNetwork();
+	const Network * pNetwork2 = this->pOtherVariable()->pNetwork();
+
+	// We shouldn't introduce a tie if it is present in the other network.
+
+	int i = pMiniStep->ego();
+	int j = pMiniStep->alter();
+
+	return pNetwork1->tieValue(i, j) || !pNetwork2->tieValue(i, j);
 }
+
+}

Modified: pkg/RSienaTest/src/model/filters/DisjointFilter.h
===================================================================
--- pkg/RSienaTest/src/model/filters/DisjointFilter.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/DisjointFilter.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -28,6 +28,7 @@
 		const NetworkVariable * pOtherVariable);
 
 	virtual void filterPermittedChanges(int ego, bool * permitted);
+	virtual bool validMiniStep(const NetworkChange * pMiniStep);
 };
 
 }

Modified: pkg/RSienaTest/src/model/filters/HigherFilter.cpp
===================================================================
--- pkg/RSienaTest/src/model/filters/HigherFilter.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/HigherFilter.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -13,6 +13,7 @@
 #include "network/IncidentTieIterator.h"
 #include "network/Network.h"
 #include "model/variables/NetworkVariable.h"
+#include "model/ml/NetworkChange.h"
 
 namespace siena
 {
@@ -57,4 +58,22 @@
 	}
 }
 
+
+/**
+ * Returns if applying the given ministep on the current state of the
+ * network would be valid with respect to this filter.
+ */
+bool HigherFilter::validMiniStep(const NetworkChange * pMiniStep)
+{
+	const Network * pNetwork1 = this->pVariable()->pNetwork();
+	const Network * pNetwork2 = this->pOtherVariable()->pNetwork();
+
+	// We shouldn't withdraw a tie if it is present in the other network.
+
+	int i = pMiniStep->ego();
+	int j = pMiniStep->alter();
+
+	return !pNetwork1->tieValue(i, j) || !pNetwork2->tieValue(i, j);
 }
+
+}

Modified: pkg/RSienaTest/src/model/filters/HigherFilter.h
===================================================================
--- pkg/RSienaTest/src/model/filters/HigherFilter.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/HigherFilter.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -27,6 +27,7 @@
 		const NetworkVariable * pOtherVariable);
 
 	virtual void filterPermittedChanges(int ego, bool * permitted);
+	virtual bool validMiniStep(const NetworkChange * pMiniStep);
 };
 
 }

Modified: pkg/RSienaTest/src/model/filters/LowerFilter.cpp
===================================================================
--- pkg/RSienaTest/src/model/filters/LowerFilter.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/LowerFilter.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -12,6 +12,7 @@
 #include "network/IncidentTieIterator.h"
 #include "network/Network.h"
 #include "model/variables/NetworkVariable.h"
+#include "model/ml/NetworkChange.h"
 
 namespace siena
 {
@@ -62,4 +63,22 @@
 	}
 }
 
+
+/**
+ * Returns if applying the given ministep on the current state of the
+ * network would be valid with respect to this filter.
+ */
+bool LowerFilter::validMiniStep(const NetworkChange * pMiniStep)
+{
+	const Network * pNetwork1 = this->pVariable()->pNetwork();
+	const Network * pNetwork2 = this->pOtherVariable()->pNetwork();
+
+	// We shouldn't introduce a tie if it is not present in the other network.
+
+	int i = pMiniStep->ego();
+	int j = pMiniStep->alter();
+
+	return pNetwork1->tieValue(i, j) || pNetwork2->tieValue(i, j);
 }
+
+}

Modified: pkg/RSienaTest/src/model/filters/LowerFilter.h
===================================================================
--- pkg/RSienaTest/src/model/filters/LowerFilter.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/LowerFilter.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -27,6 +27,7 @@
 		const NetworkVariable * pOtherVariable);
 
 	virtual void filterPermittedChanges(int ego, bool * permitted);
+	virtual bool validMiniStep(const NetworkChange * pMiniStep);
 };
 
 }

Modified: pkg/RSienaTest/src/model/filters/PermittedChangeFilter.h
===================================================================
--- pkg/RSienaTest/src/model/filters/PermittedChangeFilter.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/filters/PermittedChangeFilter.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -20,6 +20,7 @@
 // ----------------------------------------------------------------------------
 
 class NetworkVariable;
+class NetworkChange;
 
 
 // ----------------------------------------------------------------------------
@@ -41,6 +42,12 @@
 	 */
 	virtual void filterPermittedChanges(int ego, bool * permitted) = 0;
 
+	/**
+	 * Returns if applying the given ministep on the current state of the
+	 * network would be valid with respect to this filter.
+	 */
+	virtual bool validMiniStep(const NetworkChange * pMiniStep) = 0;
+
 protected:
 	inline const NetworkVariable * pVariable() const;
 

Modified: pkg/RSienaTest/src/model/ml/BehaviorChange.cpp
===================================================================
--- pkg/RSienaTest/src/model/ml/BehaviorChange.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/ml/BehaviorChange.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -53,4 +53,14 @@
 	}
 }
 
+
+/**
+ * Returns if this ministep is diagonal, namely, it does not change
+ * the dependent variables.
+ */
+bool BehaviorChange::diagonal() const
+{
+	return this->difference() == 0;
 }
+
+}

Modified: pkg/RSienaTest/src/model/ml/BehaviorChange.h
===================================================================
--- pkg/RSienaTest/src/model/ml/BehaviorChange.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/ml/BehaviorChange.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -29,6 +29,7 @@
 	virtual ~BehaviorChange();
 
 	virtual void makeChange(DependentVariable * pVariable);
+	virtual bool diagonal() const;
 };
 
 }

Modified: pkg/RSienaTest/src/model/ml/Chain.cpp
===================================================================
--- pkg/RSienaTest/src/model/ml/Chain.cpp	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/ml/Chain.cpp	2010-03-15 14:56:31 UTC (rev 62)
@@ -14,6 +14,7 @@
 #include "model/ml/MiniStep.h"
 #include "model/ml/BehaviorChange.h"
 #include "model/ml/NetworkChange.h"
+#include "model/ml/MLSimulation.h"
 #include "network/Network.h"
 #include "network/IncidentTieIterator.h"
 #include "data/Data.h"
@@ -26,7 +27,7 @@
 /**
  * Creates an empty chain.
  */
-Chain::Chain()
+Chain::Chain(Data * pData)
 {
 	this->lpFirst = new MiniStep(0, 0, 0);
 	this->lpLast = new MiniStep(0, 0, 0);
@@ -34,7 +35,14 @@
 	this->lpFirst->pNext(this->lpLast);
 	this->lpLast->pPrevious(this->lpFirst);
 
+	this->lpData = pData;
 	this->lperiod = -1;
+
+	this->lminiSteps.push_back(this->lpLast);
+	this->lpLast->index(0);
+
+	this->lmu = 0;
+	this->lsigma2 = 0;
 }
 
 
@@ -43,12 +51,16 @@
  */
 Chain::~Chain()
 {
-	this->emptyChain();
+	this->clear();
 
 	delete this->lpFirst;
 	delete this->lpLast;
 	this->lpFirst = 0;
 	this->lpLast = 0;
+	this->lpData = 0;
+
+	this->lminiSteps.clear();
+	this->ldiagonalMiniSteps.clear();
 }
 
 
@@ -56,7 +68,7 @@
  * Removes all ministeps from this chain except for the dummy ministeps
  * at the ends of the chain.
  */
-void Chain::emptyChain()
+void Chain::clear()
 {
 	MiniStep * pMiniStep = this->lpFirst->pNext();
 
@@ -69,6 +81,15 @@
 
 	this->lpFirst->pNext(this->lpLast);
 	this->lpLast->pPrevious(this->lpFirst);
+
+	this->lminiSteps.clear();
+	this->lminiSteps.push_back(this->lpLast);
+	this->lpLast->index(0);
+
+	this->ldiagonalMiniSteps.clear();
+
+	this->lmu = 0;
+	this->lsigma2 = 0;
 }
 
 
@@ -79,11 +100,26 @@
 {
 	MiniStep * pPreviousMiniStep = pExistingMiniStep->pPrevious();
 
+	pNewMiniStep->pChain(this);
+
 	pNewMiniStep->pPrevious(pPreviousMiniStep);
 	pPreviousMiniStep->pNext(pNewMiniStep);
 
 	pNewMiniStep->pNext(pExistingMiniStep);
 	pExistingMiniStep->pPrevious(pNewMiniStep);
+
+	if (pNewMiniStep->diagonal())
+	{
+		this->ldiagonalMiniSteps.push_back(pNewMiniStep);
+		pNewMiniStep->diagonalIndex(this->ldiagonalMiniSteps.size() - 1);
+	}
+
+	this->lminiSteps.push_back(pNewMiniStep);
+	pNewMiniStep->index(this->lminiSteps.size() - 1);
+
+	double rr = pNewMiniStep->reciprocalRate();
+	this->lmu += rr;
+	this->lsigma2 += rr * rr;
 }
 
 
@@ -96,6 +132,28 @@
 	pMiniStep->pNext()->pPrevious(pMiniStep->pPrevious());
 	pMiniStep->pNext(0);
 	pMiniStep->pPrevious(0);
+
+	if (pMiniStep->diagonal())
+	{
+		MiniStep * pLastMiniStep =
+			this->ldiagonalMiniSteps[this->ldiagonalMiniSteps.size() - 1];
+		this->ldiagonalMiniSteps[pMiniStep->diagonalIndex()] = pLastMiniStep;
+		pLastMiniStep->diagonalIndex(pMiniStep->diagonalIndex());
+		this->ldiagonalMiniSteps.pop_back();
+		pMiniStep->diagonalIndex(-1);
+	}
+
+	MiniStep * pLastMiniStep = this->lminiSteps[this->lminiSteps.size() - 1];
+	this->lminiSteps[pMiniStep->index()] = pLastMiniStep;
+	pLastMiniStep->index(pMiniStep->index());
+	this->lminiSteps.pop_back();
+	pMiniStep->index(-1);
+
+	double rr = pMiniStep->reciprocalRate();
+	this->lmu -= rr;
+	this->lsigma2 -= rr * rr;
+
+	pMiniStep->pChain(0);
 }
 
 
@@ -104,17 +162,20 @@
  * given data object for the given period. The chain is simple in the sense
  * that no two ministeps cancel each other out.
  */
-void Chain::connect(Data * pData, int period)
+void Chain::connect(int period)
 {
-	this->emptyChain();
+	this->clear();
 	this->lperiod = period;
 	vector<MiniStep *> miniSteps;
 
 	// Create the required ministeps
 
-	for (unsigned i = 0; i < pData->rDependentVariableData().size(); i++)
+	for (unsigned variableIndex = 0;
+		variableIndex < this->lpData->rDependentVariableData().size();
+		variableIndex++)
 	{
-		LongitudinalData * pVariableData = pData->rDependentVariableData()[i];
+		LongitudinalData * pVariableData =
+			this->lpData->rDependentVariableData()[variableIndex];
 		NetworkLongitudinalData * pNetworkData =
 			dynamic_cast<NetworkLongitudinalData *>(pVariableData);
 		BehaviorLongitudinalData * pBehaviorData =
@@ -205,6 +266,22 @@
 
 
 /**
+ * Performs the necessary updates when the reciprocal rate of the
+ * given ministep changes to the given value.
+ */
+void Chain::onReciprocalRateChange(const MiniStep * pMiniStep, double newValue)
+{
+	double oldValue = pMiniStep->reciprocalRate();
+
+	this->lmu -= oldValue;
+	this->lsigma2 -= oldValue * oldValue;
+
+	this->lmu += newValue;
+	this->lsigma2 += newValue * newValue;
+}
+
+
+/**
  * Returns the period whose end observations are connected by this chain.
  */
 int Chain::period() const
@@ -230,4 +307,63 @@
 	return this->lpLast;
 }
 
+
+/**
+ * Returns the number of ministeps of this chain excluding the first,
+ * but including the last dummy ministep.
+ */
+int Chain::ministepCount() const
+{
+	return this->lminiSteps.size();
 }
+
+
+/**
+ * Returns the number of diagonal ministeps in this chain.
+ */
+int Chain::diagonalMinistepCount() const
+{
+	return this->ldiagonalMiniSteps.size();
+}
+
+
+/**
+ * Returns the sum of reciprocal rates over all non-dummy ministeps.
+ */
+double Chain::mu() const
+{
+	return this->lmu;
+}
+
+
+/**
+ * Returns the sum of squared reciprocal rates over all non-dummy ministeps.
+ */
+double Chain::sigma2() const
+{
+	return this->lsigma2;
+}
+
+
+// ----------------------------------------------------------------------------
+// Section: Random draws
+// ----------------------------------------------------------------------------
+
+/**
+ * Returns a random ministep excluding the first dummy ministep.
+ */
+MiniStep * Chain::randomMiniStep() const
+{
+	return this->lminiSteps[nextInt(this->lminiSteps.size())];
+}
+
+
+/**
+ * Returns a random diagonal ministep.
+ */
+MiniStep * Chain::randomDiagonalMiniStep() const
+{
+	return this->ldiagonalMiniSteps[nextInt(this->ldiagonalMiniSteps.size())];
+}
+
+}

Modified: pkg/RSienaTest/src/model/ml/Chain.h
===================================================================
--- pkg/RSienaTest/src/model/ml/Chain.h	2010-02-27 18:18:39 UTC (rev 61)
+++ pkg/RSienaTest/src/model/ml/Chain.h	2010-03-15 14:56:31 UTC (rev 62)
@@ -8,10 +8,13 @@
  * Description: This file contains the definition of the Chain class.
  *****************************************************************************/
 
-
 #ifndef CHAIN_H_
 #define CHAIN_H_
 
+#include <vector>
+
+using namespace std;
+
 namespace siena
 {
 
@@ -35,18 +38,29 @@
 class Chain
 {
 public:
-	Chain();
+	Chain(Data * pData);
 	virtual ~Chain();
 
-	void emptyChain();
+	void clear();
 	void insertBefore(MiniStep * pNewMiniStep, MiniStep * pExistingMiniStep);
 	void remove(MiniStep * pMiniStep);
-	void connect(Data * pData, int period);
+	void connect(int period);
 
+	void onReciprocalRateChange(const MiniStep * pMiniStep, double newValue);
+
 	int period() const;
 	MiniStep * pFirst() const;
 	MiniStep * pLast() const;
+	int ministepCount() const;
+	int diagonalMinistepCount() const;
+	double mu() const;
+	double sigma2() const;
 
+	// Random draws
+
+	MiniStep * randomMiniStep() const;
+	MiniStep * randomDiagonalMiniStep() const;
+
 private:
 	// A dummy first ministep in the chain
 	MiniStep * lpFirst;
@@ -54,8 +68,25 @@
 	// A dummy last ministep in the chain
 	MiniStep * lpLast;
 
+	// The underlying observed data
+	Data * lpData;
+
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rsiena -r 62


More information about the Rsiena-commits mailing list