Difference between revisions of "Analytica User FAQs/Expressions - How to"

 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category: FAQ]]
 +
[[Category: Expressions]]
 +
 
(In progress -- two things need to occur.  Common questions need to be collected here and organized, and then answers need to be added.  Feel free to contribute to either aspect)
 
(In progress -- two things need to occur.  Common questions need to be collected here and organized, and then answers need to be added.  Feel free to contribute to either aspect)
  
Line 25: Line 28:
 
Generally, to aggregate, you will need a mapping (many-to-one) from the fine grain to the coarse grain.  This mapping will take the form of an array, indexed by the fine grain (e.g., days), where each value in the array is an element in the coarser index (e.g., months).    For illustration, let's call this ''Day2Month''.
 
Generally, to aggregate, you will need a mapping (many-to-one) from the fine grain to the coarse grain.  This mapping will take the form of an array, indexed by the fine grain (e.g., days), where each value in the array is an element in the coarser index (e.g., months).    For illustration, let's call this ''Day2Month''.
  
There are many different ways to aggregate dataThe most common is to sum all fine grain values that belong to the same coarse grain category. For clarity and flexibility, I like to always create a [[User-Defined Functions|User-Defined Function (UDF)]] to actually perform the aggregation, so that the intended operation is clear in the definition that uses it, hiding "implementation details".  In this case, the aggregation function could look like this:
+
Once you have this map, use the [[Aggregate]] functionHere is an example:
 +
<code>
 +
:Index theDate
 +
:Variable Revenue_by_date { ''indexed by theDate'' }
 +
:Variable Date_to_year_map := [[DatePart]]( theDate, "Y" )
 +
:Variable Year := 2000..2020
 +
:Variable Revenue_by_year := [[Aggregate]](Revenue_by_date, Date_to_year_map, theDate, Year )
 +
</code>
  
:Function SumAggregate( x : Array[I] ; map : Array[I] ; I,J : Index )
+
See [[Aggregate]] for more details.
:Description: Aggregates x from a fine-grained index I to a coarser index J, but summing all values that aggregate to the same coarse category.  Map contains the mapping from I to J.
 
:Definition: [[Frequency]](w:x, X:map, I:I, A:J )
 
 
 
Once you've introduced this function, then all you need to do to aggregate daily revenue to a monthly level is:
 
:SumAggregate( revenue, day2Month, Day, Month )
 
  
 
= Distributions =
 
= Distributions =
Line 50: Line 55:
 
= How do I model XXX? =
 
= How do I model XXX? =
  
=== How do I model Depreciation (offset in time)? ===
+
* [[Modeling Depreciation|How do I model depreciation over time?]]
To model depreciation, two inputs are required:
 
1) a schedule of spend to be depreciated
 
2) a depreciation schedule
 
  
For this example, the index time is 10 year series with a constant startYear=2008
+
== How can I represent missing values? ==
time:=sequence (startYear,startYear+9,1)
 
  
Example depreciation schedule is a 5yr MACRS depreciation schedule entered as a variable:
+
The constant [[Null]] is useful to represent a missing value.
Table(time)(.2,.32,.192,.1152,.1152,.0576,0,0,0,0)
 
  
SpendOverTime is a variable indexed by time with the capital spend amounts as desired:
+
* [[Operators]], such as + - * / ^ > >= OR AND, all return [[Null]] if one or both operands are [[Null]].
table(time)(0,0,111,0,0,0,0,0,0,0)
+
* Reducing array functions, including, [[Sum]], [[Product]], [[Max]], [[Min]], [[Average]], ignore [[Null]] -- that is, they take the Sum, Product, Max, Min, Average of all the numbers that are not [[Null]].  (If all values are [[Null]], they return [[Null]].)  
 +
* A Graph view does not display [[Null]] values, leaving gaps in lines where the array contains [[Null]].
 +
* Statistical functions, such as [[Mean]], [[Sdeviation]], [[Getfract]], ignore [[v]] values.
 +
* Probability distributions ignore [[Null]] values when generating density functions, cumulative probability functions, and so on. They treat the sample as though the [[Null]] didn't exist, and the sample size is the number of non-[[NULL]] values.
 +
* Interpolation functions, [[LinearInterp]], [[StepInterp]], [[CubicInterp]], interpolate over [[Null]] between the nearest numerical values.
 +
* [[Regression]] also ignores [[Null]] values in the dependent or independent variables.
 +
* Some other array functions may just return [[Null]] if their parameters contain a [[Null]] value.
  
Create a user defined function:
+
Library functions will similarly ignore NULL values if they rely on built-in functions that do that. If you want your user-defined functions to treat Null differently, you can insert appropriate conditional logic to deal with the missing values in the appropriate fashion. For example:
Function_Depreciation,
 
'''Parameters''': (SpendOverTime,DepreciationSchedule),
 
'''Definition''':
 
index depr_years = copyindex(time);
 
var deprStartyr:=time;
 
sum(if time-depr_years+1<1 then 0 else
 
slice(SpendOverTime,time,Depr_years-startYear+1)*slice(depreciationSchedule,time,time-depr_years+1),depr_years)
 
 
 
Then create a depreciation test variable to check the result
 
