跳转至

对数收益率(百分比非对称性)

#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

// 计算百分比收益率
double percentageReturn(double startPrice, double endPrice) {
  return (endPrice - startPrice) / startPrice * 100;
}

// 计算对数收益率
double logReturn(double startPrice, double endPrice) {
  return log(endPrice) - log(startPrice);  // 自然对数差值
}

int main() {
  // 示例1:100 -> 110 -> 100的价格变动
  double price1 = 100.0;
  double price2 = 110.0;
  double price3 = 100.0;

  // 计算百分比收益率
  double upPercent = percentageReturn(price1, price2);
  double downPercent = percentageReturn(price2, price3);
  double totalPercent = upPercent + downPercent;

  // 计算对数收益率
  double upLog = logReturn(price1, price2);
  double downLog = logReturn(price2, price3);
  double totalLog = upLog + downLog;

  cout << fixed << setprecision(4);
  cout << "示例1:100 -> 110 -> 100" << endl;
  cout << "百分比收益率:" << endl;
  cout << " 上涨阶段:" << upPercent << "%" << endl;
  cout << " 下跌阶段:" << downPercent << "%" << endl;
  cout << " 总收益:" << totalPercent << "%" << " (存在误差)" << endl << endl;

  cout << "对数收益率:" << endl;
  cout << " 上涨阶段:" << upLog << " (约" << upLog * 100 << "%)" << endl;
  cout << " 下跌阶段:" << downLog << " (约" << downLog * 100 << "%)" << endl;
  cout << " 总收益:" << totalLog << " (完美归零)" << endl << endl;

  // 示例2:展示不同价格基数下相同比例变动的一致性
  double priceA1 = 10.0, priceA2 = 11.0;    // 10%涨幅
  double priceB1 = 100.0, priceB2 = 110.0;  // 10%涨幅

  double logA = logReturn(priceA1, priceA2);
  double logB = logReturn(priceB1, priceB2);

  cout << "示例2:相同比例变动的一致性" << endl;
  cout << "10->11的对数收益:" << logA << endl;
  cout << "100->110的对数收益:" << logB << endl;
  cout << "两者是否相等:" << (abs(logA - logB) < 1e-9 ? "是" : "否") << endl;

  return 0;
}