Cogito Learning » 2013 » December Professional IT Courses Wed, 02 Dec 2015 11:11:23 +0000 en-US hourly 1 Android Property Animations: Programming Animations Thu, 05 Dec 2013 10:23:24 +0000 Read more]]> In the previous post on property animations I introduced the 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.

Object Animator

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(;
  ObjectAnimator anim 
    = ObjectAnimator.ofFloat(image, "rotationY", 0.0f, 360.0f);

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(;
  ObjectAnimator anim 
    = ObjectAnimator.ofFloat(image, "alpha", 
        1.0f, 0.25f, 0.75f, 0.15f, 0.5f, 0.0f);  
Using way-points in property animations to make a police box disappear and reappear.

Using way-points in property animations to make a police box disappear and reappear before disappearing completely.

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.

Setting Interpolators

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.

Animator Set

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(;
  View policeboxlight = findViewById(;
  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.setInterpolator(new LinearInterpolator());
Using AnimatorSet to add a pulsating light to the disappearing policebox.

Using AnimatorSet to add a pulsating light to the disappearing policebox.

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.

]]> 1
QuantLib: Random Number Generators Mon, 02 Dec 2013 13:17:43 +0000 Read more]]> QuantLib uses random numbers in many places, such as in Pricing Engines to calculate the NPV of an instrument, in the Brownian Generator that is used in market-model simulations and in the Monte Carlo Model for path samples. In financial calculations the quality of the random number generator can be important and QuantLib provides a number of different generators that generate pseudo-random numbers and quasi-random numbers with uniform distribution and mechanisms to transform these to arbitrary probability distributions. The library distinguishes between Random Number Generators and Random Sequence Generators. Random number generators provide individual numbers while random sequence generators fill a 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.

Uniform Random Number Generators

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);

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

Generic Random Sequence Generator

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.

  (Size dimensionality, const RNG& rng);
  (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.

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

Gaussian and Other Distributions

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)); 

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 {
    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 {
    Real operator()(Real y) {
      return log(y/(1-y));

Now we can define the following random number generator.

typedef InverseCumulativeRNG<
  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

]]> 0