<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Diederik on Medium]]></title>
        <description><![CDATA[Stories by Diederik on Medium]]></description>
        <link>https://medium.com/@burdivelam?source=rss-9bc5ee3700d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BDqFqQ4KV0P8jZ2cuO4z3w.jpeg</url>
            <title>Stories by Diederik on Medium</title>
            <link>https://medium.com/@burdivelam?source=rss-9bc5ee3700d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 15:21:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@burdivelam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using K-means on the S2FX model of PlanB]]></title>
            <link>https://medium.com/@burdivelam/using-k-means-on-the-s2fx-model-of-planb-be8909248828?source=rss-9bc5ee3700d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/be8909248828</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[btc]]></category>
            <dc:creator><![CDATA[Diederik]]></dc:creator>
            <pubDate>Wed, 07 Oct 2020 21:53:05 GMT</pubDate>
            <atom:updated>2020-10-08T15:03:42.919Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In an article named “<a href="https://medium.com/@100trillionUSD/bitcoin-stock-to-flow-cross-asset-model-50d260feed12">Bitcoin Stock-to-Flow Cross Asset Model</a>” PlanB (@100trillionUSD) describes a new model named the BTC S2F cross asset (S2FX) model. In this new model time is removed and other assets (silver and gold) are added to the model. It’s an update of the original S2F model described <a href="https://medium.com/@100trillionUSD/modeling-bitcoins-value-with-scarcity-91fa0fc03e25">here</a>. If you are not familiar with these articles, I recommend reading them.</p><p>The idea of the model is that for each month since the start of Bitcoin (jan. 2009) the market capitalization is calculated. This is the sum of all BTC mined (stock) at the end of each month multiplied by the monthly closing price in USD. Also, at the end of each month is the inflow of BTC calculated. The stock is then divided by the flow (s2f). We plot all these months (map,s2f) in a log-log plot and we get a set of scattered datapoints. PlanB uses a genetic algorithm to quantify four clusters in these datapoints and makes the following statement:</p><blockquote>Quantifying these clusters can be done by minimizing distance between monthly BTC data and clusters. I use a genetic algorithm (minimizing absolute distance) to quantify four clusters. Future research could focus on different clustering algorithms (e.g. k-means algorithm).</blockquote><p>In this article I will show the results using k-means to cluster the data and will show the Python code how I got my results.</p><h4>TL;DR here is the result!</h4><p>K-means gives the following result:</p><p><em>log(y) = 4.08638 log(x) + 5.52622</em></p><p>with a RMS error = 0.99686</p><p>If we use the same assumptions PlanB uses in his article that “<em>BTC S2F will be 56 in 2020–2024″.</em> Then the market capitalisation will be 10^(4.08638 log(56) + 5.52622) is approx. $ 4.7 x 10¹²</p><p>This translates into a BTC price (given 19M BTC in 2020–2024) of <strong>$247K</strong>.</p><h4>K-means Clustering Algorithm</h4><p>In simplified terms, the K-means algorithm (sometimes called Lloyd’s algorithm) works as follows. <br>Because we don’t know the cluster centers in advance, we start by initializing them to some random values. Then we alternate between two steps.</p><ul><li>One, we assign each data point to the cluster with the closest cluster center.</li><li>And two, we update the cluster center to be the mean of all the data points in its cluster.</li></ul><p>We iterate these last two steps until we can’t make any more changes. That is, we repeat these steps until we converge.</p><h4>Preparation</h4><p>First of all we need data. You can use the full BTC blockchain or you can use aggregated data from a block explorer. I’ve used blockchair.com and coinmetrics.com and they provide similar results. In this article I use the coinmetric.io data. Download the data from:<br><a href="https://coinmetrics.io/community-network-data/#comm-files">https://coinmetrics.io/community-network-data/#comm-files</a></p><p>Lets start by loading the proper libraries:</p><pre>import pandas as pd<br>import numpy as np<br>from datetime import datetime,date<br>from sklearn.cluster import KMeans<br>from sklearn.linear_model import LinearRegression</pre><p>then read the downloaded csv file into a dataframe.</p><pre>df_cm = pd.read_csv(&#39;btc-coinmetrics.csv&#39;) #read coinmetric file</pre><p>You can clean up the dataframe columns but make sure you keep <strong>date, CapMrktCurUSD, IssContNtv, PriceUSD</strong></p><p>Set date to datetime format and create a column for the months in the format 2009–01 to current month.</p><pre>#set date to datetime<br>df_cm[&#39;date&#39;]= pd.to_datetime(df_cm[&#39;date&#39;])<br>df_cm[&#39;year_month&#39;] = df_cm[&#39;date&#39;].apply(lambda x: x.strftime(&#39;%Y-%m&#39;))</pre><p>Create a new dataframe by grouping on “date” on a monthly basis and take the last ‘PriceUSD’ of the month. Then create a column ‘flow’ where every row is the sum of the daily minted BTC over one month. The ‘stock’ is the cumulated sum of the daily ‘flow’. Then calculate the s2f based on 12 months (year). We can calculate the market capitalisation ‘map’ by taking the product of the ‘stock’ and the ‘PriceUSD’.</p><pre>df_s2f = df_cm.groupby(pd.Grouper(key=&#39;date&#39;,freq=&#39;M&#39;))[&#39;PriceUSD&#39;].last().to_frame()<br>df_s2f[&#39;flow&#39;]= df_cm.groupby(pd.Grouper(key=&#39;date&#39;,freq=&#39;M&#39;))[&#39;IssContNtv&#39;].sum().tolist()<br>df_s2f[&#39;stock&#39;] = df_s2f[&#39;flow&#39;].cumsum()<br>df_s2f[&#39;s2f&#39;] = df_s2f[&#39;stock&#39;]/(df_s2f[&#39;flow&#39;]*12)<br>df_s2f[&#39;mcap&#39;] = df_s2f[&#39;PriceUSD&#39;]*df_s2f[&#39;stock&#39;]</pre><p>Finaly we create a column ‘year_month’ with date in the format ‘2009–01’…</p><pre>df_s2f[&#39;year_month&#39;] = df_s2f.index<br>df_s2f[&#39;year_month&#39;] = df_s2f[&#39;year_month&#39;].apply(lambda x: x.strftime(&#39;%Y-%m&#39;))</pre><p>In the first months the price of BTC was not really known. Some block explorers use a $ price of 0,01. Coinmetric uses a Nan value. So we need to drop all rows with Nan values. Also I’ve dropped all months (in my case 6) after the halving to make it comparable with the original model.</p><pre>df_s2f.dropna(inplace=True)<br>df_s2f.drop(df_s2f.tail(6).index,inplace=True)</pre><p>If we plot al months in the dataframe on a log-log scale we get the following graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BtY1ivdEHUdO1suwcHoeQQ.png" /><figcaption>Interactive version <a href="http://www.n-ventures.nl/neural-ventures/s2f/s2f_monthly_data.html">here</a></figcaption></figure><h4>K-means Clustering</h4><p>We use Kmeans from the sklearn.cluster library. First we assign an array with the ‘s2f’ and ‘map’ values to ‘X’. Because we are clustering in a log domain we need to transform ‘X’ to log10.</p><pre>X = df_s2f.iloc[:,[3,4]].values<br>X = np.log10(X)</pre><p>Next we apply Kmeans to ‘X’ and we compute the cluster centers and predict cluster index for each sample in ‘X’. We create a new column in the df_s2f dataframe ‘cluster’ and assign the predicted cluster index for each month. We set number of clusters to 4. K-means++ selects initial cluster centers for k-mean clustering in a smart way to speed up convergence and ensures that you don’t get trapped in local minima . We use a random_state of 42 because…. well… ehh… after all 42 is the answer to all questions.</p><pre>kmeans = KMeans(n_clusters=4,init=&#39;k-means++&#39;,random_state=42)<br>y_kmeans = kmeans.fit_predict(X)<br>df_s2f[&#39;cluster&#39;] = y_kmeans</pre><p>Next step is to use linear regression on the cluster centers. But first we must inverse from the Log10 transformation:</p><pre>centerX = 10 ** kmeans.cluster_centers_[:,0]<br>centerY = 10 ** kmeans.cluster_centers_[:,1]</pre><p>Because PlanB added silver and gold to the model we must add the market cap and s2f of these assets to the cluster centers before we use linear regression:</p><pre>#add silver to centeroids<br>centerX = np.append(centerX, 33)<br>centerY = np.append(centerY, 560000000000)<br>#add gold to centeroids<br>centerX = np.append(centerX, 62)<br>centerY = np.append(centerY, 10088000000000)</pre><p>Then we use Linear regression to find optimal fit:</p><pre>x = np.log10(centerX.reshape((-1,1)))<br>y = np.log10(centerY)<br>model = LinearRegression()<br>model.fit(x, y)</pre><p>This results in the following fit:<br><em>log(y) = 4.08638 log(x) + 5.52622</em><br>with RMS error = 0.99686</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nv6TnWb4enamH6e5EDx3Kg.png" /><figcaption>Interactive version <a href="http://www.n-ventures.nl/neural-ventures/s2f/k-means_s2f_coinmetric_gold+silver.html">here</a></figcaption></figure><p>So there it is!</p><p>Now let’s hope that the model fits reality at least until the gold market cap and s2f is met.</p><h4>Reprise</h4><p>As a bonus I did one extra run with al months including sep-2020.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VNijfIGYRTZUG10Z2Fh08w.png" /><figcaption>Interactive version <a href="http://www.n-ventures.nl/neural-ventures/s2f/k-means_s2f_coinmetric_gold+silver_monthsafter_halvening.html">here</a></figcaption></figure><p>You can see the RMS error is a bit larger because of the points added on the right of the chart. It makes you wonder is this “normal” behaviour or is the model from now on not applicable. If we look back to the previous halvenings (or is it HalFining) from:</p><ul><li>50 to 25 in block 210.000 on 2012–11–28</li><li>25 to 12,5 in block 420.000 on 2016–07–09</li><li>12,5 to 6,25 in block 630.000 on 2020–05–11</li></ul><p>and we isolate 5 months after these events we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o2QDnFd7uoW477Zwf4I_9w.png" /><figcaption>Interactive version <a href="http://www.n-ventures.nl/neural-ventures/s2f/s2f_5_monthly_data.html">here</a></figcaption></figure><p>You can see some similarity in behaviour between the first months after the halvings. At least the one in 2016 compared to 2020…</p><p>Anyway if the model is still applicable we should see some ATH’s very soon!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be8909248828" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>