はじめに
この実験では、C プログラムを用いて内部収益率 (IRR) を近似する方法を学びます。まず、投資の異なる時点における投資額または収益額を表すキャッシュフローを読み込みます。次に、現在価値 (NPV) がほぼゼロとなるレートを反復的なアプローチで探し出し、推定 IRR を得ます。最後に、計算された IRR を出力します。この実験では、重要な財務数学的概念をカバーし、それらの C プログラムでの実装を示します。
キャッシュフローの読み込み
このステップでは、C プログラムで内部収益率 (IRR) を計算するために、キャッシュフローを読み込み、格納する方法を学びます。キャッシュフローは、投資の異なる時点における投資額または収益額を表します。
まず、キャッシュフローの読み込み機能を実装するための C ファイルを作成しましょう。
cd ~/project
nano irr_calculation.c
次に、キャッシュフローを読み込むための初期コードを記述します。
#include <stdio.h>
#define MAX_CASH_FLOWS 10
int main() {
double cash_flows[MAX_CASH_FLOWS];
int num_cash_flows;
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
printf("Enter cash flows (negative for investments, positive for returns):\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
// 入力されたキャッシュフローを出力して確認
printf("\nEntered Cash Flows:\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: %.2f\n", i, cash_flows[i]);
}
return 0;
}
プログラムをコンパイルして実行します。
gcc irr_calculation.c -o irr_calculation
./irr_calculation
実行例:
Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500
Entered Cash Flows:
Cash flow 0: -1000.00
Cash flow 1: 300.00
Cash flow 2: 400.00
Cash flow 3: 500.00
説明
- 過度のメモリ使用を防ぐために、キャッシュフローの最大数 (
MAX_CASH_FLOWS) を定義します。 - プログラムは最初に、ユーザーにキャッシュフローの数を入力するように求めます。
- 次に、ユーザーに各キャッシュフローの値を入力するように求めます。
- 負の値は初期投資を表します。
- 正の値は収益または収入を表します。
- プログラムは、入力されたキャッシュフローを検証するために、入力されたキャッシュフローを出力します。
反復法を用いた NPV≈0 となるレートの算出
このステップでは、反復的な数値計算手法を用いて内部収益率 (IRR) を計算する、これまでのキャッシュフロープログラムを拡張します。
まず、既存の C ファイルを修正します。
cd ~/project
nano irr_calculation.c
次に、NPV と IRR の計算ロジックを実装します。
#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
#define EPSILON 0.0001
double calculate_npv(double cash_flows[], int num_cash_flows, double rate) {
double npv = 0.0;
for (int i = 0; i < num_cash_flows; i++) {
npv += cash_flows[i] / pow(1 + rate, i);
}
return npv;
}
double find_irr(double cash_flows[], int num_cash_flows) {
double rate_low = -0.9;
double rate_high = 10.0;
double rate = 0.1;
while ((rate_high - rate_low) > EPSILON) {
double npv = calculate_npv(cash_flows, num_cash_flows, rate);
if (fabs(npv) < EPSILON) {
return rate;
}
if (npv > 0) {
rate_low = rate;
} else {
rate_high = rate;
}
rate = (rate_low + rate_high) / 2.0;
}
return rate;
}
int main() {
double cash_flows[MAX_CASH_FLOWS];
int num_cash_flows;
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
printf("Enter cash flows (negative for investments, positive for returns):\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
double irr = find_irr(cash_flows, num_cash_flows);
printf("\nApproximate Internal Rate of Return (IRR): %.4f or %.2f%%\n", irr, irr * 100);
return 0;
}
数学ライブラリを使用してプログラムをコンパイルします。
gcc irr_calculation.c -o irr_calculation -lm
サンプルキャッシュフローでプログラムを実行します。
./irr_calculation
実行例:
Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500
Approximate Internal Rate of Return (IRR): 0.2154 or 21.54%
説明
calculate_npv()は、与えられた金利に対する現在価値 (NPV) を計算します。find_irr()は、二分法を用いて NPV がほぼ 0 となるレートを見つけます。EPSILONを用いて収束精度を定義します。- アルゴリズムは反復的に IRR の範囲を狭めていきます。
- 最終的な IRR は、小数点表示とパーセント表示で出力されます。
Print Estimated IRR
In this final step, we'll enhance our IRR calculation program to provide more detailed output and demonstrate different investment scenarios.
Let's modify our existing C file to add more comprehensive IRR analysis:
cd ~/project
nano irr_calculation.c
Update the code with additional output and analysis:
#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
#define EPSILON 0.0001
double calculate_npv(double cash_flows[], int num_cash_flows, double rate) {
double npv = 0.0;
for (int i = 0; i < num_cash_flows; i++) {
npv += cash_flows[i] / pow(1 + rate, i);
}
return npv;
}
double find_irr(double cash_flows[], int num_cash_flows) {
double rate_low = -0.9;
double rate_high = 10.0;
double rate = 0.1;
while ((rate_high - rate_low) > EPSILON) {
double npv = calculate_npv(cash_flows, num_cash_flows, rate);
if (fabs(npv) < EPSILON) {
return rate;
}
if (npv > 0) {
rate_low = rate;
} else {
rate_high = rate;
}
rate = (rate_low + rate_high) / 2.0;
}
return rate;
}
void print_investment_analysis(double cash_flows[], int num_cash_flows, double irr) {
double total_investment = 0;
double total_returns = 0;
printf("\n--- Investment Analysis ---\n");
// Detailed cash flow breakdown
for (int i = 0; i < num_cash_flows; i++) {
printf("Period %d: $%.2f\n", i, cash_flows[i]);
if (cash_flows[i] < 0) {
total_investment += fabs(cash_flows[i]);
} else {
total_returns += cash_flows[i];
}
}
// Investment summary
printf("\nTotal Investment: $%.2f\n", total_investment);
printf("Total Returns: $%.2f\n", total_returns);
printf("Net Profit: $%.2f\n", total_returns - total_investment);
// IRR details
printf("\nInternal Rate of Return (IRR):\n");
printf("Decimal: %.4f\n", irr);
printf("Percentage: %.2f%%\n", irr * 100);
// Investment performance interpretation
if (irr > 0.15) {
printf("\nInvestment Performance: Excellent\n");
} else if (irr > 0.10) {
printf("\nInvestment Performance: Good\n");
} else if (irr > 0) {
printf("\nInvestment Performance: Moderate\n");
} else {
printf("\nInvestment Performance: Poor\n");
}
}
int main() {
double cash_flows[MAX_CASH_FLOWS];
int num_cash_flows;
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
printf("Enter cash flows (negative for investments, positive for returns):\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
double irr = find_irr(cash_flows, num_cash_flows);
print_investment_analysis(cash_flows, num_cash_flows, irr);
return 0;
}
Compile the program:
gcc irr_calculation.c -o irr_calculation -lm
Run the program with sample investment scenario:
./irr_calculation
Example output:
Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500
--- Investment Analysis ---
Period 0: $-1000.00
Period 1: $300.00
Period 2: $400.00
Period 3: $500.00
Total Investment: $1000.00
Total Returns: $1200.00
Net Profit: $200.00
Internal Rate of Return (IRR):
Decimal: 0.2154
Percentage: 21.54%
Investment Performance: Excellent
Explanation
- Added
print_investment_analysis()function to provide comprehensive output - Calculates total investment, returns, and net profit
- Interprets IRR performance with descriptive categories
- Provides detailed breakdown of cash flows and investment metrics
まとめ
この実験では、最初に C プログラムで内部収益率 (IRR) を計算するために、キャッシュフローを読み取り、格納する方法を学びました。キャッシュフローは、投資の異なる期間において投資されたり、受け取られたりするお金を表します。次に、現在価値 (NPV) がほぼゼロとなるレートを見つけるために反復法を用いる方法を学びました。これは、IRR を求める方法です。最後に、推定 IRR の出力方法を学びました。重要な学習点は、キャッシュフローのデータ構造を理解すること、IRR を見つけるための反復アルゴリズムを実装すること、そして最終結果を提示することです。



