Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/693eaf5a-1494-8007-b1e3-00abefb11523
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Assistant: Below is a clean, ready-to-run Python script that: === Below is a clean, ready-to-run Python script that: # Takes a 1-D price series # Computes optimal bin boundaries using maximum entropy / Freedman-Diaconis optimal binning (you can swap in other methods) # Constructs a tetrahedral volume model # Maps each bin to a proportional “swath” of the tetrahedral volume # Reports the % of the total tetrahedral volume that each binned price region occupies === <syntaxhighlight lang="python">import numpy as np === import pandas as pd === ------------------------------------------------------------- === === 1. Load or define your price series === === ------------------------------------------------------------- === === Example placeholder — replace with your actual prices: === prices = np.array([101, 100, 102, 105, 108, 107, 109, 115, 117, 120]) === ------------------------------------------------------------- === === 2. Compute optimal bins (Freedman–Diaconis rule) === === ------------------------------------------------------------- === def optimal_bins(data): data = np.asarray(data) q75, q25 = np.percentile(data, [75 ,25]) iqr = q75 - q25 bin_width = 2 '' iqr '' len(data) ''' (-1/3) if bin_width == 0: return 1 num_bins = int(np.ceil((data.max() - data.min()) / bin_width)) return max(num_bins, 1) num_bins = optimal_bins(prices) bin_edges = np.linspace(prices.min(), prices.max(), num_bins + 1) === ------------------------------------------------------------- === === 3. Bin the data === === ------------------------------------------------------------- === bin_counts, _ = np.histogram(prices, bins=bin_edges) total_points = len(prices) bin_percentages = bin_counts / total_points === ------------------------------------------------------------- === === 4. Tetrahedral geometry === === ------------------------------------------------------------- === === A regular tetrahedron with edge length L has volume: === === V = L^3 / (6√2) === L = 1 # normalized tetrahedron tetra_volume = L'''3 / (6 * np.sqrt(2)) === Each bin gets a proportional “volume swath” === === swath_volume = (bin_percentage) * (tetra_volume) === swath_volumes = bin_percentages * tetra_volume swath_percentage_of_tetra = swath_volumes / tetra_volume # equals bin % of total === ------------------------------------------------------------- === === 5. Return a structured result === === ------------------------------------------------------------- === df = pd.DataFrame({ "Bin Lower": bin_edges[:-1], "Bin Upper": bin_edges[1:], "Count": bin_counts, "Price % of Total": np.round(bin_percentages * 100, 2), "Tetrahedral Volume %": np.round(swath_percentage_of_tetra * 100, 2), }) print("\nOptimal Binning + Tetrahedral Swath Mapping\n") print(df.to_string(index=False)) print("\nTotal tetrahedral volume (normalized L=1):", tetra_volume) </syntaxhighlight>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)