# Evaluation of Risk in Investments

Given two investment options A and B, we have tho find the less risky investment of the two. The two investments A and B are each represented by an array. Each element in the array is a probable investment outcome. Thus each element in the array is a pair of two values. The first value is the amount of money received and the second value is the probability that this money can be received. For instance if A = [ (100,0.1), (200,0.2) (300,0.7) ], it means that there is 10 % probability to earn Rs 100, 20% probability to earn Rs 200 and 70% chance to earn Rs 300 from investment A.

We have to use a statistical approach to solve the problem. For each investment, we first calculate an average amount of money that can be earned from it. Secondly, we also calculate the standard deviation in the money earned. Then we need to normalize this standard deviation by dividing it by the mean.

Each probable outcome is an observation. The probability for each amount of money is its frequency. Since the observations are given with frequencies we need to apply the following formulas to calculate the mean and standard deviation

If denotes the set of observations .

Mean =

Standard deviation

Let us take an example to demonstrate how to apply this method.

Example

Input: A = [(0,0.1), (100,0.1), (200,0.2), (333,0.3), (400,0.3) ] B = [ (100,0.1), (200,0.5), (700,0.4) ] Explanation: Mean Investment of A Index | Outcome | Probability | Probability*Outcome (i) (xi) (fi) xi*fi ---------------------------------------------------------- 1 0 0.1 0 2 100 0.1 10 3 200 0.2 40 4 333 0.3 99.9 5 400 0.3 120 ---------------------------------------------------------- Total: 1.0 269.1 Mean = 269.1/1 = 269.1 Mean Investment of B: Index | Outcome | Probability | Probability*Outcome (i) (xi) (fi) xi*fi ---------------------------------------------------------- 1 100 0.1 10 2 200 0.5 100 3 700 0.4 280 ---------------------------------------------------------- Total: 1.0 390 Mean = 390/1 = 390.1 Standard Deviation of A Mean = 269.1 Index | Outcome | Probability | (xi-Mean)^2 | A*fi (i) (xi) (fi) (A) ---------------------------------------------------------- 1 0 0.1 72414.81 7241.481 2 100 0.1 28594.81 2859.481 3 200 0.2 4774.81 954.962 4 333 0.3 4083.21 1224.963 5 400 0.3 17134.81 5140.443 ---------------------------------------------------------- Total: 1.0 17421.33 Standard Deviation = sqrt(17421.33/1) = 131.989 Normalized Standard Deviation = 131.989/269.1 = 0.49 Standard Deviation of B Mean = 390.1 Index | Outcome | Probability | (xi-Mean)^2 | A*fi (i) (xi) (fi) (A) ---------------------------------------------------------- 1 100 0.1 84158.01 8415.801 2 200 0.5 36138.01 18069.005 3 700 0.4 96100.00 38440.000 ---------------------------------------------------------- Total: 1.0 64924.801 Standard Deviation = sqrt(64924.801/1) = 254.803 Normalized Standard Deviation: 254.803 / 390.1 = 0.65 Since Investment A has lesser normalized standard deviation, it is less risky. Input: A = [(0,0.1), (100,0.1), (200,0.2), (333,0.3), (400,0.3) ] B = [ (100,0.1), (200,0.5), (700,0.4) ] Explanation: For Investment A Average: 269.9 Standard Deviation: 131.987 Normalised Std: 0.489024 For Investment B Average: 258.333 Standard Deviation: 44.8764 Normalised Std: 0.173715 Investment B is less risky

The CPP implementation of the problem is given below

## C++

