소개
이 실험에서는 C 프로그램을 사용하여 내부 수익률 (IRR) 을 근사하는 방법을 배웁니다. 먼저 투자의 서로 다른 시점에서 투자된 또는 받은 돈을 나타내는 현금 흐름을 읽습니다. 그런 다음 순현재가치 (NPV) 가 근사적으로 0 이 되는 비율을 반복적인 방법으로 찾아 추정된 IRR 을 얻습니다. 마지막으로 계산된 IRR 을 출력합니다. 이 실험은 주요 재무 수학 개념을 다루고 C 프로그래밍에서의 구현을 보여줍니다.
이 실험에서는 C 프로그램을 사용하여 내부 수익률 (IRR) 을 근사하는 방법을 배웁니다. 먼저 투자의 서로 다른 시점에서 투자된 또는 받은 돈을 나타내는 현금 흐름을 읽습니다. 그런 다음 순현재가치 (NPV) 가 근사적으로 0 이 되는 비율을 반복적인 방법으로 찾아 추정된 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) 를 정의합니다.이 단계에서는 이전 현금 흐름 프로그램을 확장하여 반복적인 수치 방법을 사용하여 내부 수익률 (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 분석을 추가하기 위해 기존 C 파일을 수정합니다.
cd ~/project
nano irr_calculation.c
추가적인 출력 및 분석으로 코드를 업데이트합니다.
#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--- 투자 분석 ---\n");
// 자세한 현금 흐름 세부 정보
for (int i = 0; i < num_cash_flows; i++) {
printf("기간 %d: $%.2f\n", i, cash_flows[i]);
if (cash_flows[i] < 0) {
total_investment += fabs(cash_flows[i]);
} else {
total_returns += cash_flows[i];
}
}
// 투자 요약
printf("\n총 투자: $%.2f\n", total_investment);
printf("총 수익: $%.2f\n", total_returns);
printf("순이익: $%.2f\n", total_returns - total_investment);
// IRR 세부 정보
printf("\n내부 수익률 (IRR):\n");
printf("소수점: %.4f\n", irr);
printf("퍼센트: %.2f%%\n", irr * 100);
// 투자 성과 해석
if (irr > 0.15) {
printf("\n투자 성과: 우수\n");
} else if (irr > 0.10) {
printf("\n투자 성과: 양호\n");
} else if (irr > 0) {
printf("\n투자 성과: 보통\n");
} else {
printf("\n투자 성과: 저조\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;
}
프로그램을 컴파일합니다.
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
--- 투자 분석 ---
기간 0: $-1000.00
기간 1: $300.00
기간 2: $400.00
기간 3: $500.00
총 투자: $1000.00
총 수익: $1200.00
순이익: $200.00
내부 수익률 (IRR):
소수점: 0.2154
퍼센트: 21.54%
투자 성과: 우수
print_investment_analysis() 함수를 추가했습니다.이 실습에서는 먼저 C 프로그램에서 내부 수익률 (IRR) 을 계산하기 위해 현금 흐름을 읽고 저장하는 방법을 배웠습니다. 현금 흐름은 투자의 서로 다른 기간에 투자되거나 받은 금액을 나타냅니다. 그런 다음 순현재가치 (NPV) 가 거의 0 이 되는 비율, 즉 IRR 을 찾기 위해 반복을 사용하는 방법을 탐구했습니다. 마지막으로 추정된 IRR 을 출력하는 방법을 배웠습니다. 주요 학습 내용은 현금 흐름 데이터 구조 이해, IRR 을 찾기 위한 반복 알고리즘 구현 및 최종 결과 제시입니다.