コード例 #1
0
        /// <summary>
        /// Assumes returns are normally distributed.
        /// WARNING: Not very accurate. Intended for educational use and as a baseline for comparison to other models.
        /// </summary>
        /// <param name="returnArray">Historical returns from which VaR is to be calculated. The last value, with the highest index is assumed to be the most recent data point.</param>
        /// <param name="windowLength">Length of the VaR window. The number of historical returns that will be used to calculate the VaR.</param>
        /// <param name="confidenceLevel">VaR confidence level. 95% and 99% are typical values. If confidenceLevel is 95% then we expect 95% of days to be better (have a more positive return) than the VaR.</param>
        public static double NormalValueAtRisk(double[] returnArray, int windowLength, double confidenceLevel)
        {
            double[] da = new double[windowLength];
            for (int i = 0; i < windowLength; i++)
            {
                da[i] = returnArray[returnArray.Length - windowLength + i];
            }
            double mean = Moments.Mean(da);
            double standardDeviation = Moments.StandardDeviation(da);
            double var = Distributions.NormalCumulativeDistributionFunctionInverse(1 - confidenceLevel, mean, standardDeviation);

            return(-var); //By convention VaR is quoted as a positive value, even though it corresponds to a loss.
        }
コード例 #2
0
ファイル: Regression.cs プロジェクト: mpvyard/QuantRiskLib
        /// <summary>
        /// Returns the slope of an OLS regression analysis.
        /// </summary>
        /// <param name="yData">Data for the dependent variable, or regressand.</param>
        /// <param name="xData">Data for the independent variable, or regressor.</param>
        /// <param name="length">Window length. Method uses the most recent n points, n = length.</param>
        public static double Beta(double[] yData, double[] xData, int length)
        {
            double[] xDataSubSet = Tools.MostRecentValues(xData, length);
            double[] yDataSubSet = Tools.MostRecentValues(yData, length);

            double mX = Moments.Mean(xData);
            double mY = Moments.Mean(yData);
            double sumXY = 0, sumXX = 0;

            for (int i = 0; i < length; i++)
            {
                sumXY += xDataSubSet[i] * yDataSubSet[i];
                sumXX += xDataSubSet[i] * xDataSubSet[i];
            }

            return((sumXY - length * mY * mX) / (sumXX - length * mX * mX));
        }