Evaluation of Risk in Investments | C ++ code for above approach ; First Item in the pair is the value of observation ( xi ) . Second Item in the pair is the frequency of xi ( fi ) ; Vector stores the observation in pairs of format ( xi , fi ) , where xi = value of observation ; Function to calculate the summation of fi * xi ; Function to calculate summation fi ; Function to calculate the mean of the set of observations v ; Function to calculate the std deviation of set of observations v ; Get sum of frequencies ; Get the mean of the set of observations ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE #include <algorithm> NEW_LINE #include <cmath> NEW_LINE using namespace std ; typedef pair < float , float > Data ; typedef vector < Data > Vector ; float sigma_fx ( const Vector & v ) { float sum = 0 ; for ( auto i : v ) { sum += i . first * i . second ; } return sum ; } float sigma_f ( const Vector & v ) { float sum = 0.0 ; for ( auto i : v ) { sum += i . second ; } return sum ; } float calculate_mean ( const Vector & v ) { return sigma_fx ( v ) / sigma_f ( v ) ; } float calculate_std ( const Vector & v ) { float f = sigma_f ( v ) ; 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 ) ; } 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 STRNEWLINE " : cout << " Investment ▁ A ▁ is ▁ less ▁ risky STRNEWLINE " ; return 0 ; }