Faiza Nasir
Faiza Nasir

Reputation: 59

Calculating compound annual growth rate (CAGR) using C#

I searched everywhere to find a way to calculating compound annual growth rate (CAGR) in c# but seems like not many people have worked over. SO posting it here so that it could help someone someday.

Upvotes: 0

Views: 929

Answers (2)

Faiza Nasir
Faiza Nasir

Reputation: 59

I made it with dictionary and the result is totally like this link. What is the formula for calculating compound annual growth rate (CAGR) in Excel?[^]

public static void CalculateCAGR() {
    Dictionary<DateTime, int> lst = new Dictionary<DateTime, int>();
    lst.Add(new DateTime(2011, 11, 05), 100);
    lst.Add(new DateTime(2012, 11, 05), 115);
    lst.Add(new DateTime(2013, 11, 05), 118);
    lst.Add(new DateTime(2014, 11, 05), 121);
    lst.Add(new DateTime(2015, 11, 05), 130);
    lst.Add(new DateTime(2016, 11, 05), 144);
    lst.Add(new DateTime(2017, 11, 05), 135);
    lst.Add(new DateTime(2018, 11, 05), 132);
    lst.Add(new DateTime(2019, 11, 05), 138);
    lst.Add(new DateTime(2020, 03, 16), 161);

    var keys = new List<DateTime>(lst.Keys);

    DateTime zeroTime = new DateTime(1, 1, 1);
    double remainderInDays = 0;
    double remainder = 0;
    double yearsResult = 0;
    int numberOfCompletedYears = 0;
    TimeSpan yearSpan;
    double endingBeginning = 0;
    double? result = 0;
    double? CAGR = 0;

    var firstElement = lst.FirstOrDefault();
    int beginningValue = firstElement.Value;
    int lastValue = lst.Values.Last();

    for (int i = 0; i < keys.Count - 1; i++)
    {
        remainderInDays = (keys[i + 1] - keys[i]).TotalDays;
    }

    for (int i = 0; i < keys.Count - 1; i++)
    {
        yearSpan = (keys[i + 1] - keys[i]);
        numberOfCompletedYears += (zeroTime + yearSpan).Year - 1;
    }

    remainder = remainderInDays / 365;  //Converted to Years
    endingBeginning = 1.0*lastValue / beginningValue;
    yearsResult = 1/(numberOfCompletedYears + remainder);
    result = (Math.Pow(endingBeginning, yearsResult)) - 1;
    CAGR = result * 100;
}

Upvotes: 2

Georgi Georgiev
Georgi Georgiev

Reputation: 70

CAGR formula is: CAGR = ( EV / SV)1 / n - 1

where:

EV = Investment's ending value

SV = Investment's starting value

n = Number of years investment

You can apply the formula in C# like this (Console Appliaction Example).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tst
{
   class Program
   {
       static void Main(string[] args)
       {
          //Your code goes here
          int years = 5; //number of years
          double BeginningValue =  200000;//Begging value of investment in dollars
          double EndingValue = 450000;//Ending value of investment in dollars
        
          double total = (Math.Pow((EndingValue / BeginningValue), (double)1 / 
          (double)years) -1)*100;
          Console.WriteLine(total.ToString());
          Console.ReadLine();
       }
   }
}

Upvotes: 1

Related Questions