给定两个投资选择A和B,我们发现这两个投资的风险较小。两个投资A和B分别由一个数组表示。数组中的每个元素都是可能的投资结果。因此,数组中的每个元素都是两个值对。第一个值是收到的钱数,第二个值是可以收到这笔钱的概率。例如,如果A = [(100,0.1),(200,0.2)(300,0.7)],则意味着有10%的可能性赢得100卢比,有20%的可能性赢得200卢比,而70%的可能性赢得来自投资A的300卢比。
我们必须使用统计方法来解决该问题。对于每个投资,我们首先计算可以从中赚取的平均金额。其次,我们还计算赚钱的标准差。然后,我们需要通过将其除以平均值来归一化该标准偏差。
每个可能的结果都是一个观察结果。每种金额的概率就是其频率。由于观测是通过频率给出的,因此我们需要应用以下公式来计算均值和标准差
如果表示一组观察 。
均值=
标准偏差
让我们以一个示例来演示如何应用此方法。
例子:
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
问题的实现如下
C++
// C++ code for above approach
#include
#include
#include
#include
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 Data;
// Vector stores the observation
// in pairs of format (xi, fi),
// where xi = value of observation
typedef vector 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;
}
Java
// Java code for above approach
import java.util.*;
class GFG
{
static class pair
{
float first, second;
public pair(float first, float second)
{
this.first = first;
this.second = second;
}
}
// 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
static Vector Vector;
// Function to calculate the
// summation of fi*xi
static float sigma_fx(pair[] a)
{
float sum = 0;
for (pair i : a)
{
sum += i.first * i.second;
}
return sum;
}
// Function to calculate summation fi
static float sigma_f(pair[] a)
{
float sum = 0.0f;
for (pair i : a)
{
sum += i.second;
}
return sum;
}
// Function to calculate the mean
// of the set of observations v
static float calculate_mean(pair[] a)
{
return sigma_fx(a) / sigma_f(a);
}
// Function to calculate the std
// deviation of set of observations v
static float calculate_std(pair[] a)
{
// Get sum of frequencies
float f = sigma_f(a);
// Get the mean of the set
// of observations
float mean = sigma_fx(a) / f;
float sum = 0;
for (pair i : a)
{
sum += (i.first - mean) *
(i.first - mean) * i.second;
}
return (float) Math.sqrt(sum / f);
}
// Driver Code
public static void main(String[] args)
{
pair[] A = { new pair(0f, 0.1f),
new pair(100f, 0.1f),
new pair(200f, 0.2f),
new pair(333f, 0.3f),
new pair(400f, 0.3f) };
pair[] B = { new pair(100f, 0.1f),
new pair(200f, 0.5f),
new pair(700f, 0.4f) };
float avg_A = calculate_mean(A);
float avg_B = calculate_mean(B);
float std_A = calculate_std(A);
float std_B = calculate_std(B);
System.out.print("For Investment A" + "\n");
System.out.print("Average: " + avg_A + "\n");
System.out.print("Standard Deviation: " +
std_A + "\n");
System.out.print("Normalised Std: " +
std_A / avg_A + "\n");
System.out.print("For Investment B" + "\n");
System.out.print("Average: " + avg_B + "\n");
System.out.print("Standard Deviation: " +
std_B + "\n");
System.out.print("Normalised Std: " +
std_B / avg_B + "\n");
if ((std_B / avg_B) < (std_A / avg_A))
System.out.print("Investment B is less risky\n");
else
System.out.print("Investment A is less risky\n");
}
}
// This code is contributed by PrinciRaj1992
C#
// C# code for above approach
using System;
using System.Collections.Generic;
class GFG
{
class pair
{
public float first, second;
public pair(float first,
float second)
{
this.first = first;
this.second = second;
}
}
// First Item in the pair is the
// value of observation (xi).
// Second Item in the pair is
// the frequency of xi (fi)
// List stores the observation
// in pairs of format (xi, fi),
// where xi = value of observation
static List List;
// Function to calculate the
// summation of fi*xi
static float sigma_fx(pair[] a)
{
float sum = 0;
foreach (pair i in a)
{
sum += i.first * i.second;
}
return sum;
}
// Function to calculate summation fi
static float sigma_f(pair[] a)
{
float sum = 0.0f;
foreach (pair i in a)
{
sum += i.second;
}
return sum;
}
// Function to calculate the mean
// of the set of observations v
static float calculate_mean(pair[] a)
{
return sigma_fx(a) / sigma_f(a);
}
// Function to calculate the std
// deviation of set of observations v
static float calculate_std(pair[] a)
{
// Get sum of frequencies
float f = sigma_f(a);
// Get the mean of the set
// of observations
float mean = sigma_fx(a) / f;
float sum = 0;
foreach (pair i in a)
{
sum += (i.first - mean) *
(i.first - mean) * i.second;
}
return (float) Math.Sqrt(sum / f);
}
// Driver Code
public static void Main(String[] args)
{
pair[] A = {new pair(0f, 0.1f),
new pair(100f, 0.1f),
new pair(200f, 0.2f),
new pair(333f, 0.3f),
new pair(400f, 0.3f)};
pair[] B = {new pair(100f, 0.1f),
new pair(200f, 0.5f),
new pair(700f, 0.4f)};
float avg_A = calculate_mean(A);
float avg_B = calculate_mean(B);
float std_A = calculate_std(A);
float std_B = calculate_std(B);
Console.Write("For Investment A" + "\n");
Console.Write("Average: " + avg_A + "\n");
Console.Write("Standard Deviation: " +
std_A + "\n");
Console.Write("Normalised Std: " +
std_A / avg_A + "\n");
Console.Write("For Investment B" + "\n");
Console.Write("Average: " + avg_B + "\n");
Console.Write("Standard Deviation: " +
std_B + "\n");
Console.Write("Normalised Std: " +
std_B / avg_B + "\n");
if ((std_B / avg_B) < (std_A / avg_A))
Console.Write("Investment B is less risky\n");
else
Console.Write("Investment A is less risky\n");
}
}
// This code is contributed by Rajput-Ji
输出:
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
参考
https://www.statcan.gc.ca/edu/power-pouvoir/ch12/5214891-eng.htm
std :: accumulate cppreference.com