Evaluation of Risk in Investments in C++

C++Server Side ProgrammingProgramming

In this problem, we are given two arrays each denoting an investment plan. Our task is to perform the evaluation of Risk in Investment and find which of the two investments is more promising.

Both the investments I1[][] and I2[][] has a set of outcomes and the probability of that investment outcome.

Using these values, we need to find the risk in each investment and then print the better investment out of the two investments.

For this, we will be using statistical mathematics and find some values that will help us conclude to the better investment.

We will find these values,

  • Mean or average amount form the investment, the sum of product of investment outcome and probability.
  • Deviation on the amount earned

We will find the value of,         

standard Deviation / mean of investment about

The investment with a lesser value of S.D. / mean is the result.

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef pair<float,float> Data;
typedef vector Vector;

float totalProdProbOutcome(const Vector & v)
{
   float sum = 0;
   for ( auto i : v) {
      sum += i.first * i.second;
   }
   return sum;
}

float totalProb(const Vector & v) {
   
   float sum = 0.0;
   for ( auto i : v) {
      sum += i.second;
   }
   return sum;
}

float CalcMeanVal(const Vector & v) {
   
   return totalProdProbOutcome(v) / totalProb(v);
}

float calcStdDevi(const Vector & v)
{
   float mean = CalcMeanVal(v);
   float sum = 0;
   
   for (auto i: v)
      sum += (i.first-mean)* (i.first-mean)*i.second;
   
   return sqrt(sum/totalProb(v));
}

int main() {
   
   Vector A = { {450,0.3}, {250,0.4}, {100,0.2}, {300,0.1}};
   Vector B = { {300,0.2}, {150,0.5}, {500,0.3}};

   float meanA = CalcMeanVal(A);
   float meanB = CalcMeanVal(B);
   float SdA = calcStdDevi(A);
   float SdB = calcStdDevi(B);
   
   if( (SdA / meanA) > (SdB / meanB))
      cout<<"Investment A is Better investment.\n";
   else
      cout<<"Investment B is better investment.\n";
   return 0;
}

Output −

Investment B is better investment.
raja
Published on 22-Jan-2021 17:42:24
Advertisements