Variable: DepreciationTest  definition: function_Depreciation(spendOverTime,DepreciationSchedule)
 
 
 
== What does Analytica use to represent missing values? ==
 
 
 
Analytica has no special MissingValue constant.  The constant [[Null]] typically serves that purpose.
 
 
 
Several common functions that operate over arrays, but not all functions, will ignore [[Null]] values.  For example, [[Sum]], [[Product]], [[Max]], [[Min]], [[Average]], etc.  This treatment of [[Null]] by these functions is relatively recent -- starting with 4.0 -- and we've been enhancing the remaining functions with successive releases.  Currently there still remain several built-in functions that do not ignore [[Null]], such as many statistical functions, and your own UDFs may not ignore nulls.  You may also desire a different treatment of missing values than just ignoring the values. To deal with these cases, you will need to insert appropriate conditional logic to deal with the missing values in the appropriate fashion. For example:
 
  
 
:Variance( MaybeMissing(x), I )
 
:Variance( MaybeMissing(x), I )
Line 89: Line 79:
 
:Function MaybeMissing( x ; defX : optional )
 
:Function MaybeMissing( x ; defX : optional )
 
:Definition: if x=Null then if IsNotSpecified(defX) then 0 else defX else X
 
:Definition: if x=Null then if IsNotSpecified(defX) then 0 else defX else X
 +
 +
= Debugging =
 +
 +
== How do I go about debugging a model? ==
 +
 +
See [[Debugging Hints]]

Latest revision as of 19:57, 18 March 2016


(In progress -- two things need to occur. Common questions need to be collected here and organized, and then answers need to be added. Feel free to contribute to either aspect)

Array Abstraction

How do I access a single row of an array?

See Subscript/Slice Operator. The syntax is A[I=x], where A is the array, I is the index that identifies the row dimension, and x identifies the row using the value or label from I.

How do I represent a square matrix?

The rule in Analytica is that every dimension must correspond to a different index. So when you have two dimensions, you need two separate indexes. In the case of a square matrix, the second index is often a copy of the first.

Try this. Create an index node, name it State, and define it as 1..5. How create a second index, name it State2, and define it as CopyIndex(State). Now you have two indexes with identical value. You have what you need to represent a square matrix. Finally, create a variable, name it State_Identity, define it as State=State2, and show the result table. You just created a square identity matrix.

How do I re-index an array, exchanging one index, I, for another of the same length, J?

Again, this is accomplished using the Subscript/Slice Operator. Two methods:

  • A[I=J]
  • A[@I=@J]

The first re-indexes associatively, the second re-indexes by position. You can use the first when I and J have the same labels or values with no duplicates (in general, it is bad practice to have duplicate values in indexes). The second can be used if they are the same length, but have different values.

How do I aggregate an array from a fine grain index (e.g., days) to a coarser index (e.g., months)?

Generally, to aggregate, you will need a mapping (many-to-one) from the fine grain to the coarse grain. This mapping will take the form of an array, indexed by the fine grain (e.g., days), where each value in the array is an element in the coarser index (e.g., months). For illustration, let's call this Day2Month.

Once you have this map, use the Aggregate function. Here is an example:

Index theDate
Variable Revenue_by_date { indexed by theDate }
Variable Date_to_year_map := DatePart( theDate, "Y" )
Variable Year := 2000..2020
Variable Revenue_by_year := Aggregate(Revenue_by_date, Date_to_year_map, theDate, Year )

See Aggregate for more details.

Distributions

  • How do I generate independent distributions across an index, for example, so that Noise := Normal(0,1) is independent for each time point t?
  • How do I define a chance variable so that its uncertainty is correlated with an existing chance variable?

User-Defined Functions

  • How do I create my own User-Defined function?
  • How do I create a custom distribution function?

How do I model XXX?

How can I represent missing values?

The constant Null is useful to represent a missing value.

  • Operators, such as + - * / ^ > >= OR AND, all return Null if one or both operands are Null.
  • Reducing array functions, including, Sum, Product, Max, Min, Average, ignore Null -- that is, they take the Sum, Product, Max, Min, Average of all the numbers that are not Null. (If all values are Null, they return Null.)
  • A Graph view does not display Null values, leaving gaps in lines where the array contains Null.
  • Statistical functions, such as Mean, Sdeviation, Getfract, ignore v values.
  • Probability distributions ignore Null values when generating density functions, cumulative probability functions, and so on. They treat the sample as though the Null didn't exist, and the sample size is the number of non-NULL values.
  • Interpolation functions, LinearInterp, StepInterp, CubicInterp, interpolate over Null between the nearest numerical values.
  • Regression also ignores Null values in the dependent or independent variables.
  • Some other array functions may just return Null if their parameters contain a Null value.

Library functions will similarly ignore NULL values if they rely on built-in functions that do that. If you want your user-defined functions to treat Null differently, you can insert appropriate conditional logic to deal with the missing values in the appropriate fashion. For example:

Variance( MaybeMissing(x), I )
Cumulate( MaybeMissing(x,1), I )

where

Function MaybeMissing( x ; defX : optional )
Definition: if x=Null then if IsNotSpecified(defX) then 0 else defX else X

Debugging

How do I go about debugging a model?

See Debugging Hints

Comments


You are not allowed to post comments.