PYield 0.32.2__tar.gz → 0.32.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. {pyield-0.32.2 → pyield-0.32.4}/PKG-INFO +1 -1
  2. pyield-0.32.4/pyield/__about__.py +1 -0
  3. {pyield-0.32.2 → pyield-0.32.4}/pyield/__init__.py +2 -1
  4. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/di.py +4 -5
  5. {pyield-0.32.2 → pyield-0.32.4}/pyield/conftest.py +2 -0
  6. pyield-0.32.4/pyield/fwd.py +164 -0
  7. pyield-0.32.2/pyield/__about__.py +0 -1
  8. pyield-0.32.2/pyield/forward.py +0 -56
  9. {pyield-0.32.2 → pyield-0.32.4}/.gitignore +0 -0
  10. {pyield-0.32.2 → pyield-0.32.4}/LICENSE +0 -0
  11. {pyield-0.32.2 → pyield-0.32.4}/README.md +0 -0
  12. {pyield-0.32.2 → pyield-0.32.4}/pyield/anbima/__init__.py +0 -0
  13. {pyield-0.32.2 → pyield-0.32.4}/pyield/anbima/ettj.py +0 -0
  14. {pyield-0.32.2 → pyield-0.32.4}/pyield/anbima/ima.py +0 -0
  15. {pyield-0.32.2 → pyield-0.32.4}/pyield/anbima/imaq.py +0 -0
  16. {pyield-0.32.2 → pyield-0.32.4}/pyield/anbima/ipca.py +0 -0
  17. {pyield-0.32.2 → pyield-0.32.4}/pyield/anbima/tpf.py +0 -0
  18. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/__init__.py +0 -0
  19. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/di_over.py +0 -0
  20. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/futures/__init__.py +0 -0
  21. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/futures/common.py +0 -0
  22. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/futures/historical.py +0 -0
  23. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/futures/intraday.py +0 -0
  24. {pyield-0.32.2 → pyield-0.32.4}/pyield/b3/futures/xml.py +0 -0
  25. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/__init__.py +0 -0
  26. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/auction.py +0 -0
  27. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/bcdata.py +0 -0
  28. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/ptax.py +0 -0
  29. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/repo.py +0 -0
  30. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/trade.py +0 -0
  31. {pyield-0.32.2 → pyield-0.32.4}/pyield/bc/vna.py +0 -0
  32. {pyield-0.32.2 → pyield-0.32.4}/pyield/bday.py +0 -0
  33. {pyield-0.32.2 → pyield-0.32.4}/pyield/data_cache.py +0 -0
  34. {pyield-0.32.2 → pyield-0.32.4}/pyield/date_converter.py +0 -0
  35. {pyield-0.32.2 → pyield-0.32.4}/pyield/holidays/__init__.py +0 -0
  36. {pyield-0.32.2 → pyield-0.32.4}/pyield/holidays/br_holidays_new.txt +0 -0
  37. {pyield-0.32.2 → pyield-0.32.4}/pyield/holidays/br_holidays_old.txt +0 -0
  38. {pyield-0.32.2 → pyield-0.32.4}/pyield/holidays/brholidays.py +0 -0
  39. {pyield-0.32.2 → pyield-0.32.4}/pyield/ibge/__init__.py +0 -0
  40. {pyield-0.32.2 → pyield-0.32.4}/pyield/ibge/ipca.py +0 -0
  41. {pyield-0.32.2 → pyield-0.32.4}/pyield/interpolator.py +0 -0
  42. {pyield-0.32.2 → pyield-0.32.4}/pyield/py.typed +0 -0
  43. {pyield-0.32.2 → pyield-0.32.4}/pyield/retry.py +0 -0
  44. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/__init__.py +0 -0
  45. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/lft.py +0 -0
  46. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/ltn.py +0 -0
  47. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/ntnb.py +0 -0
  48. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/ntnc.py +0 -0
  49. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/ntnf.py +0 -0
  50. {pyield-0.32.2 → pyield-0.32.4}/pyield/tpf/tools.py +0 -0
  51. {pyield-0.32.2 → pyield-0.32.4}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PYield
3
- Version: 0.32.2
3
+ Version: 0.32.4
4
4
  Summary: A Python library for analysis of fixed income instruments in Brazil
5
5
  Project-URL: Source, https://github.com/crdcj/PYield
6
6
  Author-email: Carlos Carvalho <cr.cj@outlook.com>
@@ -0,0 +1 @@
1
+ __version__ = "0.32.4"
@@ -4,7 +4,7 @@ from pyield import anbima, b3, bc, bday, ibge
4
4
  from pyield.__about__ import __version__
