[Rsiena-commits] r172 - in pkg: RSiena RSienaTest RSienaTest/data RSienaTest/src RSienaTest/src/model RSienaTest/src/model/effects RSienaTest/src/model/variables
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Sep 19 19:10:56 CEST 2011
Author: cgreenan
Date: 2011-09-19 19:10:56 +0200 (Mon, 19 Sep 2011)
New Revision: 172
Added:
pkg/RSienaTest/src/model/effects/DiffusionRateEffect.cpp
pkg/RSienaTest/src/model/effects/DiffusionRateEffect.h
pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.cpp
pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.h
Modified:
pkg/RSiena/changeLog
pkg/RSienaTest/changeLog
pkg/RSienaTest/data/allEffects.csv
pkg/RSienaTest/src/model/StatisticCalculator.cpp
pkg/RSienaTest/src/model/variables/DependentVariable.cpp
pkg/RSienaTest/src/model/variables/DependentVariable.h
pkg/RSienaTest/src/siena07internals.cpp
Log:
Modified: pkg/RSiena/changeLog
===================================================================
--- pkg/RSiena/changeLog 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSiena/changeLog 2011-09-19 17:10:56 UTC (rev 172)
@@ -1,3 +1,16 @@
+2011-09-19 R-forge revision 172 RSienaTest only
+
+ * src/model/effects/DiffusionRateEffect.cpp,
+ src/model/effects/DiffusionRateEffect.h,
+ src/model/variables/DiffusionEffectValueTable.cpp,
+ src/model/variable/DiffusionEffectValueTable.h: files created to
+ implement diffusion rate effects.
+ * data/allEffects.csv, src/siena07internals.cpp,
+ src/model/StatisticCalculator.cpp,
+ src/model/variables/DependentVariable.cpp,
+ src/model/variables/DependentVariable.h: diffusion rate effect
+ averageExposure added.
+
2011-09-07 R-forge revision 171
* R/globals.r, R/siena08.r, man/siena08.Rd: fix bugs in siena08 which was
Modified: pkg/RSienaTest/changeLog
===================================================================
--- pkg/RSienaTest/changeLog 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSienaTest/changeLog 2011-09-19 17:10:56 UTC (rev 172)
@@ -1,3 +1,16 @@
+2011-09-19 R-forge revision 172 RSienaTest only
+
+ * src/model/effects/DiffusionRateEffect.cpp,
+ src/model/effects/DiffusionRateEffect.h,
+ src/model/variables/DiffusionEffectValueTable.cpp,
+ src/model/variable/DiffusionEffectValueTable.h: files created to
+ implement diffusion rate effects.
+ * data/allEffects.csv, src/siena07internals.cpp,
+ src/model/StatisticCalculator.cpp,
+ src/model/variables/DependentVariable.cpp,
+ src/model/variables/DependentVariable.h: diffusion rate effect
+ averageExposure added.
+
2011-09-07 R-forge revision 171
* R/globals.r, R/siena08.r, man/siena08.Rd: fix bugs in siena08 which was
Modified: pkg/RSienaTest/data/allEffects.csv
===================================================================
--- pkg/RSienaTest/data/allEffects.csv 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSienaTest/data/allEffects.csv 2011-09-19 17:10:56 UTC (rev 172)
@@ -30,22 +30,23 @@
behaviorBipartiteObjective,behavior xxxxxx ave. sim. x popularity ego,beh. xxxxxx ave. sim. x indegrees ego,avSimPopEgo,TRUE,yyyyyy,,eval,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,objective,NA,NA,0,0,0,0,
behaviorObjective,behavior xxxxxx linear shape,beh. xxxxxx cent. sum,linear,TRUE,,,eval,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,objective,NA,NA,0,0,0,0,OK
behaviorObjective,behavior xxxxxx quadratic shape,beh. xxxxxx sum of cent. squares,quad,TRUE,,,eval,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,objective,NA,NA,0,0,0,0,
-behaviorRate,rate xxxxxx period 1,Amount of behavioral change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0
-behaviorRate,rate xxxxxx (period nnnnnn),Amount of behavioral change in period nnnnnn on xxxxxx,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0
-behaviorOneModeRate,outdegree effect on rate xxxxxx,Amount of change on xxxxxx x outdegree,outRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-behaviorOneModeRate,indegree effect on rate xxxxxx,Amount of change on xxxxxx x indegree,inRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-behaviorOneModeRate,reciprocated effect on rate xxxxxx,Amount of change on xxxxxx x reciprocity,recipRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-behaviorBipartiteRate,outdegree effect on rate xxxxxx,Amount of change on xxxxxx x outdegree,outRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-behaviorBipartiteRate,reciprocated effect on rate xxxxxx,Amount of change on xxxxxx x reciprocity,recipRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-nonSymmetricRate,basic rate parameter xxxxxx,Amount of network change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0
-nonSymmetricRate,constant xxxxxx rate (period nnnnnn),Amount of network change in period nnnnnn,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0
-nonSymmetricRate,outdegree effect on rate xxxxxx,Amount of change x outdegrees,outRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-nonSymmetricRate,indegree effect on rate xxxxxx,Amount of change x indegrees,inRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-nonSymmetricRate,reciprocity effect on rate xxxxxx,Amount of change x reciprocity,recipRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-nonSymmetricRate,effect 1/outdegree on rate xxxxxx,Amount of change x (1/outdegrees),outRateInv,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
-bipartiteRate,basic rate parameter xxxxxx,Amount of network change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0
-bipartiteRate,constant xxxxxx rate (period nnnnnn),Amount of network change in period nnnnnn,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0
-bipartiteRate,outdegree effect on rate xxxxxx,Amount of change x outdegrees,outRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0
+behaviorRate,rate xxxxxx period 1,Amount of behavioral change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0,
+behaviorRate,rate xxxxxx (period nnnnnn),Amount of behavioral change in period nnnnnn on xxxxxx,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0,
+behaviorOneModeRate,outdegree effect on rate xxxxxx,Amount of change on xxxxxx x outdegree,outRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+behaviorOneModeRate,indegree effect on rate xxxxxx,Amount of change on xxxxxx x indegree,inRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+behaviorOneModeRate,reciprocated effect on rate xxxxxx,Amount of change on xxxxxx x reciprocity,recipRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+behaviorOneModeRate,average exposure effect on rate xxxxxx,Amount of change on xxxxxx x average exposure,avExposure,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,diffusion,0,0,0,0,
+behaviorBipartiteRate,outdegree effect on rate xxxxxx,Amount of change on xxxxxx x outdegree,outRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+behaviorBipartiteRate,reciprocated effect on rate xxxxxx,Amount of change on xxxxxx x reciprocity,recipRate,NA,yyyyyy,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+nonSymmetricRate,basic rate parameter xxxxxx,Amount of network change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0,
+nonSymmetricRate,constant xxxxxx rate (period nnnnnn),Amount of network change in period nnnnnn,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0,
+nonSymmetricRate,outdegree effect on rate xxxxxx,Amount of change x outdegrees,outRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+nonSymmetricRate,indegree effect on rate xxxxxx,Amount of change x indegrees,inRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+nonSymmetricRate,reciprocity effect on rate xxxxxx,Amount of change x reciprocity,recipRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+nonSymmetricRate,effect 1/outdegree on rate xxxxxx,Amount of change x (1/outdegrees),outRateInv,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
+bipartiteRate,basic rate parameter xxxxxx,Amount of network change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0,
+bipartiteRate,constant xxxxxx rate (period nnnnnn),Amount of network change in period nnnnnn,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0,
+bipartiteRate,outdegree effect on rate xxxxxx,Amount of change x outdegrees,outRate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
bipartiteRate,effect 1/outdegree on rate xxxxxx,Amount of change x (1/outdegrees),outRateInv,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,NA,structural,0,0,0,0,
symmetricRate,basic rate parameter xxxxxx,Amount of network change,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,1,NA,0,0,0,0,
symmetricRate,constant xxxxxx rate (period nnnnnn),Amount of network change in period nnnnnn,Rate,NA,,,rate,FALSE,FALSE,FALSE,FALSE,FALSE,",",0,0,rate,nnnnnn,NA,0,0,0,0,
Modified: pkg/RSienaTest/src/model/StatisticCalculator.cpp
===================================================================
--- pkg/RSienaTest/src/model/StatisticCalculator.cpp 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSienaTest/src/model/StatisticCalculator.cpp 2011-09-19 17:10:56 UTC (rev 172)
@@ -35,6 +35,7 @@
#include "model/variables/NetworkVariable.h"
#include "model/variables/BehaviorVariable.h"
#include "model/tables/Cache.h"
+#include "network/IncidentTieIterator.h"
namespace siena
{
@@ -959,7 +960,7 @@
}
//}
}
- else
+ else if (rateType == "structural")
{
// We expect a structural (network-dependent) rate effect here.
@@ -1006,6 +1007,48 @@
this->lstatistics[pInfo] = statistic;
delete pStructural;
}
+ else if (rateType == "diffusion")
+ {
+ NetworkLongitudinalData *pNetworkData = this->lpData->
+ pNetworkData(interactionName);
+ Network * pStructural =
+ pNetworkData->pNetwork(this->lperiod)->clone();
+ this->subtractNetwork(pStructural,
+ pNetworkData->pMissingTieNetwork(this->lperiod));
+
+ double statistic = 0;
+ for (int i = 0; i < pBehaviorData->n(); i++)
+ {
+ if (effectName == "avExposure")
+ {
+ double totalAlterValue = 0;
+ double averageAlterValue = 0;
+ if (pStructural->outDegree(i)>0)
+ {
+ for (IncidentTieIterator iter = pStructural->outTies(i);
+ iter.valid();
+ iter.next())
+ {
+ double alterValue = pBehaviorData->
+ value(this->lperiod,iter.actor());
+ totalAlterValue += alterValue;
+ }
+ averageAlterValue = totalAlterValue /
+ pStructural->outDegree(i);
+ }
+
+ statistic += averageAlterValue *
+ difference[i];
+ }
+ else
+ {
+ throw domain_error("Unexpected rate effect " + effectName);
+ }
+ }
+
+ this->lstatistics[pInfo] = statistic;
+ delete pStructural;
+ }
}
delete[] difference;
Added: pkg/RSienaTest/src/model/effects/DiffusionRateEffect.cpp
===================================================================
--- pkg/RSienaTest/src/model/effects/DiffusionRateEffect.cpp (rev 0)
+++ pkg/RSienaTest/src/model/effects/DiffusionRateEffect.cpp 2011-09-19 17:10:56 UTC (rev 172)
@@ -0,0 +1,122 @@
+/******************************************************************************
+ * SIENA: Simulation Investigation for Empirical Network Analysis
+ *
+ * Web: http://www.stats.ox.ac.uk/~snijders/siena/
+ *
+ * File: DiffusionRateEffect.cpp
+ *
+ * Description: This file contains the implementation of the class
+ * DiffusionRateEffect.
+ *****************************************************************************/
+
+#include "DiffusionRateEffect.h"
+#include "utils/Utils.h"
+#include "network/OneModeNetwork.h"
+#include "model/variables/NetworkVariable.h"
+#include "model/variables/BehaviorVariable.h"
+#include "model/variables/DiffusionEffectValueTable.h"
+#include "network/IncidentTieIterator.h"
+#include <cmath>
+#include <R_ext/Print.h>
+
+namespace siena
+{
+
+/**
+ * Constructor.
+ * @param[in] pVariable the network variable this effect depends on
+ * @param[in] pBehaviorVariable the behavior variable this effect depends on
+ * @param[in] type the type of this effect
+ * @param[in] parameter the statistical parameter of this effect
+ */
+
+DiffusionRateEffect::DiffusionRateEffect(const NetworkVariable * pVariable,
+ const BehaviorVariable * pBehaviorVariable,
+ DiffusionRateEffectType type,
+ double parameter)
+{
+ this->lpVariable = pVariable;
+ this->lpBehaviorVariable = pBehaviorVariable;
+ this->ltype = type;
+
+ double possibleDegreeNumer = 1;
+ double possibleDegreeDenom = 1;
+
+ if (this->ltype == AVERAGE_EXPOSURE_RATE)
+ {
+ possibleDegreeNumer = (this->lpBehaviorVariable->range())*
+ max(this->lpVariable->n(),
+ this->lpVariable->m());
+ possibleDegreeDenom = max(this->lpVariable->n(),
+ this->lpVariable->m());
+ }
+ this->lpTable = new DiffusionEffectValueTable(possibleDegreeNumer,
+ possibleDegreeDenom);
+ this->lpTable->parameter(parameter);
+}
+
+/**
+ * Destructor.
+ */
+DiffusionRateEffect::~DiffusionRateEffect()
+{
+ delete this->lpTable;
+ this->lpTable = 0;
+}
+
+
+/**
+ * Returns the contribution of this effect for the given actor.
+ */
+double DiffusionRateEffect::value(int i)
+{
+ Network * pNetwork = this->lpVariable->pNetwork();
+ switch (this->ltype)
+ {
+ case AVERAGE_EXPOSURE_RATE:
+ {
+ int totalAlterValue = 0;
+ if (pNetwork->outDegree(i) > 0)
+ {
+ for (IncidentTieIterator iter = pNetwork->outTies(i);
+ iter.valid();
+ iter.next())
+ {
+ double alterValue = this->lpBehaviorVariable->
+ value(iter.actor());
+ totalAlterValue += alterValue;
+ }
+ }
+
+ if (totalAlterValue==0)
+ {
+ return 1;
+ }
+ if (totalAlterValue>0)
+ {
+ return this->lpTable->value(totalAlterValue,max(1,
+ pNetwork->outDegree(i)));
+ }
+ }
+ }
+
+ throw new logic_error("Unexpected diffusion rate effect type");
+}
+/**
+ * Stores the parameter for the diffusion rate effect.
+ */
+void DiffusionRateEffect::parameter(double parameterValue)
+{
+ this->lpTable->parameter(parameterValue);
+}
+
+/**
+ * Returns the parameter for the diffusion rate effect.
+ */
+double DiffusionRateEffect::parameter() const
+{
+ return this->lpTable->parameter();
+}
+
+
+}
Property changes on: pkg/RSienaTest/src/model/effects/DiffusionRateEffect.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: pkg/RSienaTest/src/model/effects/DiffusionRateEffect.h
===================================================================
--- pkg/RSienaTest/src/model/effects/DiffusionRateEffect.h (rev 0)
+++ pkg/RSienaTest/src/model/effects/DiffusionRateEffect.h 2011-09-19 17:10:56 UTC (rev 172)
@@ -0,0 +1,83 @@
+/******************************************************************************
+ * SIENA: Simulation Investigation for Empirical Network Analysis
+ *
+ * Web: http://www.stats.ox.ac.uk/~snijders/siena/
+ *
+ * File: DiffusionRateEffect.h
+ *
+ * Description: This file contains the definition of the
+ * DiffusionRateEffect class.
+ *****************************************************************************/
+
+#ifndef DIFFUSIONRATEEFFECT_H_
+#define DIFFUSIONRATEEFFECT_H_
+
+namespace siena
+{
+
+// ----------------------------------------------------------------------------
+// Section: Enums
+// ----------------------------------------------------------------------------
+
+/**
+ * Available types of diffusion rate effects.
+ */
+enum DiffusionRateEffectType
+{
+ AVERAGE_EXPOSURE_RATE
+};
+
+
+// ----------------------------------------------------------------------------
+// Section: Forward declarations
+// ----------------------------------------------------------------------------
+
+class NetworkVariable;
+class BehaviorVariable;
+class DiffusionEffectValueTable;
+
+
+// ----------------------------------------------------------------------------
+// Section: Class definition
+// ----------------------------------------------------------------------------
+
+/**
+ * Encapsulates the information necessary for calculating the contributions
+ * of a diffusion rate effect. This includes the effect type,
+ * the network variable and the behavior variable
+ * the effect depends on, and the statistical parameter of the effect.
+ */
+class DiffusionRateEffect
+{
+public:
+ DiffusionRateEffect(const NetworkVariable * pVariable,
+ const BehaviorVariable * pBehaviorVariable,
+ DiffusionRateEffectType type,
+ double parameter);
+ virtual ~DiffusionRateEffect();
+
+ double value(int i);
+ void parameter(double parameterValue);
+ double parameter() const;
+
+private:
+ // The network variable this effect depends on
+ const NetworkVariable * lpVariable;
+
+ // The behavior variable this effect depends on
+ const BehaviorVariable * lpBehaviorVariable;
+
+ // The type of the effect
+ DiffusionRateEffectType ltype;
+
+ // A table for efficient calculation of contributions. If two actors have
+ // the same effect value, then the table ensures that we don't
+ // calculate the same contribution twice.
+
+ DiffusionEffectValueTable * lpTable;
+
+};
+
+}
+
+#endif /* DIFFUSIONRATEEFFECT_H_ */
Property changes on: pkg/RSienaTest/src/model/effects/DiffusionRateEffect.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: pkg/RSienaTest/src/model/variables/DependentVariable.cpp
===================================================================
--- pkg/RSienaTest/src/model/variables/DependentVariable.cpp 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSienaTest/src/model/variables/DependentVariable.cpp 2011-09-19 17:10:56 UTC (rev 172)
@@ -31,8 +31,10 @@
#include "model/effects/AllEffects.h"
#include "model/effects/EffectFactory.h"
#include "model/effects/StructuralRateEffect.h"
+#include "model/effects/DiffusionRateEffect.h"
#include "model/variables/NetworkVariable.h"
#include "model/variables/EffectValueTable.h"
+#include "network/IncidentTieIterator.h"
namespace siena
{
@@ -151,7 +153,7 @@
"'.");
// }
}
- else
+ else if (rateType == "structural")
{
// We expect a structural (network-dependent) rate effect here.
@@ -246,6 +248,54 @@
throw domain_error("Unexpected rate effect " + effectName);
}
}
+ else if (rateType == "diffusion")
+ {
+
+ const NetworkVariable * pVariable;
+ const BehaviorVariable * pBehaviorVariable =
+ dynamic_cast<const BehaviorVariable *>(this);
+
+ if (interactionName == "")
+ {
+ pVariable = dynamic_cast<const NetworkVariable *>(this);
+ }
+ else
+ {
+ pVariable = dynamic_cast<const NetworkVariable *>(
+ this->lpSimulation->pVariable(interactionName));
+ }
+
+ if (!pVariable)
+ {
+ throw logic_error("The diffusion rate effect " +
+ effectName +
+ " for dependent variable " +
+ this->name() +
+ " refers to a non-existing network variable " +
+ interactionName);
+ }
+
+ if (effectName == "averageExposure")
+ {
+ if (this->lpActorSet != pVariable->pSenders())
+ {
+ throw std::invalid_argument("Mismatch of actor sets");
+ }
+
+ this->ldiffusionRateEffects.push_back(
+ new DiffusionRateEffect(pVariable,
+ pBehaviorVariable,
+ AVERAGE_EXPOSURE_RATE,
+ parameter));
+ this->laverageExposureScores[pVariable] = 0;
+ this->laverageExposureSumTerm[pVariable] = 0;
+ }
+ else
+ {
+ throw domain_error("Unexpected rate effect " + effectName);
+ }
+ }
+
}
// If there are no rate effects depending on changing covariates,
// or behavior variables then the covariate based rates can be calculated
@@ -317,6 +367,9 @@
// Delete the structural rate effects.
deallocateVector(this->lstructuralRateEffects);
+ // Delete the diffusion rate effects.
+ deallocateVector(this->ldiffusionRateEffects);
+
// Nullify the fields
this->lpSimulation = 0;
@@ -484,7 +537,8 @@
bool DependentVariable::constantRates() const
{
return this->lstructuralRateEffects.empty() &&
- this->lbehaviorVariableParameters.empty();
+ this->ldiffusionRateEffects.empty() &&
+ this->lbehaviorVariableParameters.empty();
}
@@ -499,9 +553,10 @@
// later two components are precomputed for efficiency.
return this->basicRate() *
- this->lcovariateRates[i] *
- this->behaviorVariableRate(i) *
- this->structuralRate(i);
+ this->lcovariateRates[i] *
+ this->behaviorVariableRate(i) *
+ this->structuralRate(i) *
+ this->diffusionRate(i);
}
@@ -626,6 +681,22 @@
}
/**
* Returns the component of the rate function of actor <i>i</i> depending
+ * on diffusion effects.
+ */
+double DependentVariable::diffusionRate(int i) const
+{
+ double rate = 1;
+ int effectCount = this->ldiffusionRateEffects.size();
+
+ for (int effectIndex = 0; effectIndex < effectCount; effectIndex++)
+ {
+ rate *= this->ldiffusionRateEffects[effectIndex]->value(i);
+ }
+
+ return rate;
+}
+/**
+ * Returns the component of the rate function of actor <i>i</i> depending
* on behavior variables.
*/
double DependentVariable::behaviorVariableRate(int i) const
@@ -794,6 +865,43 @@
iter->second -= this->linverseOutDegreeSumTerm[iter->first] * tau;
}
+
+ // Update scores for diffusion rate parameters
+
+ for (std::map<const NetworkVariable *, double>::iterator iter =
+ this->laverageExposureScores.begin();
+ iter != this->laverageExposureScores.end();
+ iter++)
+ {
+ const Network * pNetwork = iter->first->pNetwork();
+ const BehaviorVariable * pBehaviorVariable;
+ pBehaviorVariable = dynamic_cast<const BehaviorVariable *>(pSelectedVariable);
+ if (this == pSelectedVariable)
+ {
+ int totalAlterValue = 0;
+ double averageAlterValue = 0;
+ if (pNetwork->outDegree(selectedActor) > 0)
+ {
+ for (IncidentTieIterator iter2 =
+ pNetwork->outTies(selectedActor);
+ iter2.valid();
+ iter2.next())
+ {
+ double alterValue = pBehaviorVariable->
+ value(iter2.actor());
+ totalAlterValue += alterValue;
+ }
+ averageAlterValue = totalAlterValue /
+ pNetwork->outDegree(selectedActor);
+ }
+
+ iter->second += averageAlterValue;
+ }
+
+ iter->second -= this->laverageExposureSumTerm[iter->first] *
+ tau;
+ }
+
}
/**
@@ -1079,6 +1187,41 @@
(this->ltotalRate * timesRate - timesRateSquared);
}
+
+ // Update scores for diffusion rate parameters.
+
+ for (std::map<const NetworkVariable *, double>::iterator iter =
+ this->laverageExposureScores.begin();
+ iter != this->laverageExposureScores.end();
+ iter++)
+ {
+ const Network * pNetwork = iter->first->pNetwork();
+ const BehaviorVariable * pBehaviorVariable;
+ pBehaviorVariable = dynamic_cast<const BehaviorVariable *>(this);
+
+ double timesRate = 0;
+ for (int i = 0; i < this->n(); i++)
+ {
+ int totalAlterValue = 0;
+ double averageAlterValue = 0;
+ if (pNetwork->outDegree(i) > 0)
+ {
+ for (IncidentTieIterator iter2 = pNetwork->outTies(i);
+ iter2.valid();
+ iter2.next())
+ {
+ double alterValue = pBehaviorVariable->
+ value(iter2.actor());
+ totalAlterValue += alterValue;
+ }
+ averageAlterValue = totalAlterValue /
+ pNetwork->outDegree(i);
+ }
+
+ timesRate += averageAlterValue * this->lrate[i];
+ }
+ this->laverageExposureSumTerm[iter->first] = timesRate;
+ }
}
/**
@@ -1239,7 +1382,23 @@
return iter->second;
}
+double DependentVariable::averageExposureScore(
+ const NetworkVariable * pNetworkData) const
+{
+ map<const NetworkVariable *, double>::const_iterator iter =
+ this->laverageExposureScores.find(pNetworkData);
+ if (iter == this->laverageExposureScores.end())
+ {
+ throw invalid_argument(
+ string("Unknown network: ") +
+ "The given average exposure rate effect is not " +
+ "part of the model.");
+ }
+
+ return iter->second;
+}
+
// ----------------------------------------------------------------------------
// Section: Composition change
// ----------------------------------------------------------------------------
@@ -1392,6 +1551,26 @@
iter++;
}
}
+
+ vector < DiffusionRateEffect * >::iterator iter2 =
+ this->ldiffusionRateEffects.begin();
+
+ for (unsigned i = 0; i < rRateEffects.size(); i++)
+ {
+ EffectInfo * pEffectInfo = rRateEffects[i];
+ string interactionName = pEffectInfo->interactionName1();
+ string rateType = pEffectInfo->rateType();
+
+ if (rateType == "diffusion")
+ {
+ DiffusionRateEffect *pEffect = *iter2;
+ pEffect->parameter(pEffectInfo->parameter());
+ iter2++;
+ }
+ }
+
+
+
}
// ----------------------------------------------------------------------------
// Section: Properties
Modified: pkg/RSienaTest/src/model/variables/DependentVariable.h
===================================================================
--- pkg/RSienaTest/src/model/variables/DependentVariable.h 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSienaTest/src/model/variables/DependentVariable.h 2011-09-19 17:10:56 UTC (rev 172)
@@ -41,6 +41,7 @@
class Network;
class EffectInfo;
class StructuralRateEffect;
+class DiffusionRateEffect;
class MiniStep;
@@ -118,6 +119,10 @@
double reciprocalDegreeScore(const NetworkVariable * pNetwork) const;
double inverseOutDegreeScore(const NetworkVariable * pNetwork) const;
+ // Diffusion effects
+
+ double averageExposureScore(const NetworkVariable * pNetwork) const;
+
// Maximum likelihood related
/**
@@ -174,6 +179,7 @@
bool constantRates() const;
double calculateRate(int i);
double structuralRate(int i) const;
+ double diffusionRate(int i) const;
double behaviorVariableRate(int i) const;
void updateCovariateRates();
void calculateScoreSumTerms();
@@ -214,6 +220,10 @@
vector<StructuralRateEffect *> lstructuralRateEffects;
+ // The diffusion rate effects.
+
+ vector<DiffusionRateEffect *> ldiffusionRateEffects;
+
// The evaluation function for this variable
Function * lpEvaluationFunction;
@@ -256,6 +266,9 @@
// Scores for rate effects depending on inverse degree
map<const NetworkVariable *, double> linverseOutDegreeScores;
+ // Scores for rate effects depending on average exposure
+ map<const NetworkVariable *, double> laverageExposureScores;
+
// Sum term for scores for rate effects depending on constant covariates
map<const ConstantCovariate *, double> lconstantCovariateSumTerm;
@@ -277,6 +290,9 @@
// Sum term for scores for rate effects depending on inverse degree
map<const NetworkVariable *, double> linverseOutDegreeSumTerm;
+ // Sum term for scores for rate effects depending on average exposure
+ map<const NetworkVariable *, double> laverageExposureSumTerm;
+
// Sum term for model B scores for rate effects depending on constant
// covariates
map<const ConstantCovariate *, double> lconstantCovariateModelBSumTerm;
Added: pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.cpp
===================================================================
--- pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.cpp (rev 0)
+++ pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.cpp 2011-09-19 17:10:56 UTC (rev 172)
@@ -0,0 +1,96 @@
+/******************************************************************************
+ * SIENA: Simulation Investigation for Empirical Network Analysis
+ *
+ * Web: http://www.stats.ox.ac.uk/~snijders/siena/
+ *
+ * File: DiffusionEffectValueTable.cpp
+ *
+ * Description: This file contains the implementation of the
+ * DiffusionEffectValueTable class.
+ *****************************************************************************/
+using namespace std;
+#include <cmath>
+#include "DiffusionEffectValueTable.h"
+
+namespace siena
+{
+
+/**
+ * Creates a new look-up table for <i>n</i> actors.
+ */
+
+DiffusionEffectValueTable::DiffusionEffectValueTable(int numeratorRange,
+ int denominatorRange)
+{
+ this->lvalues = new double[numeratorRange*denominatorRange];
+ this->lparameterValues = new double[numeratorRange*denominatorRange];
+
+ // Calculate the table for parameter 0
+
+ this->lparameter = 0;
+ this->ldenominatorRange = denominatorRange;
+ this->lnumeratorRange = numeratorRange;
+
+ for (int i = 0; i < numeratorRange*denominatorRange; i++)
+ {
+ // exp(0) = 1
+
+ this->lvalues[i] = 1;
+ this->lparameterValues[i] = 0;
+ }
+}
+
+/**
+ * Deallocates this look-up table.
+ */
+DiffusionEffectValueTable::~DiffusionEffectValueTable()
+{
+ delete[] this->lvalues;
+ delete[] this->lparameterValues;
+
+ this->lvalues = 0;
+ this->lparameterValues = 0;
+}
+
+
+/**
+ * Returns the current value of the effect parameter.
+ */
+double DiffusionEffectValueTable::parameter() const
+{
+ return this->lparameter;
+}
+
+
+/**
+ * Stores the effect parameter.
+ */
+void DiffusionEffectValueTable::parameter(double value)
+{
+ this->lparameter = value;
+}
+
+
+/**
+ * Returns the value of the effect.
+ */
+
+double DiffusionEffectValueTable::value(int numerator, int denominator)
+{
+ int arrayIndex = ((numerator-1)*this->ldenominatorRange)+
+ (denominator-1);
+
+ if (this->lparameterValues[arrayIndex] != this->lparameter)
+ {
+ // The value stored in the table was calculated for a different
+ // parameter, hence we must recalculate the value.
+
+ this->lvalues[arrayIndex] = exp(this->lparameter *
+ numerator/denominator);
+ this->lparameterValues[arrayIndex] = this->lparameter;
+ }
+
+ return this->lvalues[arrayIndex];
+}
+
+}
Property changes on: pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.h
===================================================================
--- pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.h (rev 0)
+++ pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.h 2011-09-19 17:10:56 UTC (rev 172)
@@ -0,0 +1,60 @@
+/******************************************************************************
+ * SIENA: Simulation Investigation for Empirical Network Analysis
+ *
+ * Web: http://www.stats.ox.ac.uk/~snijders/siena/
+ *
+ * File: DiffusionEffectValueTable.h
+ *
+ * Description: This file contains the definition of the
+ * DiffusionEffectValueTable class.
+ *****************************************************************************/
+
+#ifndef DIFFUSIONEFFECTVALUETABLE_H_
+#define DIFFUSIONEFFECTVALUETABLE_H_
+
+namespace siena
+{
+
+/**
+ * This class provides a look-up table supporting effective calculation of
+ * diffusion effects on rate functions. The value of the effect for an
+ * argument <i>i</i> in [0, <i>n</i>) is defined as
+ * exp(\alpha <i>f</i>(<i>i</i>)), where \alpha is a parameter associated
+ * with this effect and <i>f</i> is an arbitrary function. Since calculating
+ * exponentials is expensive, the values are stored for each <i>i</i> for
+ * later reuse as long as the parameter remains unchanged.
+ */
+class DiffusionEffectValueTable
+{
+public:
+ DiffusionEffectValueTable(int numeratorRange, int denominatorRange);
+ virtual ~DiffusionEffectValueTable();
+
+ double parameter() const;
+ void parameter(double value);
+ double value(int i);
+ double value(int numerator, int denominator);
+
+private:
+ // The look-up table for effect values
+ double * lvalues;
+
+ // Here we remember the parameter value whenever we store a value
+ // in the table.
+
+ double * lparameterValues;
+
+ // The actual value of the effect parameter
+ double lparameter;
+
+ // The range of the denominator;
+ int ldenominatorRange;
+
+ // The range of the numerator;
+ int lnumeratorRange;
+
+};
+
+}
+
+#endif /*DIFFUSIONEFFECTVALUETABLE_H_*/
Property changes on: pkg/RSienaTest/src/model/variables/DiffusionEffectValueTable.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: pkg/RSienaTest/src/siena07internals.cpp
===================================================================
--- pkg/RSienaTest/src/siena07internals.cpp 2011-09-07 16:10:08 UTC (rev 171)
+++ pkg/RSienaTest/src/siena07internals.cpp 2011-09-19 17:10:56 UTC (rev 172)
@@ -1430,6 +1430,47 @@
score = 0;
}
}
+ else if (strcmp(rateType, "diffusion") == 0)
+ {
+ EffectInfo * pEffectInfo = (EffectInfo *)
+ R_ExternalPtrAddr(
+ VECTOR_ELT(VECTOR_ELT(EFFECTS,
+ pointerCol), i));
+ statistic = pCalculator->statistic(pEffectInfo);
+ if (pEpochSimulation)
+ {
+ const DependentVariable * pVariable =
+ pEpochSimulation->pVariable(networkName);
+ const NetworkVariable * pNetworkVariable;
+ if (strcmp(interaction1, "") == 0)
+ {
+ pNetworkVariable =
+ (const NetworkVariable *)
+ pEpochSimulation->pVariable(networkName);
+ }
+ else
+ {
+ pNetworkVariable =
+ (const NetworkVariable *)
+ pEpochSimulation->pVariable(interaction1);
+ }
+ if (strcmp(effectName, "avExposure") == 0)
+ {
+ score =
+ pVariable->averageExposureScore(pNetworkVariable);
+ }
+ else
+ {
+
+ error("Unexpected rate effect %s\n",
+ effectName);
+ }
+ }
+ else
+ {
+ score = 0;
+ }
+ }
else
{
EffectInfo * pEffectInfo = (EffectInfo *)
More information about the Rsiena-commits
mailing list