# AUTOGENERATED! DO NOT EDIT! File to edit: 03_pytorch_discrete_choice.ipynb (unless otherwise specified).

__all__ = ['log_loss']

# Cell
# Py3 Built-in module for type-hint annotations
from typing import Dict, Tuple, List, Callable, Optional
# Third-party modules for:
# class definition
import attr
# Optimization
import scipy.optimize as opt
# Numerical computation
import torch
import torch.nn as nn
import numpy as np
# Utility functions for pytorch optimization
import botorch.optim.numpy_converter as numpy_converter
import botorch.optim.utils as optim_utils
from botorch.optim.numpy_converter import TorchAttr
# Input/Output of data
import pandas as pd

# Cell
def log_loss(probs: torch.Tensor,
             targets: torch.Tensor) -> torch.Tensor:
    """
    Computes the log-loss (i.e., the negative log-likelihood) for given
    long-format tensors of probabilities and choice indicators.

    Parameters
    ----------
    probs : 1D torch.Tensor.
        The probability of choosing each row's alternative for the given choice situation.
    targets : 1D torch.Tensor.
        A Tensor of zeros and ones indicating the chosen row for each choice situation. Should have the same size as `probs`.

    Returns
    -------
    neg_log_likelihood : scalar torch.Tensor.
        The negative log-likelihood computed from `probs` and `targets`.
    """
    log_likelihood = torch.sum(targets * torch.log(probs))
    return -1 * log_likelihood