5
5
  from pyield.b3 import futures
6
6
  from pyield.b3.di import DIFutures
7
- from pyield.forward import forwards
7
+ from pyield.fwd import forward, forwards
8
8
  from pyield.interpolator import Interpolator
9
9
  from pyield.tpf import lft, ltn, ntnb, ntnc, ntnf
10
10
 
@@ -14,6 +14,7 @@ __all__ = [
14
14
  "bday",
15
15
  "DIFutures",
16
16
  "forwards",
17
+ "forward",
17
18
  "futures",
18
19
  "ibge",
19
20
  "Interpolator",
@@ -5,8 +5,7 @@ from zoneinfo import ZoneInfo
5
5
  import pandas as pd
6
6
 
7
7
  import pyield.date_converter as dc
8
- import pyield.forward as fw
9
- from pyield import b3, bday, interpolator
8
+ from pyield import b3, bday, fwd, interpolator
10
9
  from pyield.data_cache import get_cached_dataset
11
10
  from pyield.date_converter import DateArray, DateScalar
12
11
 
@@ -206,9 +205,9 @@ class DIFutures:
206
205
  if df.empty:
207
206
  return pd.DataFrame()
208
207
 
209
- df["ForwardRate"] = fw.forwards(
210
- business_days=bday.count(df["TradeDate"], df["ExpirationDate"]),
211
- zero_rates=df["SettlementRate"],
208
+ df["ForwardRate"] = fwd.forwards(
209
+ bdays=bday.count(df["TradeDate"], df["ExpirationDate"]),
210
+ rates=df["SettlementRate"],
212
211
  groupby_dates=df["TradeDate"],
213
212
  )
214
213
 
@@ -1,3 +1,4 @@
1
+ import pandas
1
2
  import pytest
2
3
 
3
4
  import pyield
@@ -6,6 +7,7 @@ import pyield
6
7
  @pytest.fixture(autouse=True)
7
8
  def add_np(doctest_namespace):
8
9
  doctest_namespace["yd"] = pyield
10
+ doctest_namespace["pd"] = pandas
9
11
 
10
12
 
11
13
  def pytest_configure(config):
@@ -0,0 +1,164 @@
1
+ import pandas as pd
2
+
3
+
4
+ def forwards(
5
+ bdays: pd.Series,
6
+ rates: pd.Series,
7
+ groupby_dates: pd.Series | None = None,
8
+ ) -> pd.Series:
9
+ """
10
+ Calcula taxas a termo (forward rates) a partir de taxas zero (spot rates).
11
+
12
+ Utiliza a fórmula:
13
+ f₁→₂ = ((1 + r₂)^(du₂/252) / (1 + r₁)^(du₁/252))^(252/(du₂ - du₁)) - 1
14
+
15
+ Como du/252 = t (tempo em anos úteis), a fórmula pode ser simplificada para:
16
+ f₁→₂ = ((1 + r₂)^t₂ / (1 + r₁)^t₁)^(1/(t₂ - t₁)) - 1
17
+
18
+ Onde:
19
+ - r₁ é a taxa zero para o vértice anterior.
20
+ - r₂ é a taxa zero para o vértice atual.
21
+ - t₁ é o prazo em anos para o vértice anterior (calculado como du₁/252).
22
+ - t₂ é o prazo em anos para o vértice atual (calculado como du₂/252).
23
+ - A constante 252 representa o número de dias úteis no ano.
24
+
25
+ A primeira taxa a termo de cada sequência (ou grupo) é definida como a
26
+ taxa zero desse primeiro vértice (f₁→₂ = r₂), dado que não existe um vértice
27
+ anterior (r₁) para se calcular a taxa a termo do primeiro ponto.
28
+
29
+ A função lida internamente com o alinhamento dos índices das Séries de
30
+ entrada através de `reset_index`. Os cálculos são realizados após ordenar
31
+ os dados pelos prazos (`bdays`) dentro de cada grupo definido por
32
+ `groupby_dates` (ou como um único grupo se `groupby_dates` não for
33
+ fornecido), garantindo a ordem cronológica correta para a fórmula.
34
+
35
+ Valores NaN nas taxas ou prazos de entrada resultarão em NaN na saída,
36
+ exceto para o primeiro ponto de cada grupo, que é tratado separadamente.
37
+
38
+ Args:
39
+ bdays (pd.Series): Número de dias úteis para cada taxa zero.
40
+ rates (pd.Series): Taxas zero correspondentes aos dias úteis.
41
+ groupby_dates (pd.Series | None, optional): Critério de agrupamento
42
+ opcional para segmentar os cálculos (ex: por data de referência).
43
+ Se None, todos os dados são tratados como um único grupo.
44
+ Default None.
45
+
46
+ Returns:
47
+ pd.Series: Série contendo as taxas a termo calculadas (tipo Float64).
48
+ A primeira taxa de cada grupo corresponde à taxa zero inicial.
49
+
50
+ Example:
51
+ >>> bdays = pd.Series([10, 20, 30])
52
+ >>> rates = pd.Series([0.05, 0.06, 0.07])
53
+ >>> yd.forwards(bdays, rates)
54
+ 0 0.05
55
+ 1 0.070095
56
+ 2 0.090284
57
+ dtype: Float64
58
+ """
59
+ # Reset Series indexes to avoid misalignment issues during calculations
60
+ bdays = bdays.reset_index(drop=True).astype("Int64")
61
+ rates = rates.reset_index(drop=True).astype("Float64")
62
+ if groupby_dates is not None:
63
+ groupby_dates = groupby_dates.reset_index(drop=True)
64
+
65
+ # Create a DataFrame to work with the given series
66
+ df = pd.DataFrame({"du2": bdays, "r2": rates})
67
+ df["t2"] = df["du2"] / 252
68
+
69
+ # If no groupby_dates is provided, create a dummy column to group the DataFrame
70
+ if groupby_dates is not None:
71
+ df["groupby_date"] = groupby_dates
72
+ else:
73
+ df["groupby_date"] = 0 # Dummy value to group the DataFrame
74
+
75
+ # Sort by the groupby_dates and t2 columns to ensure proper chronological order
76
+ df.sort_values(by=["groupby_date", "t2"], inplace=True)
77
+
78
+ # Calculate the next zero rate and business day for each group
79
+ df["r1"] = df.groupby("groupby_date")["r2"].shift(1)
80
+ df["t1"] = df.groupby("groupby_date")["t2"].shift(1)
81
+
82
+ # Calculate the formula components
83
+ factor_r2 = (1 + df["r2"]) ** df["t2"] # (1 + r₂)^t₂
84
+ factor_r1 = (1 + df["r1"]) ** df["t1"] # (1 + r₁)^t₁
85
+ time_exp = 1 / (df["t2"] - df["t1"]) # 1/(t₂ - t₁)
86
+
87
+ # f₁→₂ = ((1 + r₂)^t₂ / (1 + r₁)^t₁)^(1/(t₂ - t₁)) - 1
88
+ df["f1_2"] = (factor_r2 / factor_r1) ** time_exp - 1
89
+
90
+ # Identifify the first index of each group of dates
91
+ first_indices = df.groupby("groupby_date").head(1).index
92
+ # Set the first forward rate of each group to the zero rate
93
+ df.loc[first_indices, "f1_2"] = df.loc[first_indices, "r2"]
94
+
95
+ # Return the forward rates as a Series
96
+ f1_2 = df["f1_2"]
97
+ f1_2.name = None
98
+ return f1_2
99
+
100
+
101
+ def forward(
102
+ bday1: int,
103
+ bday2: int,
104
+ rate1: float,
105
+ rate2: float,
106
+ ) -> float:
107
+ """
108
+ Calcula a taxa a termo (forward rate) entre dois prazos (dias úteis).
109
+
110
+ Utiliza a fórmula:
111
+ f₁→₂ = ((1 + r₂)^(du₂/252) / (1 + r₁)^(du₁/252))^(252/(du₂ - du₁)) - 1
112
+
113
+ Onde:
114
+ - r₁ é a taxa zero para o primeiro prazo (du₁).
115
+ - r₂ é a taxa zero para o segundo prazo (du₂).
116
+ - du₁ é o número de dias úteis até a primeira data.
117
+ - du₂ é o número de dias úteis até a segunda data.
118
+ - A constante 252 representa o número de dias úteis no ano.
119
+
120
+ Como du/252 = t (tempo em anos úteis), a fórmula pode ser simplificada para:
121
+ f₁→₂ = ((1 + r₂)^t₂ / (1 + r₁)^t₁)^(1/(t₂ - t₁)) - 1
122
+
123
+ Args:
124
+ bday1 (int): Número de dias úteis do primeiro ponto (prazo menor).
125
+ bday2 (int): Número de dias úteis do segundo ponto (prazo maior).
126
+ rate1 (float): Taxa zero (spot rate) para o prazo `bday1`.
127
+ rate2 (float): Taxa zero (spot rate) para o prazo `bday2`.
128
+
129
+ Returns:
130
+ float: A taxa a termo calculada entre `bday1` e `bday2`. Retorna
131
+ `float('nan')` se `bday2 <= bday1` ou se qualquer um dos
132
+ argumentos de entrada for NaN.
133
+
134
+ Example:
135
+ >>> # Exemplo correto: bday2 > bday1
136
+ >>> yd.forward(10, 20, 0.05, 0.06)
137
+ 0.0700952380952371
138
+ >>> # Exemplo inválido: bday2 <= bday1
139
+ >>> yd.forward(20, 10, 0.06, 0.05)
140
+ nan
141
+ >>> yd.forward(10, 10, 0.05, 0.05)
142
+ nan
143
+ >>> # Exemplo com NaN na entrada
144
+ >>> yd.forward(10, 20, 0.05, pd.NA)
145
+ nan
146
+
147
+ Note:
148
+ É fundamental que `bday2` seja estritamente maior que `bday1` para que
149
+ o cálculo da taxa a termo seja matematicamente válido.
150
+ """
151
+ if pd.isna(rate1) or pd.isna(rate2) or pd.isna(bday1) or pd.isna(bday2):
152
+ # If any of the inputs are NaN, return NaN
153
+ return float("nan")
154
+
155
+ # Handle the case where the two dates are the same
156
+ if bday2 <= bday1:
157
+ return float("nan")
158
+
159
+ # Convert business days to business years
160
+ t1 = bday1 / 252
161
+ t2 = bday2 / 252
162
+
163
+ # f₁→₂ = ((1 + r₂)^t₂ / (1 + r₁)^t₁)^(1/(t₂ - t₁)) - 1
164
+ return ((1 + rate2) ** t2 / (1 + rate1) ** t1) ** (1 / (t2 - t1)) - 1
@@ -1 +0,0 @@
1
- __version__ = "0.32.2"
@@ -1,56 +0,0 @@
1
- import pandas as pd
2
-
3
-
4
- def forwards(
5
- business_days: pd.Series,
6
- zero_rates: pd.Series,
7
- groupby_dates: pd.Series | None = None,
8
- ) -> pd.Series:
9
- """Calculates forward rates from zero rates.
10
-
11
- Args:
12
- business_days (pd.Series): Number of business days for each zero rate.
13
- zero_rates (pd.Series): Zero rates corresponding to the business days.
14
- groupby_dates (pd.Series | None, optional): Optional grouping criteria to
15
- segment calculations. If not provided, calculations will not be grouped.
16
-
17
- Returns:
18
- pd.Series: Series of calculated forward rates with the first rate set to the
19
- corresponding zero rate.
20
- """
21
- # Reset Series indexes to avoid misalignment issues during calculations
22
- business_days = business_days.reset_index(drop=True)
23
- zero_rates = zero_rates.reset_index(drop=True)
24
- if groupby_dates is not None:
25
- groupby_dates = groupby_dates.reset_index(drop=True)
26
-
27
- # Create a DataFrame to work with the given series
28
- df = pd.DataFrame({"bd": business_days, "zero_rate": zero_rates})
29
-
30
- # If no groupby_dates is provided, create a dummy column to group the DataFrame
31
- if groupby_dates is not None:
32
- df["groupby_date"] = groupby_dates
33
- else:
34
- df["groupby_date"] = 0 # Dummy value to group the DataFrame
35
-
36
- # Sort by the groupby_dates and bd columns to ensure proper chronological order
37
- df.sort_values(by=["groupby_date", "bd"], inplace=True)
38
-
39
- # Calculate the next zero rate and business day for each group
40
- df["next_rate"] = df.groupby("groupby_date")["zero_rate"].shift(1)
41
- df["next_bday"] = df.groupby("groupby_date")["bd"].shift(1)
42
-
43
- # Calculate the forward rates using the formula
44
- factor1 = (1 + df["next_rate"]) ** (df["next_bday"] / 252)
45
- factor2 = (1 + df["zero_rate"]) ** (df["bd"] / 252)
46
- factor3 = 252 / (df["next_bday"] - df["bd"])
47
- df["fwd_rate"] = (factor1 / factor2) ** factor3 - 1
48
-
49
- # Identifify the first index of each group of dates
50
- first_indices = df.groupby("groupby_date").head(1).index
51
- # Set the first forward rate of each group to the zero rate
52
- df.loc[first_indices, "fwd_rate"] = df.loc[first_indices, "zero_rate"]
53
-
54
- df["fwd_rate"] = df["fwd_rate"].astype("Float64")
55
-
56
- return df["fwd_rate"]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes