Difference between revisions of "Tutorial: Arrays"
DKontotasiou (talk | contribs) |
DKontotasiou (talk | contribs) |
||
Line 47: | Line 47: | ||
==Continue with model== | ==Continue with model== | ||
+ | In this chapter, you will continue with the model you developed in [[Creating Models|Chapter 4]]. The model from the end of [[Creating Models|Chapter 4]] can be found in the '''Tutorial Models''' directory: | ||
+ | |||
+ | [[File:Chapter_5.1-updated.png]] | ||
+ | |||
+ | ==Creating an index variable== | ||
+ | Index variables define the categories along a dimension of an array. They are represented by index nodes (parallelogram shapes) in the influence diagram. In this example, your decision will be arrayed by '''Car type''' and '''Finance Option''' so you will need to define an index for each of these dimensions. Index variables are generally defined as a ''list of labels'' or a ''list of values'' representing categories in the array. | ||
+ | |||
+ | (For the finance options you will leave out "Loan" for now. It will be added later.) | ||
+ | :'''Index Car_type := <span style="color: blue">['Standard','SUV','Hybrid']</span>''' | ||
+ | :'''Index Finance_option := <span style="color: blue">['Purchase','Lease']</span>''' | ||
+ | |||
+ | [[File:Chapter_5.2-updated.png]] | ||
+ | |||
+ | ---- | ||
+ | As an alternative to selecting '''''List of Labels''''' in the Expression pop-up menu, you can select () and enter the definition directly using Expression syntax. Simply list the values separated by commas and encase them in square brackets [ ]. Note that all text values in Expression syntax must be enclosed in quotes (single or double). | ||
+ | ---- | ||
+ | |||
+ | ==Defining a variable as a table== | ||
+ | Each of the cars has a unique purchase price. You will define '''Car price''' as a '''''Table''''' indexed by '''Car type'''. In this context, a "table" does not necessarily have two dimensions. It can have one (as in this case), two, three or more. | ||
+ | :'''Variable Car_price := <span style="color: blue">Table(Car_type)(22K,40K,24K)</span>''' | ||
+ | |||
+ | [[File:Chapter_5.3-updated.png]] | ||
+ | |||
+ | After you close the Indexes window an ''edit table'' will appear. | ||
+ | |||
+ | Enter the following values for Car price: | ||
+ | |||
+ | :Standard: ''''22K'''' | ||
+ | :SUV: '''''40K'''' | ||
+ | :Hybrid: ''''24K'''' | ||
+ | |||
+ | [[File:Chapter_5.4-updated.png]] | ||
+ | |||
+ | ---- | ||
+ | You can draw influence arrows from index nodes to table variables to pre-populate the Indexes window with the chosen indexes. Influence arrows from index nodes are invisible by default but can be made visible by choosing the '''''Diagram''''' menu and selecting '''''Set diagram style'''''. | ||
+ | ---- | ||
+ | |||
+ | Next you will create a variable for lease payments. Fast Tony lists the following monthly payments for the two-year lease option: | ||
+ | :Standard: ''''$400'''' | ||
+ | :SUV: '''''$700'''' | ||
+ | :Hybrid: ''''$500'''' | ||
+ | |||
+ | '''Lease Payment''' is similar to the '''Car price''' variable. But this time you will enter the definition directly using expression syntax instead of using the edit table. | ||
+ | |||
+ | :'''Variable Lease_payment := <span style="color: blue">Table(Car_type)(400, 700, 500)</span>''' | ||
+ | |||
+ | [[File:Chapter_5.5-updated.png]] | ||
+ | |||
+ | As you can see, Table definitions follow a specific syntax. The '''Table '''declaration is followed by two lists enclosed in parentheses. The first list contains the indexes by which the table will be arrayed. (In this simple example there is only one index but there can be many.) The second list contains the values. | ||
+ | |||
+ | '''It is important for the values to be in the same order as the index categories. '''Recall that the Car type index was defined as: | ||
+ | |||
+ | :'''Index Car_type := <nowiki>[</nowiki><span style="color: blue">'Standard','SUV','Hybrid'</span><nowiki>]</nowiki>''' | ||
+ | |||
+ | which matches the respective order of values in the '''Lease payment '''variable. The same principle applies when there is more than one index. For example, a two dimensional array might look like: | ||
+ | |||
+ | :'''Table (Index_abc, Index_123)''' | ||
+ | :'''('a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3')''' | ||
+ | |||
+ | It is generally easier to use an ''edit table ''when multiple dimensions are involved. | ||
+ | |||
+ | Finally, re-define the existing '''Miles per gallon '''variable as a table indexed by '''Car type'''. The MPG values for Standard, SUV and Hybrid are 28, 23, and 45 respectively. | ||
+ | |||
+ | :'''Variable Mpg := <span style="color: blue">Table(Car_type)(28, 23, 45)</span>''' | ||
+ | |||
+ | Select the '''Miles per gallon '''variable. | ||
+ | |||
+ | Open the Definition field and select [[File:Chapter_5.6-updated.png]] from the Expression popup menu. Enter the new definition as shown above (blue text). | ||
==See Also== | ==See Also== |
Revision as of 08:33, 1 July 2015
This chapter shows you how to:
- Create and define an Index
- Define an array variable as a Table
- Understand principles of array abstraction when combining arrays in an expression
- Use conditional expressions and logical values when defining an array
- Define variables using Expression syntax
- Use local variables to simplify an expression
- Analyze a multi-dimensional result by pivoting indexes
- Reduce an array using subscripts
- Use array reducing functions such as Sum() and NPV()
This chapter demonstrates Intelligent Arrays, one of the most powerful features of Analytica. An array can be generally defined as a variable to which multiple values are assigned simultaneously. For example, in the previous chapter you assigned a list of values to a single variable, Miles per gallon. This is an example of a simple one-dimensional array. When you defined Fuel cost using the Miles per gallon array variable as an input, it also became an array. This demonstrates the concept of array abstraction. Array variables can be used just like ordinary variables in expressions. Whenever you expand an index of the array to include more values, or even add an entirely new index along a new dimension, all dependent variables downstream will be extended automatically! Intelligent arrays allow you to scale your model without making any changes to the design.
This Chapter is rich in content and covers lots of ground. The example model is still somewhat simplified, but it is chosen to be complex enough to demonstrate as many important array concepts as possible. The workflow will be streamlined to allow you to concentrate on the ideas with-out getting bogged down with procedural details. Therefore, you should already be familiar with the basic mechanics of Analytica‘s user interface.
The prerequisite skills include:
- Creating a new model; Opening an existing model; Save; Save As... (See Chapter 1)
- Creating and defining new variables; Entering attributes in Attribute or Object windows; Drawing influence arrows between nodes. (See Chapter 4)
Summarizing variables using Expression syntax
Although Analytica offers a convenient and visually intuitive interface with which to define variables, it is often helpful for users to be able to enter definitions directly using Expression syntax. Every variable type and every functional expression can be represented this way. In addition to its usefulness in building models, expression syntax is a convenient and efficient way to summarize examples in the documentation. This efficiency is evident in the following four-line summary of the Car cost model from the previous chapter:
- Variable MPY := 12K
- Variable MPG := Sequence(20,50,5)
- Variable Fuel_price := 3
- Variable Fuel_cost := MPY*Fuel_price/MPG
In order to expedite the process of building the model, we will summarize new variables in this for- mat throughout the rest of this chapter. It is useful to be familiar with Expression syntax since you will also see it in User Guide and Wiki examples:
- The first term indicates the Class of the object being defined. There is a Class identifier for each type of node found in the node palette. These include: Decision, Variable, Chance, Objective, Module, Index, Constant and Function
- The second term contains the Identifier for the object. (Keep in mind that identifiers are not allowed to contain spaces.)
- A colon followed by a equal sign (:=) is referred to as the assignment operator.
- The expression to the right of the assignment operator defines the object. Conveniently, this portion of the summary line exactly matches the syntax that Analytica requires in the definition field. In fact, you can copy this directly into Analytica if you wish! For your convenience, the portions of the summary line that can be copied into the definition field will be highlighted in blue.
Fast Tony’s Generic Wheels
In this chapter you will extend the Car cost model to address an important decision scenario: the purchase of a new car. There are three cars to choose from: Standard, SUV and Hybrid.
There are also three finance options: Cash purchase, Lease, or Loan. As you can see, Fast Tony has presented you with an array of decision possibilities, a 3 x 3 array to be exact. Your objective is to determine the option with the lowest total cash outflow over a 24-month period, including fuel. (Maintenance costs are covered under warranty as long as Fast Tony’s cousin, Greasy Tony, is on his feet that day.) Ownership equity will be represented as a positive cash flow on the last period.
Continue with model
In this chapter, you will continue with the model you developed in Chapter 4. The model from the end of Chapter 4 can be found in the Tutorial Models directory:
Creating an index variable
Index variables define the categories along a dimension of an array. They are represented by index nodes (parallelogram shapes) in the influence diagram. In this example, your decision will be arrayed by Car type and Finance Option so you will need to define an index for each of these dimensions. Index variables are generally defined as a list of labels or a list of values representing categories in the array.
(For the finance options you will leave out "Loan" for now. It will be added later.)
- Index Car_type := ['Standard','SUV','Hybrid']
- Index Finance_option := ['Purchase','Lease']
As an alternative to selecting List of Labels in the Expression pop-up menu, you can select () and enter the definition directly using Expression syntax. Simply list the values separated by commas and encase them in square brackets [ ]. Note that all text values in Expression syntax must be enclosed in quotes (single or double).
Defining a variable as a table
Each of the cars has a unique purchase price. You will define Car price as a Table indexed by Car type. In this context, a "table" does not necessarily have two dimensions. It can have one (as in this case), two, three or more.
- Variable Car_price := Table(Car_type)(22K,40K,24K)
After you close the Indexes window an edit table will appear.
Enter the following values for Car price:
- Standard: '22K'
- SUV: 40K'
- Hybrid: '24K'
You can draw influence arrows from index nodes to table variables to pre-populate the Indexes window with the chosen indexes. Influence arrows from index nodes are invisible by default but can be made visible by choosing the Diagram menu and selecting Set diagram style.
Next you will create a variable for lease payments. Fast Tony lists the following monthly payments for the two-year lease option:
- Standard: '$400'
- SUV: $700'
- Hybrid: '$500'
Lease Payment is similar to the Car price variable. But this time you will enter the definition directly using expression syntax instead of using the edit table.
- Variable Lease_payment := Table(Car_type)(400, 700, 500)
As you can see, Table definitions follow a specific syntax. The Table declaration is followed by two lists enclosed in parentheses. The first list contains the indexes by which the table will be arrayed. (In this simple example there is only one index but there can be many.) The second list contains the values.
It is important for the values to be in the same order as the index categories. Recall that the Car type index was defined as:
- Index Car_type := ['Standard','SUV','Hybrid']
which matches the respective order of values in the Lease payment variable. The same principle applies when there is more than one index. For example, a two dimensional array might look like:
- Table (Index_abc, Index_123)
- ('a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3')
It is generally easier to use an edit table when multiple dimensions are involved.
Finally, re-define the existing Miles per gallon variable as a table indexed by Car type. The MPG values for Standard, SUV and Hybrid are 28, 23, and 45 respectively.
- Variable Mpg := Table(Car_type)(28, 23, 45)
Select the Miles per gallon variable.
Open the Definition field and select from the Expression popup menu. Enter the new definition as shown above (blue text).
See Also
Tutorial Chapter 4 <- | Working with Arrays (Tables) | -> Creating the Party Problem Model |
Enable comment auto-refresher