{
  "id": 556619,
  "title": "Custom R2 Loss Function for LGBM",
  "url": "/competitions/jane-street-real-time-market-data-forecasting/discussion/556619",
  "author_name": "Kirderf",
  "post_date": "2025-01-14T09:11:11.824000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>For comparison and as another example of lgb loss/metric implementation:</p>\n<p>I used below for CV timessplit training and with all the data and only scaler as feature engineering I got CV 0.0108 and 0.0062 in the public leaderboard.</p>\n<pre><code> ():\n    numerator = np.(weights * (y_true - y_pred)**)\n    denominator = np.(weights * (y_true**))\n      - (numerator / denominator)\n\n ():\n    numerator = np.(weights * (y_true - y_pred)**)\n    denominator = np.(weights * (y_true**))\n    r2 =  - (numerator / denominator)\n      - r2\n\n ():\n    y_true = train_data.get_label()\n    weights = train_data.get_weight()\n\n    loss = custom_r2_loss(y_true, y_pred, weights)\n    grad = - * weights * (y_true - y_pred) / (np.(weights * y_true**) + )\n    hess =  * weights / (np.(weights * y_true**) + )\n\n     grad, hess\n</code></pre>\n<p>Use the code with lgb.Dataset(X, y, weight = weights) and lgb.train(lgb_params, lgb_train, feval = lgb_r2_loss).</p>",
  "messages": [
    {
      "id": 3096330,
      "postDate": "2025-01-14T09:11:11.823Z",
      "content": "<p>For comparison and as another example of lgb loss/metric implementation:</p>\n<p>I used below for CV timessplit training and with all the data and only scaler as feature engineering I got CV 0.0108 and 0.0062 in the public leaderboard.</p>\n<pre><code> ():\n    numerator = np.(weights * (y_true - y_pred)**)\n    denominator = np.(weights * (y_true**))\n      - (numerator / denominator)\n\n ():\n    numerator = np.(weights * (y_true - y_pred)**)\n    denominator = np.(weights * (y_true**))\n    r2 =  - (numerator / denominator)\n      - r2\n\n ():\n    y_true = train_data.get_label()\n    weights = train_data.get_weight()\n\n    loss = custom_r2_loss(y_true, y_pred, weights)\n    grad = - * weights * (y_true - y_pred) / (np.(weights * y_true**) + )\n    hess =  * weights / (np.(weights * y_true**) + )\n\n     grad, hess\n</code></pre>\n<p>Use the code with lgb.Dataset(X, y, weight = weights) and lgb.train(lgb_params, lgb_train, feval = lgb_r2_loss).</p>",
      "rawMarkdown": "For comparison and as another example of lgb loss/metric implementation:\n\nI used below for CV timessplit training and with all the data and only scaler as feature engineering I got CV 0.0108 and 0.0062 in the public leaderboard.\n\n```python\ndef r_squared(y_true, y_pred, weights):\n    numerator = np.sum(weights * (y_true - y_pred)**2)\n    denominator = np.sum(weights * (y_true**2))\n    return 1 - (numerator / denominator)\n\ndef custom_r2_loss(y_true, y_pred, weights):\n    numerator = np.sum(weights * (y_true - y_pred)**2)\n    denominator = np.sum(weights * (y_true**2))\n    r2 = 1 - (numerator / denominator)\n    return 1 - r2\n\ndef lgb_r2_loss(y_pred, train_data):\n    y_true = train_data.get_label()\n    weights = train_data.get_weight()\n    \n    loss = custom_r2_loss(y_true, y_pred, weights)\n    grad = -2 * weights * (y_true - y_pred) / (np.sum(weights * y_true**2) + 1e-7)\n    hess = 2 * weights / (np.sum(weights * y_true**2) + 1e-7)\n\n    return grad, hess\n```\n\nUse the code with lgb.Dataset(X, y, weight = weights) and lgb.train(lgb_params, lgb_train, feval = lgb_r2_loss).",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "3096330": "For comparison and as another example of lgb loss/metric implementation:\n\nI used below for CV timessplit training and with all the data and only scaler as feature engineering I got CV 0.0108 and 0.0062 in the public leaderboard.\n\n```python\ndef r_squared(y_true, y_pred, weights):\n    numerator = np.sum(weights * (y_true - y_pred)**2)\n    denominator = np.sum(weights * (y_true**2))\n    return 1 - (numerator / denominator)\n\ndef custom_r2_loss(y_true, y_pred, weights):\n    numerator = np.sum(weights * (y_true - y_pred)**2)\n    denominator = np.sum(weights * (y_true**2))\n    r2 = 1 - (numerator / denominator)\n    return 1 - r2\n\ndef lgb_r2_loss(y_pred, train_data):\n    y_true = train_data.get_label()\n    weights = train_data.get_weight()\n    \n    loss = custom_r2_loss(y_true, y_pred, weights)\n    grad = -2 * weights * (y_true - y_pred) / (np.sum(weights * y_true**2) + 1e-7)\n    hess = 2 * weights / (np.sum(weights * y_true**2) + 1e-7)\n\n    return grad, hess\n```\n\nUse the code with lgb.Dataset(X, y, weight = weights) and lgb.train(lgb_params, lgb_train, feval = lgb_r2_loss)."
  }
}