Numeric variables

Numerical variables are defined byNumericVarand contains discrete or continuous numerical values stored as double precision float values. Numerical variables offers value and label representations. All other representations can be used, but with caution since it can alter the content. For example index representation truncates values to the biggest integer which contains the values, however the index setter sets a correct value since an integer can be converted to a double.

Various builders

Numeric variables can be built in various was and are handy shortcuts for various scenarios.

Empty variables

// builds a variable with no data
Var empty1 = NumericVar.empty();

// builds a variable of a given size which contains only missing data
Var empty2 = NumericVar.empty(100);

Scalar variables

Var scalar = NumericVar.scalar(Math.PI);

Sequence of values

// a sequence of numbers, starting from 0, ending with 5 with step 1
Var seq1 = NumericVar.seq(5);

// a sequence of numbers, starting from 1, ending with 5 with step 1
Var seq2 = NumericVar.seq(1, 5);

// a sequence of numbers starting at 0, ending at 1 with step 0.1
Var seq3 = NumericVar.seq(0, 1, 0.1);

Variables filled with the same number

// build a variable of a given size which contains only zeros
Var fill1 = NumericVar.fill(5);

// builds a variable of a given size which contains only ones
Var fill2 = NumericVar.fill(5, 1);

Copy from another source

// numeric variable which contains the values copied from another variable
Var copy1 = NumericVar.copy(seq1);

// numeric variable with values copied from a collection
Normal normal = new Normal();
        List<Double> list1 = DoubleStream.generate(normal::sampleNext)
                .limit(10)
                .boxed()
                .collect(Collectors.toList());
Var copy2 = NumericVar.copy(list1);

// numeric variable with values copied from a double array
Var copy3 = NumericVar.copy(1, 3, 4.0, 7);

// numeric variable with values copied from an int array
Var copy4 = NumericVar.copy(1, 3, 4, 7);

Generated using a lambda function

// numeric variables with values generated as the sqrt of the row number
Var from1 = NumericVar.from(10, Math::sqrt);

// numeric variable with values generated using a function which receives a row value
// as parameter and outputs a double value; in this case we generate values as 
// a sum of the values of other two variables
Var from2 = NumericVar.from(4, row -> copy3.getValue(row) + copy4.getValue(row));

// numeric variable with values generated from values of another variable using
// a transformation provided via a lambda function
Var from3 = NumericVar.from(from1, x -> x + 1);

Wrapper around a double array

This builder creates a new numeric variable instance as a wrapper around a double array of values. Notice that it is not the same as the copy builder, since in the wrapper case any change in the new numerical variable is reflected also in the original array of numbers. In the case of the copy builder this is not true, since the copy builder (as its name implies) creates an internal copy of the array.

double[] src = new double[] {1, 4, 19, 23, 5};
Var wrap1 = NumericVar.wrap(src);

Inspect a numerical variable

Most of the objects which contains information implements the Printable interface. This interface allows one to display a summary of the content of the given object. This is the case also with the numerical variables. Additionally, the numerical variables implements also two other methods, one which displays all the values and another one which displays only the first values.

// build a numerical variable with values as the sqrt
// of the first 30 integer values
Var x = NumericVar.from(30, Math::sqrt).withName("x");

// print the first 5 values
x.printLines(5);

// print all values of the variable
x.printLines();

// print a summary of the content of the variable
x.printSummary();

which has the following output:

 [0] 0.0 [2] 1.4142135623730951 [4] 2.0
 [1] 1.0 [3] 1.7320508075688772        


 [0]                0.0 [10] 3.1622776601683795 [20]   4.47213595499958
 [1]                1.0 [11]    3.3166247903554 [21]   4.58257569495584
 [2] 1.4142135623730951 [12] 3.4641016151377544 [22]   4.69041575982343
 [3] 1.7320508075688772 [13]  3.605551275463989 [23]  4.795831523312719
 [4]                2.0 [14] 3.7416573867739413 [24]  4.898979485566356
 [5]   2.23606797749979 [15]  3.872983346207417 [25]                5.0
 [6]  2.449489742783178 [16]                4.0 [26] 5.0990195135927845
 [7] 2.6457513110645907 [17]  4.123105625617661 [27]  5.196152422706632
 [8] 2.8284271247461903 [18]  4.242640687119285 [28]  5.291502622129181
 [9]                3.0 [19]  4.358898943540674 [29]  5.385164807134504

> printSummary(var: x)
name: x
type: NUMERIC
rows: 30, complete: 30, missing: 0
   Min. : 0.000
1st Qu. : 2.691
 Median : 3.807
   Mean : 3.554
2nd Qu. : 4.663
   Max. : 5.385

results matching ""

    No results matching ""