`// C++ code for above approach ` `#include <iostream> ` `#include <vector> ` `#include <algorithm> ` `#include <cmath> ` `using` `namespace` `std; ` ` ` `// First Item in the pair is the ` `// value of observation (xi). ` `// Second Item in the pair is ` `// the frequency of xi (fi) ` `typedef` `pair<` `float` `,` `float` `> Data; ` ` ` `// Vector stores the observation ` `// in pairs of format (xi, fi), ` `// where xi = value of observation ` `typedef` `vector<Data> Vector; ` ` ` `// Function to calculate the ` `// summation of fi*xi ` `float` `sigma_fx(` `const` `Vector & v) ` `{ ` ` ` `float` `sum = 0; ` ` ` `for` `( ` `auto` `i : v) { ` ` ` `sum += i.first * i.second; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Function to calculate summation fi ` `float` `sigma_f(` `const` `Vector & v) ` `{ ` ` ` `float` `sum = 0.0; ` ` ` `for` `( ` `auto` `i : v) { ` ` ` `sum += i.second; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Function to calculate the mean ` `// of the set of observations v ` `float` `calculate_mean(` `const` `Vector & v) ` `{ ` ` ` `return` `sigma_fx(v) / sigma_f(v); ` `} ` ` ` `// Function to calculate the std ` `// deviation of set of observations v ` `float` `calculate_std(` `const` `Vector & v) ` `{ ` ` ` `// Get sum of frequencies ` ` ` `float` `f = sigma_f(v); ` ` ` ` ` `// Get the mean of the set ` ` ` `// of observations ` ` ` `float` `mean = sigma_fx(v) / f; ` ` ` ` ` `float` `sum = 0; ` ` ` ` ` `for` `(` `auto` `i: v) { ` ` ` `sum += (i.first-mean)* ` ` ` `(i.first-mean)*i.second; ` ` ` `} ` ` ` ` ` `return` `sqrt` `(sum/f); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `Vector A = { {0,0.1}, {100,0.1}, ` ` ` `{200,0.2}, {333,0.3}, {400,0.3}}; ` ` ` `Vector B = { {100,0.1}, {200,0.5}, {700,0.4}}; ` ` ` ` ` `float` `avg_A = calculate_mean(A); ` ` ` `float` `avg_B = calculate_mean(B); ` ` ` `float` `std_A = calculate_std(A); ` ` ` `float` `std_B = calculate_std(B); ` ` ` ` ` ` ` `cout << ` `"For Investment A"` `<< endl; ` ` ` `cout << ` `"Average: "` `<< avg_A << endl; ` ` ` `cout << ` `"Standard Deviation: "` `<< ` ` ` `std_A << endl; ` ` ` `cout << ` `"Normalised Std: "` `<< ` ` ` `std_A / avg_A << endl; ` ` ` `cout << ` `"For Investment B"` `<< endl; ` ` ` `cout << ` `"Average: "` `<< avg_B << endl; ` ` ` `cout << ` `"Standard Deviation: "` `<< ` ` ` `std_B << endl; ` ` ` `cout << ` `"Normalised Std: "` `<< std_B / ` ` ` `avg_B << endl; ` ` ` ` ` `(std_B/avg_B) < (std_A/avg_A) ? cout << ` ` ` `"Investment B is less risky\n"` `: ` ` ` `cout << ` `"Investment A is less risky\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

For Investment A Average: 269.9 Standard Deviation: 131.987 Normalised Std: 0.489024 For Investment B Average: 390 Standard Deviation: 254.755 Normalised Std: 0.653217 Investment A is less risky

References

https://www.statcan.gc.ca/edu/power-pouvoir/ch12/5214891-eng.htm

std::accumulate cppreference.com

## Recommended Posts:

- Expression Evaluation
- Horner's Method for Polynomial Evaluation
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Calculate the number of set bits for every number from 0 to N
- Number of ways to distribute N Paper Set among M students
- Diameter of a Binary Indexed Tree with N nodes
- Program for nth Fuss–Catalan Number
- Check if a number is Euler Pseudoprime
- Maximum prime moves to convert X to Y
- Minimum difference between any two primes from the given range
- Sum of internal angles of a Polygon
- Find the number of squares inside the given square grid
- Longest subsequence with a given AND value
- Number of ways to split a binary number such that every part is divisible by 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.