com.opengamma.analytics.financial.model.option.parameters.BlackFlatCapFloorParameters.java Source code

Java tutorial

Introduction

Here is the source code for com.opengamma.analytics.financial.model.option.parameters.BlackFlatCapFloorParameters.java

Source

/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.analytics.financial.model.option.parameters;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.Validate;

import com.opengamma.analytics.financial.instrument.index.IborIndex;
import com.opengamma.analytics.financial.model.volatility.VolatilityModel;
import com.opengamma.analytics.math.curve.Curve;

/**
 * Class describing the Black volatility surface used in swaption modeling.
 */
public class BlackFlatCapFloorParameters implements VolatilityModel<double[]> {

    /**
     * The volatility curve. The dimension is the expiration. Not null.
     */
    private final Curve<Double, Double> _volatility;
    /**
     * The Ibor index for which the volatility is valid. Not null.
     */
    private final IborIndex _index;

    /**
     * Constructor from the parameter surfaces. The default SABR volatility formula is HaganVolatilityFunction.
     * @param volatility The Black volatility curve.
     * @param index The Ibor index for which the volatility is valid.
     */
    public BlackFlatCapFloorParameters(final Curve<Double, Double> volatility, final IborIndex index) {
        Validate.notNull(volatility, "volatility curve");
        Validate.notNull(index, "Ibor index");
        _volatility = volatility;
        _index = index;
    }

    /**
     * Return the volatility for a time to expiration.
     * @param expiration The time to expiration.
     * @return The volatility.
     */
    public double getVolatility(final double expiration) {
        return _volatility.getYValue(expiration);
    }

    @Override
    /**
     * Return the volatility for a expiration tenor array.
     * @param data An array of one doubles with the expiration.
     * @return The volatility.
     */
    public Double getVolatility(final double[] data) {
        Validate.notNull(data, "data");
        Validate.isTrue(data.length == 1, "data should have one components (expiration)");
        return getVolatility(data[0]);
    }

    /**
     * Gets the Ibor index for which the volatility is valid.
     * @return The index.
     */
    public IborIndex getIndex() {
        return _index;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + _index.hashCode();
        result = prime * result + _volatility.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BlackFlatCapFloorParameters other = (BlackFlatCapFloorParameters) obj;
        if (!ObjectUtils.equals(_index, other._index)) {
            return false;
        }
        if (!ObjectUtils.equals(_volatility, other._volatility)) {
            return false;
        }
        return true;
    }

}