\subsection{Generating the acceptance probability calculation code}

This chapter describes how the generator data structures are used when generating the Fortran code which calculates the acceptance probability for a single parameter.
 
The following connections between the objects are needed:
\begin{itemize}
\item The variables\footnote{In this section, the term variable is used when referring to a variable or a parameter and when it's irrelevant which one there really is.} on which the current parameter depends
\item The distribution which describes the dependency (the name of the distribution and the order of its parameters)
\item The variables which depend on the current parameter
\item The distributions which describe the dependencies (the names of the distributions and the order of their parameters)
\item The proposal distribution and the proposal strategy of the current parameter
\end{itemize}

When these fields are correctly set, it is possible to generate the code which calculates the acceptance probability.

The basic algorithm for generating the probability calculation code for one variable is:
\begin{itemize}
\item Initialize p\_acc (an internal variable in the update subroutine describing the acceptance probability of the variable being updated) to 1 and generate a proposal
\item Considering the current parameter's distribution...
\item generate a subroutine call: the frequency function\footnote{Note that frequency function is used as a mathematical term whereas the result is often calculated by using a (Fortran) subroutine, not a (Fortran) function.} with the parameters defined and \textbf{the old value} (store the result into a temporary variable)
\item generate a subroutine call: the frequency function with the parameters defined and \textbf{the new value} (store the result into a temporary variable)
\item Generate code which divides the latter by the former and multiplies p\_acc with it
\item Loop through the variables which depend on the current parameter. For each:
	\begin{itemize}
		\item Based on its distribution...
		\item generate a subroutine call: the frequency function with its parameters including \textbf{the old value of the current variable} and the value of the depending variable (store the result into a temporary variable)
		\item generate a subroutine call: the frequency function with its parameters including \textbf{the new value of the current variable} and the value of the depending variable (store the result into a temporary variable)
		\item Generate code which divides the latter by the former and multiplies p\_acc with it		 
	\end{itemize}
\item If the proposal strategy is random walk: 
	\begin{itemize}
	\item Generate code which calculates the difference ``new value - current value''
	\item Generate a subroutine call: the density function of the proposal distribution with its parameters and \textbf{the opposite number of the difference}
	\item Generate a subroutine call: the density function of the proposal distribution with its parameters and \textbf{the difference}
	\item Generate code which divides the former result by the latter result and multiplies p\_acc with it
	\end{itemize}
\item If the proposal strategy is fixed proposal distribution
	\begin{itemize}
	\item Generate a subroutine call: the density function of the proposal distribution with its parameters and \textbf{the new value}
	\item Generate a subroutine call: the density function of the proposal distribution again with its parameters and \textbf{the old value}
	\item Generate code which divides the former result by the latter result and multiplies p\_acc with it.
	\end{itemize}
	Note: The current value doesn't affect the next proposed value in any way.
\end{itemize}

When generating the detailed code for getting the correct values, the following information is needed:
\begin{itemize}
\item The entity to which the current parameter belongs
\item The entities to which the affected variables belong
\item The entities to which the affecting variables belong
\end{itemize}

For each entity the following information is needed:
\begin{itemize}
\item The dimension and the size of the entity 
\item If the entity describes a intersection of two entities, the parent entities (XCoordinate and YCoordinate)
\end{itemize}

\subsubsection{Indexing}

We have the current variable with which we are dealing right now, and which we know how to index. (For example a\_i\_j, and we know we have to use a \% two\_dim(i, j) \% value.) Then we have another variable, which appears in the distribution of the current variable, but which we don't know how to index. The other variable may be global, belonging to the same entity as the current variable or belonging to a different entity as the current variable.

The following cases are possible:
\begin{enumerate}
\item The two variables are both global
\item The two variables belong to the same one-dimensional entity 
\item The two variables belong to the same two-dimensional entity 
\item The affecting variable is global and the current variable belongs to an one-dimensional entity
\item The affecting variable belongs to an one-dimensional entity and the current variable belongs to a two-dimensional entity
\item The affecting variable is global and the current variable belongs to a two-dimensional entity (Note that this can only be done through a functional parameter between them.)
\end{enumerate}

By using \textit{Entity} objects it's possible to find out which case is present.

This is how the previous situations are solved:
\begin{enumerate}
\item No special indexing is needed.
\item The same indexing is used for the two variables. For example if we know how to index a \% one\_dim(i), we index b \% one\_dim(i) accordingly.
\item Same as the previous case except that we need two indices, for example a \% two\_dim(i, j) and b \% two\_dim(i, j).
\item No indexing is needed for the global variable and we already know how to index the current variable.
\item We must use the correct indexing based on the entities of the variables. For example: we have a \% two\_dim(i, j) and an one-dimensional variable b affects it. We must decide whether we use b \% one\_dim(i) or b \% one\_dim(j). If b:s Entity equals the XCoordinate of a, we use b \% one\_dim(j) and if b:s Entity equals the YCoordinate of a, we use b \% one\_dim(i). Note that in Fortran the first coordinate is y, not x.
\item No special indexing is needed.
\end{enumerate}

Note that in the generated Fortran program a global variable is stored as a one-dimensional variable with only one unit.

So that's how we can generate code which gets the values of the needed variables. New values are acquired the same way except we use \% new\_value instead of \% value, and we need to keep track which is the variable for which we must use the new value.

\subsubsection{Loops}

When we are generating the probability calculation code for a current variable, we might need loops, if the current variable affects other variables that belong to a different entity which has more dimensions than the entity of the current variable.

We assume that the current variable is indexed with \% one\_dim(1) (if it is global), \% one\_dim(i) (if it is one-dimensional) or \% two\_dim(i, j) (if it is two-dimensional).

The following cases are possible:
\begin{enumerate}
\item The affecting variable and the affected variable are both global
\item The affecting variable and the affected variable belong to the same one-dimensional entity 
\item The affecting variable and the affected variable belong to the same two-dimensional entity 
\item The affecting variable is global and the affected variable belongs to an one-dimensional entity
\item The affecting variable belongs to an one-dimensional entity and the affected variable belongs to a two-dimensional entity
\item The affecting variable is global and the affected variable belongs to a two-dimensional entity (Note that this can only be done through a functional parameter between them.)
\end{enumerate}

This is how the previous situations are solved:
\begin{enumerate}
\item No loops are needed. 
\item No loops are needed. 
\item No loops are needed. 
\item We need to loop through all the variable instances of the affected variable. For example if the global variable a affects all variable b:s related to birds, we must loop through all the b:s with: \newline

\begin{verbatim}
DO i=1, 200 
	(where 200 is the size of the entity, 
	for example the number of the birds)
	calculate everything, the indexing is:
	a % one_dim(1) (global variable)
	b % one_dim(i)
END DO
\end{verbatim}
\item We need to loop through the variable instances which belong to the two-dimensional entity, and we need to know which is the current index of the one-dimensional variable. For example if we have the variable \textbf{a} related to birds and the variable \textbf{b} related to the intersection of birds and squares, we need to loop through all the b:s related to the specific bird:
\begin{verbatim}
DO j=1, 300 
	(where 300 is the size of the entity, 
	for example the number of the squares)
	calculate everything, the indexing needed is:
	a % one_dim(i) 
	b % two_dim(i, j) (if a's Entity is b:s YCoordinate)
	or b % two_dim(j, i) (if a's Entity is b:s XCoordinate)
END DO
\end{verbatim}
\item We need to loop through the variable instances which belong to the two-dimensional entity. For example if we have the variable \textbf{a} which is global and the variable \textbf{b} related to the intersection of birds and squares, we need to loop through all the b:s related to the specific bird:
\begin{verbatim}
DO j=1, 300 
	(where 300 is the size of the entity, 
	for example the number of the squares)
	calculate everything, the indexing needed is:
	a % one_dim(1) (global) 
	b % two_dim(i, j) 
END DO
\end{verbatim}
\end{enumerate}

\subsubsection{Functional parameters}

How the functional parameters are dealt with: 
\begin{itemize}
\item When a parameter is updated, the functional parameters affected by it are also updated.
\item When a new value is generated for a parameter, new values are also calculated for all the functional parameters which depend on the parameter.
\item A variable is considered to affect another variable also when there is a functional parameter between them.
\end{itemize}

All needed information related to functional parameters is stored in the \textit{Equation} object.

\subsubsection{Generating the function/subroutine calls}

Generating the function / subroutine calls is straightforward, when we know the parameters as \textit{Strings}. We simply call the method getFreqCode in the correct Distribution object, passing the wanted parameters as Strings.

For example: \newline 
getFreqCode(``alpha \% one\_dim(i)'', ``beta \% one\_dim(j)'', ``x \% two\_dim(i, j)'', ``frequency'')

The method generates the corresponding NAG subroutine call, which calls the frequency function with given parameters and stores the result into the given variable.
