`ObjectAnimator`

to create animations on arbitrary properties on objects. The only restriction is that the object must define a setter method for the property that should be animated. `ObjectAnimator`

extends the abstract `Animator`

class which forms the basis of the Property Animation System. The system defines three other Animator classes, the `AnimatorSet`

, the `ValueAnimator`

and the `TimeAnimator`

. In practice you will rarely have the need for the latter two and the `ObjectAnimator`

together with the `AnimatorSet`

should be sufficient. In this post I will show you how to programmatically create animations using these two classes. The `ValueAnimator`

and the `TimeAnimator`

will be the topic of a later post.
The `ObjectAnimator`

class defines a collection of static factory methods that allow you to create standard object animations in a single line of code. Recall the example given in the introduction to property animations. We created an animation that rotated a view around its vertical axis. In order to achieve the same result programmatically, without the need to create a separate XML resource file containing the animation. Simply replace the `flipOnVertical`

method with the following code.

public void flipOnVertical(View view) { View image = findViewById(R.id.some_image); ObjectAnimator anim = ObjectAnimator.ofFloat(image, "rotationY", 0.0f, 360.0f); anim.setDuration(300); anim.start(); }

As you can see, the code is not much longer than before. You will find this to be the case in most animations and you will rarely find a need for writing animation resource files. The important method in the code above is the static method `ofFloat`

, defined by `ObjectAnimator`

. The method is a simple static factory method that creates an `ObjectAnimator`

for you. The first argument of the method is the object that should be animated. The second argument id the name of the parameter that should be changed inside the object. The object will need to have a setter method for the property in order for the animation to work. This means for the property *“rotationY”* there has to be a method `setRotationY`

defined by the image object. This method should accept `float`

values for is argument. The values that follow the property name are the start and end values for the animation. The `ofFloat`

method allows you to specify not only two values but an arbitrary number of intermediate values. The `ObjectAnimator`

will then take all the values in sequence and arrange them at equal time intervals and interpolate between them. You can think of the values as way-points that the animator will pass at regular time intervals. I modify the previous example to illustrate this.

public void disappearBox(View view){ View image = findViewById(R.id.policebox); ObjectAnimator anim = ObjectAnimator.ofFloat(image, "alpha", 1.0f, 0.25f, 0.75f, 0.15f, 0.5f, 0.0f); anim.setDuration(5000); anim.start(); }

I have changed the property name to *“alpha”* and created a list of alpha values that alternate between decreasing and increasing. The duration of the animation has been changed to 5s=5000 milliseconds. This is done by calling the `setDuration`

method. I have also changed the image to be animated to depict an old fashioned UK police box and updated the method name to `disappearBox`

. The `onClick`

callback in the layout file should be changed accordingly. The resulting animation can be seen on the right.

You can set interpolators to use with the animation, just as you could with view animations. The interpolators of the View Animation System can be reused for this purpose. By default the Animator uses the `AccelerateDecelerateInterpolator`

. To make the example above more realistic just add the following line before starting the animation.

anim.setInterpolator(new LinearInterpolator());

Available Interpolators are

`LinearInterpolator`

`AccelerateDecelerateInterpolator`

`AccelerateInterpolator`

`AnticipateInterpolator`

`AnticipateOvershootInterpolator`

`BounceInterpolator`

`CycleInterpolator`

`DecelerateInterpolator`

`LinearInterpolator`

`OvershootInterpolator`

For details on the interpolators, and on how to write your own, see the post on interpolators.

Up to now, I have just described how to use the `ObjectAnimator`

to animate a single property of an object. In many cases you want to modify multiple properties at the same time. This can be done using animator sets. The `AnimatorSet`

class allows you to specify multiple animation to run in parallel. You can define an animator set inside an XML resource file using the `animatorSet`

tag. But the real power of the animator set only comes to light when you use the animator set programmatically. You can add `Animator`

objects and play them in a specific order. In addition, all the animators can modify properties on a different objects. In this way you can create an animation that animates multiple views at the same time. Let’s extend the example above and create a second animation.

