FinBERT-company-sentiment (private)
What it is for. The sentiment of a short financial-news text about one named company: the input is the pair
(company name, text) and the output is positive, negative or neutral for that company. "Neutral" also means the text
says nothing about this company. It is the sentiment model of step 6 of the project's news pipeline (PLAN D39/D40), meant to be
used together with the relevance model (gamug/deberta-v3-company-relevance) through the stack in sentiment_stack.json.
Not for: raw article bodies (it was trained on summaries of a few sentences), texts with no company (sector, market, economy: they are read by another model), languages other than English, or any use as investment advice.
How it was trained
- Start:
gamug/FinBERT-financial-news@93863fcb7252874e7c0339081b34f691f9e17ff6(itself a continued fine-tune ofProsusAI/finberton a balanced pool of 3,865 sentences labelled one by one by an LLM, not tied to a company). - Recipe: input = company name as segment A, text as segment B (only the text is truncated, 512 tokens); 2 epochs, learning rate 3e-5, seed 4, no validation slice (every row is used for training).
- Training rows: the 809 owner-tagged pairs below plus 341 replay sentences from the 3,865 (sentences that name their article's company, read as a pair with it; their labels are an LLM's sentence-level reading, not the owner's rule) to keep the sentence knowledge: 1,150 examples.
Data. 809 rows, each a text (a short summary of a financial-news article, about 330 characters on average, English,
published 2022 to 2026; written by sshleifer/distilbart-cnn-12-6) and a company (228 companies of the project's tracked
universe of 507). The labels were made by one person, the project owner, who read each text for that company: the sentiment
an investor would read on the company's price (positive 394, negative 213, neutral 202). The rule: if the company is in
the facts the text presents, the mark is the sentiment about it; if the text gives no fact about the company (it is absent,
mentioned in passing, or the facts are about another company or the market), the mark is neutral. The rows come from two
draws balanced by what an earlier model predicted (not by the owner's classes), so every figure below is weighted back
to the pool each draw came from. The marks and their texts are tracked in the project repository gamug/portfolio-nlp
(gold/sentiment_train.csv, gold/sentiment_train_2.csv, gold/texts/); this model repository holds no news text.
Figures (cross-validation, not an exam)
Measured before this final model existed, on the same recipe: 5-fold cross-validation, twice, over the 809 rows (each text scored by a model that did not train on it, "out-of-fold"), weighted as above. Selection metric (owner, D37): the mean of nine figures: precision, recall and ROC (one class against the other two) per class.
| sentiment model alone | stacked with the relevance model | |
|---|---|---|
| score (mean of the nine) | 0.737 | 0.814 (nested, below) |
| macro precision / recall | 0.692 / 0.674 | 0.780 / 0.763 |
| macro ROC (one vs rest) | 0.845 | 0.898 |
| neutral precision / recall | 0.634 / 0.442 | 0.787 / 0.686 |
| negative precision / recall | 0.680 / 0.745 | 0.747 / 0.748 |
| positive precision / recall | 0.761 / 0.834 | 0.807 / 0.855 |
For comparison, the text-only model that is pinned in the pipeline scores 0.648 on the same texts. The stack was evaluated nested (its coefficients fitted on 4/5 of the out-of-fold rows and applied to the other 1/5, 10 repeats x 2); gains over the sentiment model alone are paired and bootstrapped (400 resamples): stack score +0.077 [+0.057, +0.095].
The 180-text gold has not been scored. That is the exam of the project (D32/D35); it is run once, on this published revision, and decides the pin. Until then these figures are estimates of the recipe, not of this model.
Assumptions and limits
- One annotator, one rule; agreement between annotators was not measured. The rule was given after the first 450 marks.
- The classes are not balanced (394/213/202) and the weights correct for the sampling design, not for the corpus.
- The final model saw all the rows, so there is no held-out score of this model; the figures are for the recipe.
- The stack's coefficients were fitted on out-of-fold scores; this final model is slightly stronger than the fold models, so its probabilities are a little more confident than the stack saw (the effect is not measured).
- Summaries, English, 2022 to 2026; the labels have no date cut-off (look-ahead in a backtest is not excluded).
- "Neutral because the company is absent" is learned from the pair; about 60% of the neutral texts (121 of 202) do name the company, so the model cannot rely on the name alone.
Use
import json, numpy as np, torch
from huggingface_hub import hf_hub_download
from transformers import AutoModelForSequenceClassification, AutoTokenizer
tok = AutoTokenizer.from_pretrained("gamug/FinBERT-company-sentiment")
model = AutoModelForSequenceClassification.from_pretrained("gamug/FinBERT-company-sentiment").eval()
x = tok("Apple Inc.", "Apple shares fell 5% after weak iPhone sales.", truncation="only_second", max_length=512, return_tensors="pt")
p = torch.softmax(model(**x).logits, -1)[0].tolist() # order: positive, negative, neutral
# stack with the relevance model's score r (probability of 'entailment', see its card):
s = json.load(open(hf_hub_download("gamug/FinBERT-company-sentiment", "sentiment_stack.json")))
r = 0.9
rc = np.clip(r, 1e-6, 1 - 1e-6)
f = np.array([*np.log(np.clip(p, 1e-6, 1)), np.log(rc / (1 - rc))])
z = np.array(s["coef"]) @ f + np.array(s["intercept"]); probs = np.exp(z - z.max()); probs /= probs.sum() # s["classes"] order
- Downloads last month
- 8