mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: fmt
This commit is contained in:
@@ -2,7 +2,7 @@ use brk_error::Result;
|
||||
use brk_types::{Bitcoin, Dollars, Indexes, StoredF32};
|
||||
use vecdb::Exit;
|
||||
|
||||
use super::{gini, Vecs};
|
||||
use super::{Vecs, gini};
|
||||
use crate::{distribution, internal::RatioDollarsBp32, market, mining, transactions};
|
||||
|
||||
impl Vecs {
|
||||
@@ -126,22 +126,20 @@ impl Vecs {
|
||||
)?;
|
||||
|
||||
// Supply-Adjusted Dormancy = dormancy / circulating_supply_btc
|
||||
self.dormancy.supply_adjusted
|
||||
.height
|
||||
.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&all_activity.dormancy._24h.height,
|
||||
supply_total_sats,
|
||||
|(i, dormancy, supply_sats, ..)| {
|
||||
let supply = f64::from(Bitcoin::from(supply_sats));
|
||||
if supply == 0.0 {
|
||||
(i, StoredF32::from(0.0f32))
|
||||
} else {
|
||||
(i, StoredF32::from((f64::from(dormancy) / supply) as f32))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
self.dormancy.supply_adjusted.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&all_activity.dormancy._24h.height,
|
||||
supply_total_sats,
|
||||
|(i, dormancy, supply_sats, ..)| {
|
||||
let supply = f64::from(Bitcoin::from(supply_sats));
|
||||
if supply == 0.0 {
|
||||
(i, StoredF32::from(0.0f32))
|
||||
} else {
|
||||
(i, StoredF32::from((f64::from(dormancy) / supply) as f32))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Stock-to-Flow: supply / annual_issuance
|
||||
// annual_issuance ≈ subsidy_per_block × 52560 (blocks/year)
|
||||
@@ -154,9 +152,10 @@ impl Vecs {
|
||||
if annual_flow == 0.0 {
|
||||
(i, StoredF32::from(0.0f32))
|
||||
} else {
|
||||
(i, StoredF32::from(
|
||||
(supply_sats.as_u128() as f64 / annual_flow) as f32,
|
||||
))
|
||||
(
|
||||
i,
|
||||
StoredF32::from((supply_sats.as_u128() as f64 / annual_flow) as f32),
|
||||
)
|
||||
}
|
||||
},
|
||||
exit,
|
||||
@@ -180,26 +179,25 @@ impl Vecs {
|
||||
)?;
|
||||
|
||||
// Seller Exhaustion Constant: % supply_in_profit × 30d_volatility
|
||||
self.seller_exhaustion
|
||||
.height
|
||||
.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&all_metrics.supply.in_profit.sats.height,
|
||||
&market.volatility._1m.height,
|
||||
supply_total_sats,
|
||||
|(i, profit_sats, volatility, total_sats, ..)| {
|
||||
let total = total_sats.as_u128() as f64;
|
||||
if total == 0.0 {
|
||||
(i, StoredF32::from(0.0f32))
|
||||
} else {
|
||||
let pct_in_profit = profit_sats.as_u128() as f64 / total;
|
||||
(i, StoredF32::from(
|
||||
(pct_in_profit * f64::from(volatility)) as f32,
|
||||
))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
self.seller_exhaustion.height.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&all_metrics.supply.in_profit.sats.height,
|
||||
&market.volatility._1m.height,
|
||||
supply_total_sats,
|
||||
|(i, profit_sats, volatility, total_sats, ..)| {
|
||||
let total = total_sats.as_u128() as f64;
|
||||
if total == 0.0 {
|
||||
(i, StoredF32::from(0.0f32))
|
||||
} else {
|
||||
let pct_in_profit = profit_sats.as_u128() as f64 / total;
|
||||
(
|
||||
i,
|
||||
StoredF32::from((pct_in_profit * f64::from(volatility)) as f32),
|
||||
)
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let exit = exit.clone();
|
||||
self.db.run_bg(move |db| {
|
||||
|
||||
@@ -6,7 +6,10 @@ use brk_types::Version;
|
||||
use super::{Vecs, realized_envelope::RealizedEnvelope};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{PerBlock, PercentPerBlock, RatioPerBlock, db_utils::{finalize_db, open_db}},
|
||||
internal::{
|
||||
PerBlock, PercentPerBlock, RatioPerBlock,
|
||||
db_utils::{finalize_db, open_db},
|
||||
},
|
||||
};
|
||||
|
||||
const VERSION: Version = Version::new(1);
|
||||
@@ -35,8 +38,7 @@ impl Vecs {
|
||||
flow: PerBlock::forced_import(&db, "dormancy_flow", v, indexes)?,
|
||||
};
|
||||
let stock_to_flow = PerBlock::forced_import(&db, "stock_to_flow", v, indexes)?;
|
||||
let seller_exhaustion =
|
||||
PerBlock::forced_import(&db, "seller_exhaustion", v, indexes)?;
|
||||
let seller_exhaustion = PerBlock::forced_import(&db, "seller_exhaustion", v, indexes)?;
|
||||
|
||||
let realized_envelope = RealizedEnvelope::forced_import(&db, v, indexes)?;
|
||||
|
||||
|
||||
@@ -4,9 +4,7 @@ use brk_types::{Cents, Height, Indexes, StoredI8, Version};
|
||||
use vecdb::{AnyVec, Database, EagerVec, Exit, PcoVec, ReadableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
cointime,
|
||||
distribution,
|
||||
indexes,
|
||||
cointime, distribution, indexes,
|
||||
internal::{PerBlock, Price, RatioPerBlockPercentiles},
|
||||
prices,
|
||||
};
|
||||
@@ -82,16 +80,48 @@ impl RealizedEnvelope {
|
||||
}
|
||||
|
||||
// Lower percentiles: max across all models (tightest lower bound)
|
||||
self.pct0_5.cents.height.compute_max_of_others(starting_indexes.height, &sources!(pct0_5), exit)?;
|
||||
self.pct1.cents.height.compute_max_of_others(starting_indexes.height, &sources!(pct1), exit)?;
|
||||
self.pct2.cents.height.compute_max_of_others(starting_indexes.height, &sources!(pct2), exit)?;
|
||||
self.pct5.cents.height.compute_max_of_others(starting_indexes.height, &sources!(pct5), exit)?;
|
||||
self.pct0_5.cents.height.compute_max_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct0_5),
|
||||
exit,
|
||||
)?;
|
||||
self.pct1.cents.height.compute_max_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct1),
|
||||
exit,
|
||||
)?;
|
||||
self.pct2.cents.height.compute_max_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct2),
|
||||
exit,
|
||||
)?;
|
||||
self.pct5.cents.height.compute_max_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct5),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Upper percentiles: min across all models (tightest upper bound)
|
||||
self.pct95.cents.height.compute_min_of_others(starting_indexes.height, &sources!(pct95), exit)?;
|
||||
self.pct98.cents.height.compute_min_of_others(starting_indexes.height, &sources!(pct98), exit)?;
|
||||
self.pct99.cents.height.compute_min_of_others(starting_indexes.height, &sources!(pct99), exit)?;
|
||||
self.pct99_5.cents.height.compute_min_of_others(starting_indexes.height, &sources!(pct99_5), exit)?;
|
||||
self.pct95.cents.height.compute_min_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct95),
|
||||
exit,
|
||||
)?;
|
||||
self.pct98.cents.height.compute_min_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct98),
|
||||
exit,
|
||||
)?;
|
||||
self.pct99.cents.height.compute_min_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct99),
|
||||
exit,
|
||||
)?;
|
||||
self.pct99_5.cents.height.compute_min_of_others(
|
||||
starting_indexes.height,
|
||||
&sources!(pct99_5),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let spot = &prices.spot.cents.height;
|
||||
|
||||
@@ -121,10 +151,15 @@ impl RealizedEnvelope {
|
||||
&self.pct99_5.cents.height,
|
||||
];
|
||||
|
||||
let dep_version: Version = bands.iter().map(|b| b.version()).sum::<Version>() + spot.version();
|
||||
let dep_version: Version =
|
||||
bands.iter().map(|b| b.version()).sum::<Version>() + spot.version();
|
||||
|
||||
self.index.height.validate_computed_version_or_reset(dep_version)?;
|
||||
self.index.height.truncate_if_needed(starting_indexes.height)?;
|
||||
self.index
|
||||
.height
|
||||
.validate_computed_version_or_reset(dep_version)?;
|
||||
self.index
|
||||
.height
|
||||
.truncate_if_needed(starting_indexes.height)?;
|
||||
|
||||
self.index.height.repeat_until_complete(exit, |vec| {
|
||||
let skip = vec.len();
|
||||
@@ -142,14 +177,30 @@ impl RealizedEnvelope {
|
||||
let price = spot_batch[j];
|
||||
let mut score: i8 = 0;
|
||||
|
||||
if price < b[3][j] { score -= 1; }
|
||||
if price < b[2][j] { score -= 1; }
|
||||
if price < b[1][j] { score -= 1; }
|
||||
if price < b[0][j] { score -= 1; }
|
||||
if price > b[4][j] { score += 1; }
|
||||
if price > b[5][j] { score += 1; }
|
||||
if price > b[6][j] { score += 1; }
|
||||
if price > b[7][j] { score += 1; }
|
||||
if price < b[3][j] {
|
||||
score -= 1;
|
||||
}
|
||||
if price < b[2][j] {
|
||||
score -= 1;
|
||||
}
|
||||
if price < b[1][j] {
|
||||
score -= 1;
|
||||
}
|
||||
if price < b[0][j] {
|
||||
score -= 1;
|
||||
}
|
||||
if price > b[4][j] {
|
||||
score += 1;
|
||||
}
|
||||
if price > b[5][j] {
|
||||
score += 1;
|
||||
}
|
||||
if price > b[6][j] {
|
||||
score += 1;
|
||||
}
|
||||
if price > b[7][j] {
|
||||
score += 1;
|
||||
}
|
||||
|
||||
vec.push(StoredI8::new(score));
|
||||
}
|
||||
@@ -182,8 +233,12 @@ impl RealizedEnvelope {
|
||||
.sum::<Version>()
|
||||
+ spot.version();
|
||||
|
||||
self.score.height.validate_computed_version_or_reset(dep_version)?;
|
||||
self.score.height.truncate_if_needed(starting_indexes.height)?;
|
||||
self.score
|
||||
.height
|
||||
.validate_computed_version_or_reset(dep_version)?;
|
||||
self.score
|
||||
.height
|
||||
.truncate_if_needed(starting_indexes.height)?;
|
||||
|
||||
self.score.height.repeat_until_complete(exit, |vec| {
|
||||
let skip = vec.len();
|
||||
@@ -233,14 +288,30 @@ impl RealizedEnvelope {
|
||||
let mut total: i8 = 0;
|
||||
|
||||
for model in &bands {
|
||||
if price < model[3][j] { total -= 1; }
|
||||
if price < model[2][j] { total -= 1; }
|
||||
if price < model[1][j] { total -= 1; }
|
||||
if price < model[0][j] { total -= 1; }
|
||||
if price > model[4][j] { total += 1; }
|
||||
if price > model[5][j] { total += 1; }
|
||||
if price > model[6][j] { total += 1; }
|
||||
if price > model[7][j] { total += 1; }
|
||||
if price < model[3][j] {
|
||||
total -= 1;
|
||||
}
|
||||
if price < model[2][j] {
|
||||
total -= 1;
|
||||
}
|
||||
if price < model[1][j] {
|
||||
total -= 1;
|
||||
}
|
||||
if price < model[0][j] {
|
||||
total -= 1;
|
||||
}
|
||||
if price > model[4][j] {
|
||||
total += 1;
|
||||
}
|
||||
if price > model[5][j] {
|
||||
total += 1;
|
||||
}
|
||||
if price > model[6][j] {
|
||||
total += 1;
|
||||
}
|
||||
if price > model[7][j] {
|
||||
total += 1;
|
||||
}
|
||||
}
|
||||
|
||||
vec.push(StoredI8::new(total));
|
||||
|
||||
Reference in New Issue
Block a user