public void disappearBox(View view) { View policebox = findViewById(R.id.policebox); View policeboxlight = findViewById(R.id.policeboxlight); ObjectAnimator anim1 = ObjectAnimator.ofFloat(policebox, "alpha", 1.0f, 1.0f, 0.25f, 0.75f, 0.15f, 0.5f, 0.0f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(policeboxlight, "alpha", 0.0f, 1.0f, 0.0f, 0.75f, 0.0f, 0.5f, 0.0f);

The second animation acts on a different image where it also modifies the alpha value. In order to play both of these animations together, you have to create an animator set. You can simply create one using `new`

.

AnimatorSet animSet = new AnimatorSet();

Now you have to specify which animations to include in the set and in what order to play them. You can use the `playTogether`

and `playSequentially`

methods. In our case, we want to play both animations at the same time, so we use `playTogether`

.

animSet.playTogether(anim1, anim2);

Both methods take an arbitrary number of `Animator`

objects. `playTogether`

also accepts a `Collection<Animator>`

and `playSequentially`

accepts a `List<Animator>`

. The difference arises because the order is matters when animations are played sequentially but doesn’t when they are played together. The rest of the code follows the same pattern as before. We set the duration and the interpolator on the animator set and then start the animation.

animSet.setDuration(5000); animSet.setInterpolator(new LinearInterpolator()); animSet.start(); }

The result of our little animation can be seen on the right. In the next post on property animations I will talk more about animator sets and using the `AnimatorSet.Builder`

class to choreograph animations in a more complex way.

The code for this tutorial can be found on github.

Follow the author

Image of the police box is distributed under the Creative Commons Attribution-Share Alike 3.0 Unported license. Copyright Dan Sellers.

]]>`std::vector`

with numbers. While random number generators can easily be used to create a random sequence, some algorithms, notably the low discrepancy sequence generators, can only be used to generate sequences and not individual random numbers.
QuantLib defines four pseudo-random number generators with uniform distribution. These are the Mersenne Twister 19937, the L’Ecuyer generator with added Bays-Durham shuffle, Knuth’s random number generator and Luescher’s Luxury random number generator. The last generator in this list is not implemented inside QuantLib but is a wrapper for the random number generator implemented in the **Boost Random** library. There are two versions of the luxury random number generator, Ranlux 3 and Ranlux 4. The second generator is slower than the first but provides more “luxury” (see M. Luescher, Computer Physics Communications, **79** (1994) pp 100-110). The number generators are defined in the following classes.

class MersenneTwisterUniformRng; class LecuyerUniformRng; class KnuthUniformRng; class Ranlux3UniformRng; class Ranlux4UniformRng;

All of these generators have constructors that take an optional initial seed as argument.

explicit MersenneTwisterUniformRng(unsigned long seed = 0); explicit LecuyerUniformRng(long seed = 0); explicit KnuthUniformRng(long seed = 0); explicit Ranlux3UniformRng(Size seed = 19780503u); explicit Ranlux4UniformRng(Size seed = 19780503u);

As you can see, different generators use slightly different integral types as seeds. The types depend on the internals of the generators. The default seed for the Mersenne Twister, L’Ecuyer and Knuth generators will cause a seed to be generated based on the system clock. The seed is calculated from the return value of the `time()`

function using the Mersenne Twister. All this is carried out inside the `SeedGenerator`

class.

While the details of the seeding might not be important to you there is one thing to take away from this. The seed is calculated in a deterministic way from the result of the `time()`

function but this function has a granularity of one second. If you run the same code multiple times during one second, the random number generator is likely to produce the same values.

`MersenneTwisterUniformRng`

provides another constructor that takes a vector of seeds which will initialise the internal states of the Mersenne Twister.

explicit MersenneTwisterUniformRng( const std::vector& seeds);

Each of the four random number generators has a method to get the next random number. The random number returned is a `Real`

number between, but not including, 0 and 1.

Sample<Real> next() const;

The return type is actually not just a simple `Real`

but a `Real`

value together with a weight wrapped inside the `Sample`

struct.

template <class T> struct Sample { typedef T value_type; Sample(const T& value, Real weight) : value(value), weight(weight) {} T value; Real weight; };

This struct is used throughout the QuantLib random number module to represent random numbers. For all samples returned by the random number generators, the `weight`

property is set to 1.0. To illustrate how to use the random number generators to create and print a sequence of random numbers, let’s look at a simple example.

MersenneTwisterUniformRng generator(12345); for (int i=0; i<100; ++i) { std::cout << generator.next().value << std::endl; }

The code above will create a Mersenne Twister and seed it with the number 12345. It will then create and print out 100 numbers.

You can easily create a random sequence from a random number generator with the generic `RandomSequenceGenerator`

. This class is templated with a random number generator class so it can use any generator to create the sequence.

template<class RNG> class RandomSequenceGenerator;

The class defines a type called `sample_type`

that defines the return type of the sequence generator. In general all sequence generators define the sample_type in the following way.

typedef Sample<std::vector<Real> > sample_type;

The generic random sequence generator has two constructors. The first argument of both constructors is the dimension, i.e. the length of the sequence that the generator will return. The second parameter is either a random number generator of the type specified by `RNG`

or a seed number that is used to initialise the random number generator. The seed is optional and defaults to zero if not specified.

RandomSequenceGenerator::RandomSequenceGenerator (Size dimensionality, const RNG& rng); RandomSequenceGenerator::RandomSequenceGenerator (Size dimensionality, BigNatural seed = 0);

Two methods allow access to the sequence.

const sample_type& nextSequence() const; const sample_type& lastSequence() const;

The method `nextSequence`

generates a new sequence with a length specified by dimensionality and returns a reference to it. Repeated access to the last sequence, without generating it again, is granted by `lastSequence`

. The following example shows how the sequence generator might be used.

RandomSequenceGenerator<MersenneTwisterUniformRng> generator(100, 12345); std::vector<Real> values = generator.nextSequence().value; for (int i=0; i<values.size(); ++i) { std::cout << values[i] << std::endl; }

All the previous random number and random sequence generators produce uniformly distributed random numbers. In many cases random numbers with different probability distributions are needed. The most common distribution is the Gaussian distributions. QuantLib provides two classes that generate Gaussian distributed random numbers from uniformly distributed random numbers. The `BoxMullerGaussianRng`

uses the Box Muller transform that generates pairs of normally distributed values from pairs of uniformly distributed values. Because the Box Muller transform needs to evaluate the square root and the logarithm for every number produced it can be slow for some applications. An alternative is the application of the central limit theorem, implemented in `CLGaussianRng`

. This generator uses the fact that the sum of 12 uniformly random numbers is already a good approximation for a normal distributed random variable. Both classes are templated with the type of the underlying random number generator.

template <class RNG> class BoxMullerGaussianRng; template <class RNG> class CLGaussianRng;

Both classes are simply constructed by passing the random number generator to the constructor.

explicit BoxMullerGaussianRng::BoxMullerGaussianRng (const RNG& uniformGenerator); explicit CLGaussianRng::CLGaussianRng (const RNG& uniformGenerator);

In addition, both classes follow the same pattern as the uniform random number generators for accessing the numbers and define the `next`

method.

Sample<Real> next() const;

In this way, both Gaussian generators can be plugged into the `RandomSequenceGenerator`

in the same way as any of the uniform generators. The following example will illustrate this.

typedef BoxMullerGaussianRng<MersenneTwisterUniformRng> BoxMuller; RandomSequenceGenerator<BoxMuller> generator(BoxMuller(100, 12345)); std::vector<Real> values = generator.nextSequence().value; for (int i=0; i<values.size(); ++i) { std::cout << values[i] << std::endl; }

The code above creates a vector filled with 100 normally distributed values. Inside the loop the values are then printed out.

For distributions other than Gaussian distributions QuantLib defines a general class called `InverseCumulativeRng`

. The class creates random numbers with an arbitrary distribution from its inverse cumulative distribution. Assume that the desired probability distribution of the random numbers is given by the function \(f(x)\). Then the cumulative distribution function is then given by

$$F(x) = \int_{-\infty}^x f(x’) dx’$$

All that is needed is a function object that evaluates the inverse of the cumulative distribution \(F^{-1}(x)\). The declaration of the `InverseCumulativeRng`

class looks like this.

template <class RNG, class IC> class InverseCumulativeRng { public: typedef Sample<Real> sample_type; typedef RNG urng_type; explicit InverseCumulativeRng(const RNG& uniformGenerator); sample_type next() const; };

The class `IC`

must have a default constructor and must overload the function operator to return the inverse cumulative distribution.

Real IC::operator()(Real x);

The following example may be helpful in illustrating how to use the `InverseCumulativeRng`

class. Let’s assume that you want to create a random variable with a probability distribution given by the logistic distribution with mean zero and scale parameter 1,

$$f(x) = \frac{1}{\left(e^{x/2} + e^{-x/2}\right)}$$

Then the cumulative distribution function is given by

$$F(x) = \int_{-\infty}^x \frac{1}{\left(e^{x’/2} + e^{-x’/2}\right)} dx’ = \frac{1}{1+e^{-x}}$$

The inverse of the function can be found by solving \(y=F(x)\) for \(x\) which results in

$$F^{-1}(y) = \mathrm{ln}\left(\frac{y}{1-y}\right)$$

All we need to do is to create a class that evaluates this function.

class ICLogistics { public: Real operator()(Real y) { return log(y/(1-y)); } };

Now we can define the following random number generator.

typedef InverseCumulativeRNG< MersenneTwisterUniformRng, ICLogistics > LogisticsRNG;

In the example above we can now simply replace `BoxMuller`

with `LogisticsRNG`

to produce a sequence of random numbers with the logistics distribution.

Follow the author

]]>