Freitag, 2. November 2018
A marketplace for machine learning models? onnx, pmml or pfa?
A simple search for "marketplace for machine learning models" reveals that a company called Algorithmia has built such a thing and has over 5000 models in the marketplace, most of which are machine learning related. However, if one wants to use/buy a model, one has to trust a third party, namely Algorithmia, to run that model. Also, the cost to use a model is based on the number of request. Hence the buyer does not own the model. There is another option, which does not exist yet to my knowledge, namely to directly sell the machine learning model to the buyer. This option is proposed here: https://arxiv.org/pdf/1805.11450.pdf. Although the paper is very technical, the main idea is to directly sell the machine learning model. This decouples the process of buying and running a model. Since models are trained on data and later scored on new data, this means, that the seller of the model and the buyer of the model, must have access to some similar dataset. This could be achieved by using open data. This is also the reason, why most of the models on Algorithmia are based on commond data, such as text and images. The data scientist trains his model on open data to solve a specific problem and then uploads his model to a marketplace. The buyer, could then upload open train data to the marketplace and let the model score the data to see how well the model performs. So he does not have to trust the seller of the model in terms of model accurracy. Based on the performance of different models, the buyer of the model, can choose the model, with the best accurracy and then the buyer can decide where to run the model, either on the cloud or on premise. This raises the question: What actually is a machine learning model? You could say, that a machine learning model is a specification of how to produce an output given some input. The next question is, how to transport a model from seller to buyer? This could be achieved using a model-format such as ONNX, PMML or PFA (portable format for analytics). The next question is , which of these model-format will be used? This is a wrong question to be posed. Why? Because as there are many document formats ( doc, docx, pdf, odf etc.) there can be many model-based formats. The analogy to document formats makes one thing clear: There are producers of documents and consumer of documents. The producers can nowadays choose from a set of marketplaces [just google: marketplace sell pdf], where to sell the document. Similarily the buyer can choose where to buy. This is only possible by decoupling the process of creating a document and viewing the document. Thus, it should be possible to do the same with machine learning models, using model-formats. A company comes to mind, where already a lot of data scientists, compete for producing the best model: Kaggle. Thus Kaggle or similar websites, might be candidates for offering such a marketplace for "opend-data-ml-models". The only thing they have to do, is offering a service for data scientists to export their "kernels" to a model-format such as ONNX, PMML or PFA and then let companys find the best model which solves their problem.
Edit: I wrote a request to Kaggle, but did not get a response, so I decided to start an experiment, to see how the idea eveolves:
mlmarketplace, buy and sell onnx, pmml, pfa
What is your opinion on this topic? Comments are welcome!
Please share this article if you like the idea.
Dienstag, 24. Januar 2017
A note in Galois Theory
Theorem 1:
Let $$\mathbb{G} = \{ \sigma_1,\cdots,\sigma_n\}$$ be a finite group, $$x_1,\cdots,x_n$$ be indeterminates
and let $$\rho$$ be the regular representation of $$\mathbb{G}$$. Let $$x = (x_1,\cdots,x_n)^T$$ and $$G = (\rho(\sigma_1)x,\cdots,\rho(\sigma_n)x)$$ , $$A' = diag(x_1,\cdots,x_n)$$.
Set $$A=GA'G^{-1}$$. Suppose there exists $$\alpha:=(\alpha_1,\cdots,\alpha_n) \in \mathbb{C}^n$$ such that:
1) $$det(G(\alpha)) \neq 0$$
2) $$A = A(\alpha)$$ has rational coefficients
3) the characteristic polynomial $$\chi_A(t)$$ is irreducible over $$\mathbb{Q}$$.
Then $$Gal(\chi_A(t)/\mathbb{Q}) = \mathbb{G}$$.
Proof:
On the one hand we have
\[ GA' = AG = GA' = (x_1\rho(\sigma_1)x,\cdots,x_n\rho(\sigma_n)x) \]
On the other hand we have
\[ GA' = AG = A (\rho(\sigma_1)x,\cdots,\rho(\sigma_n)x) = (A\rho(\sigma_1)x,\cdots,A\rho(\sigma_n)x) \]
and it follows that
\[
(A\rho(\sigma_1)x,\cdots,A\rho(\sigma_n)x) = GA' = (x_1\rho(\sigma_1)x,\cdots,x_n\rho(\sigma_n)x)
\]
and so we get
\begin{equation}\label{eq:1}
A\rho(\sigma_i)x = x_i \rho(\sigma_i)x \text{ for } i=1,\cdots,n
\end{equation}
Let $$K:=\mathbb{Q}(\alpha_1,\cdots,\alpha_n)$$. From $$A\rho(\sigma_i) \alpha = \alpha_i \rho(\sigma_i)\alpha$$ follows
for $$i=1$$, $$\rho(\sigma_1) = 1$$ that $$A\alpha = \alpha_1 \alpha$$. Application of $$\tau \in Gal(K/\mathbb{Q})$$
at the coefficients of the last equation gives us with $$\tau(A) = A$$ (since $$A$$ has rational coefficients)
that
\begin{equation}\label{eq:2}
A\tau(\alpha) = \tau(\alpha_1)\tau(\alpha) = \alpha_{\tau} \tau(\alpha)
\end{equation}
where we have set $$\alpha_{\tau} = \tau(\alpha_1)$$. This means that $$\tau(\alpha)$$ is an eigenvector of $$A$$ to the
eigenvalue $$\alpha_{\tau}$$. But because of equation $$(1)$$ we have:
\begin{equation}\label{eq:3}
A\rho(\sigma_{\tau}) \alpha = \alpha_{\tau} \rho(\sigma_{\tau}) \alpha
\end{equation}
Since the eigenvectors $$\rho(\sigma_1)\alpha,\cdots,\rho(\sigma_n)\alpha$$ of $$A$$ build a basis of $$K^n$$, it means
that every eigenspace has dimension $$1$$. Especially there exists $$a_{\tau} \in K$$ such that
\begin{equation}\label{eq:4}
\tau(\alpha) = a_{\tau} \rho(\sigma_{\tau}) \alpha
\end{equation}
If we build the polynomials with roots from the coefficients of the left and right side of $$(4)$$, then they must
be because of $$(4)$$ be equal:
\begin{equation}\label{eq:5}
\chi_A(t) = \prod_{i=1}^n (t -\alpha_i) =^{(4)} \prod_{j=1}^n (t - a_{\tau} \alpha_j)
\end{equation}
This means that the coefficients of both polynomials must be equal, especially we get
\begin{equation}\label{eq:6}
\sum_{i=1}^n \alpha_i = a_{\tau} \sum_{i=1}^n \alpha_i
\end{equation}
By a theorem of Frobenius concerning the factorization of the group determinant, we have
\begin{equation}\label{eq:7}
det( G(x_1,\cdots,x_n)) = (x_1+\cdots+x_n) p(x_1,\cdots,x_n)
\end{equation}
for some polynomial $$p$$. From $$(7)$$ follows by assumption
\[
0 \neq det(G(\alpha_1,\cdots,\alpha_n)) = (\alpha_1+\cdots+\alpha_n) p(\alpha_1,\cdots,\alpha_n)
\]
which means that $$0 \neq \alpha_1+\cdots+\alpha_n$$ and because of $$(6)$$ we get
\begin{equation}\label{eq:8}
a_{\tau} = 1
\end{equation}
From $$(4)$$ and $$(8)$$ it follows that
\begin{equation}\label{eq:9}
P_{\tau} \alpha = \tau(\alpha) = \rho(\sigma_{\tau}) \alpha
\end{equation}
where $$P_{\tau}$$ is the permutation matrix which corresponds to $$\tau$$. Since
\[
\chi_A(t) = \prod_{i=1}^n t-\alpha_i
\]
is separable, it follows that $$\alpha_k \neq \alpha_l$$ for $$k \neq l$$.
This means that the two permutation matrices $$P_{\tau}$$ and $$\rho(\sigma_{\tau})$$ from $$(9)$$ must be equal,
that is
\begin{equation}\label{eq:10}
P_{\tau} = \rho(\sigma_{\tau})
\end{equation}
From this we get a mapping:
\[
\phi: Gal(K/\mathbb{Q}) \rightarrow \mathbb{G}, \tau \mapsto \sigma_{\tau}
\]
$$\phi$$ is injective:
Let $$\sigma_{\tau} = \phi(\tau) = \phi(\tau') = \sigma_{\tau'}$$. Then we get
\[
P_{\tau} =^{(10)} \rho(\sigma_{\tau}) = \rho(\sigma_{\tau'}) = ^{(10)} P_{\tau'}
\]
and since the regular representation $$P$$ is faithful, it follows that $$\tau = \tau'$$.
$$\phi$$ is surjective:
Let $$m = |Gal(K/\mathbb{Q})|$$, $$n=|\mathbb{G}|=deg(\chi_A(t))$$.
Since $$\chi_A(t)$$ is irreducible, $$deg(\chi_A(t)) = n$$ is a divisor of $$m = |Gal(K/\mathbb{Q})|$$ and
it follows that $$n \le m$$. Since $$\phi$$ is injective it follows that $$m \le n$$, which means that $$m=n$$.
Since the mapping $$\phi$$ is injective on two finite sets of equal size, this means that $$\phi$$ must also
be surjective.
$$\phi$$ is an antihomomorphism of groups:
\[ \tau(\alpha) = \rho(\sigma_m)\alpha \rightarrow \phi(\tau) = \sigma_m\]
\[ \tau'(\alpha) = \rho(\sigma_k)\alpha \rightarrow \phi(\tau') = \sigma_k\]
\[ (\tau \tau')(\alpha) = \rho(\sigma_l)\alpha \rightarrow \phi(\tau \tau') = \sigma_l\]
From this it follows that:
\[
\rho(\sigma_l)\alpha = (\tau \tau')(\alpha) = \tau(\tau'(\alpha)) = \tau(\rho(\sigma_k) \alpha) = \rho(\sigma_k) \tau(\alpha) = \rho(\sigma_k) \rho(\sigma_m)\alpha
\]
Since $$\alpha_i$$ are separable, it follows that the permutation matrices $$\rho(\sigma_l)$$ and $$\rho(\sigma_k \sigma_m)$$ must be equal:
\[
\rho(\sigma_l) = \rho(\sigma_k \sigma_m)
\]
and from this, since the regular representation is faithful, it follows that $$\sigma_l = \sigma_k \sigma_m$$.
From this we get:
\[
\phi(\tau \tau') = \sigma_l = \sigma_k \sigma_m = \phi(\tau') \phi(\tau)
\]
Since for two finite groups which are antiisomorph it follows that they are isomorph (if $$\phi(xy)= \phi(y)\phi(x)$$ is an antihomomorphism, then $$\Phi(x):=\phi(x)^{-1}$$ is a homomorphism),
we conclude that
\[
Gal(K/\mathbb{Q}) = \mathbb{G}
\]
Definition:
Let $$\mathbb{G}$$ be a finite group with $$n$$ elements. Let $$\mathbb{Q}[x_1,\cdots,x_n]^{\mathbb{G}} = \mathbb{Q}[g_1,\cdots,g_m]$$. Then there exist polynomials $$s_j \in \mathbb{Q}[y_1,\cdots,y_m]$$ for $$j=1,\cdots,n$$ such that
$$e_j(x_1,\cdots,x_n) = s_j(g_1(x_1,\cdots,x_n), \cdots, g_m(x_1,\cdots,x_n))$$ for $$j=1,\cdots,n$$ where $$e_j$$ is the $$j$$-elementary symmetric polynomial in $$x_k$$. Consider the polynomial
$$p(t,y_1,\cdots,y_m) = t^n - s_1(y_1,\cdots,y_m) t^{n-1}+\cdots+(-1)^n s_n(y_1,\cdots,y_m)$$ which is a polynomial in $$\mathbb{Q}[t,y_1,\cdots,y_m]$$. We call $$p$$ the corresponding polynomial of the group $$\mathbb{G}$$ to the invariants
$$g_1,\cdots,g_m$$. Let $$I=\{h \in \mathbb{Q}[y_1,\cdots,y_m] | h(g_1,\cdots,g_m) = 0 \}$$. Define the variety $$V_{\mathbb{G}} = \{ a \in \mathbb{Q}^m | h(a) = 0 \forall h \in I \}$$. We say that a polynomial is Hilbert irreducible over some variety,
if it is irreducible and there exists a point in the variety such that the polynomial remains irreducible after specializing to the point of the variety and that other polynomials are not zero on this point.
Lemma 1:
The corresponding polynomial is irreducible in $$\mathbb{Q}[t,y_1,\cdots,y_m]$$.
Proof:
If the polynomial factored in a non-trivial way, then because of the $$t^n$$ term the factors must have degree less than $$n$$ in $$t$$ (consider the factorization in $$R=\mathbb{Q}(y_1,y_2,\ldots,y_n)[t]$$; note also that we can assume that the factors over $$R$$ are monic polynomials in $$t$$). Now specialise via $$y_i\mapsto g_i$$ and we get a non-trivial factorization of the specialised polynomial in $$\mathbb{Q}[g_1,\ldots,g_m][t]$$ and hence in $$\mathbb{Q}[x_1,\ldots,x_n][t]$$ and so in $$\mathbb{Q}(x_1,\ldots,x_n)[t]$$. But we know the complete factorization in this ring, it's just $$\prod(t-x_i)$$, so our given factorization must specialise into factors of the form $$\prod_{i\in I}(t-x_i)$$ for some subsets $$I$$ of $$\{1,2,\ldots,n\}$$ (with each $$I$$ not empty or the whole thing), and the constant term of each factor must be in $$\mathbb{Q}[g_1,\ldots,g_m]$$ and hence $$G$$-invariant. This is a contradiction.
Lemma 2:
Let $$f_1,\cdots,f_r \in \mathbb{Q}(x_1,\cdots,x_n)^{\mathbb{G}}$$ and suppose that the corresponding polynomial of $$\mathbb{G}$$ is Hilbert irreducible over the variety $$V_G$$.
Then there exists $$\alpha = (\alpha_1,\cdots,\alpha_n) \in \mathbb{C}^n$$ such that:
1) $$f_1(\alpha),\cdots,f_r(\alpha)$$ are rational numbers.
2) $$\prod_{i=1}^n t - \alpha_i$$ is irreducible in $$\mathbb{Q}[t]$$.
Proof:
We can write
\[
f_i = \frac{P_i(g_1,\cdots,g_m)}{Q_i(g_1,\cdots,g_m)} \text{ for } P_i,Q_i \in \mathbb{Q}[y_1,\cdots,y_m]
\]
The corresponding polynomial $$p(t,y_1,\cdots,y_m)$$ is irreducible in $$\mathbb{Q}[t,y_1,\cdots,y_m]$$ by Lemma 1. By assumption, we can find $$(a_1,\cdots,a_m)$$ in $$V_{\mathbb{G}}$$ such that:
1) $$p(t,a_1,\cdots,a_m)$$ is irreducible in $$\mathbb{Q}[t]$$.
2) $$Q_i(a_1,\cdots,a_m) \neq 0$$ for $$i=1,\cdots,r$$.
By application of the Hilbert Nullstellensatz we can find $$\alpha =( \alpha_1,\cdots,\alpha_n) \in \mathbb{C}^n$$ such that $$g_i(\alpha) = a_i$$ for $$i=1,\cdots,m$$.
Then
\[
e_j(\alpha) = s_j(g_1(\alpha),\cdots,g_m(\alpha)) = s_j(a_1,\cdots, a_m)
\]
and the polynomial $$p(t,a_1,\cdots,a_m)$$ factors over $$\mathbb{C}$$ as
$$p(t,a_1,\cdots,a_m) = \prod_{i=1}^n t - \alpha_i $$.
Then we get
\[
f_i(\alpha) = \frac{P_i(g_1(\alpha),\cdots,g_m(\alpha))}{Q_i(g_1(\alpha),\cdots,g_m(\alpha))} = \frac{P_i(a_1,\cdots,a_m)}{Q_i(a_1,\cdots,a_m)}
\]
is a rational number, which was to be shown.
Lemma 3:
Let the notation be as in Theorem 1. Then the entries $$a_{ij}$$ of the matrix $$A$$ are elements of $$\mathbb{Q}(x_1,\cdots,x_n)^{\mathbb{G}}$$.
Proof:
We have $$A(x) = G(x) A'(x) G(x)^{-1}$$ and want to show that $$A(\rho(\sigma)x) = A(x)$$ for all $$\sigma \in \mathbb{G}$$.
For this, it is sufficient to show that
\[
A'(\rho(\sigma)x) = \rho(\sigma)A'(x)\rho(\sigma)^{-1}
\]
and
\[
G(\rho(\sigma)x) = G(x) \rho(\sigma)^{-1}
\]
as it follows that
\[
A(\rho(\sigma) x) = G(\rho(\sigma)x) A'(\rho(\sigma) x ) G(\rho(\sigma) x) ^{-1}
\]
\[
= G(x) \rho(\sigma)^{-1} \rho(\sigma) A'(x) \rho(\sigma)^{-1} \rho(\sigma) G(x)^{-1}
\]
\[
= G(x) A'(x) G(x)^{-1} = A(x)
\]
Corollary 1:
Let $$\mathbb{G}$$ be a finite group such that the correspoding polynomial is Hilbert irreducible over $$V_{\mathbb{G}}$$.
Then $$\mathbb{G}$$ is realizable over $$\mathbb{Q}$$ as a Galois group.
Proof:
Consider the regular representation of $$\mathbb{G}$$ and let $$A = (a_{ij})$$ be defined as in Theorem 1.
Then by Lemma 3 we have $$a_{ij} \in \mathbb{Q}(x_1,\cdots,x_n)^{\mathbb{G}}$$. Hence
by Lemma 2 there exists $$\alpha \in \mathbb{C}^n$$ such that
1) $$a_{ij}(\alpha)$$ are rational numbers.
2) The characteristic polynomial $$\chi_A(t)=\prod_{i=1}^n t-\alpha_i$$ is irreducible in $$\mathbb{Q}[t]$$.
Since $$a_{ij}(\alpha)$$ are rational numbers, the matrix $$A(\alpha) = G(\alpha) A'(\alpha) G(\alpha)^{-1}$$
is defined for $$\alpha$$, hence $$det(G(\alpha)) \neq 0$$.
By Theorem 1 therefore it follows that $$Gal(\chi_A(t) / \mathbb{Q}) = \mathbb{G}$$.
Theorem 2:
The following are equivalent:
1) Hilbert Irreducibility can be applied to the corresponding polynomial $$p(t,y_1,\cdots,y_m)$$ over the variety $$V_\mathbb{G}$$
2) Every finite group $$\mathbb{G}$$ is Galois over $$\mathbb{Q}$$.
Proof:
1) $$\rightarrow$$ 2): Corollary 1.
2) $$\rightarrow$$ 1)
Let $$\mathbb{G} = Gal(K/\mathbb{Q}) = \{ \sigma_1,\cdots,\sigma_n\}$$ be a Galois group. By the normal
basis theorem there exists a $$\theta \in K$$ such that $$\sigma_1(\theta),\cdots,\sigma_n(\theta)$$ build a
basis of $$K$$ over $$\mathbb{Q}$$. Let $$\mathbb{Q}[x_1,\cdots,x_n]^\mathbb{G} = \mathbb{Q}[g_1,\cdots,g_m]$$ where
$$\mathbb{G}$$ acts through the regular representation. Let $$I=\{h \in \mathbb{Q}[y_1,\cdots,y_m] | h(g_1,\cdots,g_m)=0\}$$
and $$V_\mathbb{G} = \{ a \in \mathbb{Q}^m | h(a) = 0 \forall h \in I \}$$.
Let now $$Q_1,\cdots,Q_r \in \mathbb{Q}[y_1,\cdots,y_m]$$ be given, such that $$Q_i \notin I$$ for $$i=1,\cdots,r$$. Let $$Q:=Q_1\cdots Q_r$$.
Since $$I$$ is a prime ideal and $$Q_i \notin I$$, we must have $$Q \notin I$$, which means that there exists $$b \in \mathbb{Q}^n$$ such that
\[ Q(g_1(b),\cdots,g_m(b)) \neq 0 \]
that is $$\hat{f}(x_1,\cdots,x_n) = Q(g_1(x_1,\cdots,x_n),\cdots,g_m(x_1,\cdots,x_n))$$ is a reduced polynomial ( since $$\mathbb{Q}$$ has infinite elements)
(see Algebra, Lang, p. 177, p. 312).
Since the $$\sigma_i$$ by Artin (p. 311, Th. 12.2 Algebra, Lang) are algebraically independent and $$\hat{f}$$ is reduced,
there exists $$\alpha \in K$$, such that
\[
0 \neq Q(g_1(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)),\cdots,g_m(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)))
\]
Let $$\alpha = \sum_{i=1}^n{\alpha_i \sigma_i(\theta)}$$ with $$\alpha_i \in \mathbb{Q}$$
Then we have for $$i=1,\cdots,m$$ that $$g_i(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)) = p_i(\alpha_1,\cdots,\alpha_n)$$ is a rational number,
where $$p_i$$ is some polynomial in $$\mathbb{Q}[x_1,\cdots,x_n]$$.
But then also
\[ 0 \neq Q(g_1(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)),\cdots,g_m(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)))
\]
\[
= Q(p_1(\alpha_1,\cdots,\alpha_n),\cdots, p_m(\alpha_1,\cdots,\alpha_n))
\]
\[
= q(\alpha_1,\cdots,\alpha_n)
\]
for some polynomial $$q \in \mathbb{Q}[x_1,\cdots,x_n]$$. We can choose the $$\alpha_i$$ such that $$\alpha_i \neq \alpha_j$$ for $$i \neq j$$,
otherwise replace $$\hat{\alpha}_j = \alpha_i + u$$ for some rational number $$u \neq 0$$ and we get the polynomial in u:
\[
q(u) := q(\alpha_1,\cdots,\alpha_{j-1}, \alpha_i + u , \alpha_{j+1}, \cdots, \alpha_n)
\]
Since $$q(0) \neq 0$$, $$q$$ is not the zero polynomial in $$\mathbb{Q}[u]$$. By choosing $$u \neq 0$$ as a rational number to avoid the
finitely many roots of $$q$$, we get $$q(u) \neq 0$$ and $$\hat{\alpha}_j = \alpha_i + u \neq \alpha_i$$.
But then also $$\sigma_i(\alpha) \neq \sigma_j(\alpha)$$ for $$i \neq j$$, since $$\alpha$$ has pairwise distinct coordinates $$\alpha_k \neq \alpha_l$$ to
the given basis $$\sigma_1(\theta),\cdots,\sigma_n(\theta)$$. The rational and separable polynomial
\[
\prod_{i=1}^n (x -\sigma_i(\alpha))
\]
is irreducible in $$\mathbb{Q}[x]$$, since the Galois group operates transitively on the roots.
Let $$a_i := g_i(\sigma_1(\alpha),\cdots,\sigma_n(\alpha))$$ for $$i=1,\cdots,m$$.
Then we have $$a:=(a_1,\cdots,a_m)$$ is an element of $$V_\mathbb{G}$$ and also
\[
p(t,a_1,\cdots,a_m) = \sum_{i=0}^n (-1)^i s_i(a_1,\cdots,a_m) t^{n-i}
\]
\[
= \sum_{i=0}^n (-1)^i s_i(g_1(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)),\cdots,g_m(\sigma_1(\alpha),\cdots,\sigma_n(\alpha))) t^{n-i}
\]
\[
= \sum_{i=0}^n (-1)^i e_i(\sigma_1(\alpha),\cdots,\sigma_n(\alpha)) t^{n-i}
\]
\[
= \prod_{i=1}^n (t -\sigma_i(\alpha))
\]
is an irreducible polynomial in $$\mathbb{Q}[t]$$. Furthermore we have
$$Q(a_1,\cdots,a_m) \neq 0$$, which means that $$Q_j(a_1,\cdots,a_m) \neq 0$$ for all $$j=1,\cdots,r$$, which was to be shown.
This note can be downloaded from here (A note in Galois Theory, Orges Leka) as a pdf file.
Dienstag, 29. November 2016
Tabelle zum Wertverlust pro Jahr bei Gebrauchtwagen
In diesem Post wollen wir den Wertverlust bei Gebrauchtwagen ermitteln.
Als Datensatz zur Beantwortung dieser Frage dient uns folgender Datensatz welcher von Ebay Kleinanzeigen stammt und Informationen zu über 370000 Gebrauchtwagen enthält. Um den durchschnittlichen Preis pro Gebrauchtwagen in Abhängigkeit vom Erstzulassungsjahr, Kilometerstand und Leistung in PS zu ermitteln, benutzen wir folgende mathematische Formel (Lineare Regression, Adjusted R-squared: 0.9047)
\[
\text{ preis } = \exp( a\cdot \text{ jahr } + b\cdot \text{ km } + c \cdot \text{ ps } + d + \epsilon )
\]
wobei die Werte a,b,c,d aus der Regression stammen.
Ein durschnittliches Auto fährt (nach dem Datensatz oben) ca. 12000 km pro Jahr. Der Wertverlust nach einem Jahr lässt sich berechnen als
\[
w = 1- \frac{p'}{p} = 1 - \frac{ \exp(12000\cdot b)}{\exp(a)}
\]
wobei p der aktuelle Preis und p' der Preis in € nach einem Jahr ist.
Setzen wir die Werte für a und b aus der Regression ein, erhalten wir w = 0.119739, d.h. der Werteverlust eines durchschnittlichen Gebrauchtwagens beträgt ca. 12% pro Jahr.
Aus dieser Angabe können wir eine Tabelle erstellen, die uns anzeigt wie sich der Preis eines durchschnittlichen Gebrauchtwagens ungefähr entwickeln wird.
Alle Angaben in der Tabelle sind in € und ohne Gewähr!
| jetzt | nach 1 Jahr | nach 2 Jahren | nach 3 Jahren | nach 4 Jahren | nach 5 Jahren |
|---|---|---|---|---|---|
| 1000 | 880 | 775 | 682 | 600 | 529 |
| 2000 | 1761 | 1550 | 1364 | 1201 | 1057 |
| 3000 | 2641 | 2325 | 2046 | 1801 | 1586 |
| 4000 | 3521 | 3099 | 2728 | 2402 | 2114 |
| 5000 | 4401 | 3874 | 3410 | 3002 | 2643 |
| 6000 | 5282 | 4649 | 4092 | 3602 | 3171 |
| 7000 | 6162 | 5424 | 4775 | 4203 | 3700 |
| 8000 | 7042 | 6199 | 5457 | 4803 | 4228 |
| 9000 | 7922 | 6974 | 6139 | 5404 | 4757 |
| 10000 | 8803 | 7749 | 6821 | 6004 | 5285 |
| 11000 | 9683 | 8523 | 7503 | 6604 | 5814 |
| 12000 | 10563 | 9298 | 8185 | 7205 | 6342 |
| 13000 | 11443 | 10073 | 8867 | 7805 | 6871 |
| 14000 | 12324 | 10848 | 9549 | 8406 | 7399 |
| 15000 | 13204 | 11623 | 10231 | 9006 | 7928 |
Dienstag, 8. November 2016
Mixed Integer Programming to solve a retail category price optimization problem
In this post we are going to implement the procedure given in "A fractional programming approach for retail category price optimization" by S. Subramanian and H. D. Sherali.
We are going to use the SCIP optimization suite with Python bindings.
I have constructed a small example with n = 2 products and m = 3 prices per product.
Here is the python code:
"""
Solves the retail category price optimization in "A fractional programming approach for retail category
price optimization" from S. Subramanian and H. D. Sherali
Copyright (c) by Orges Leka (2016)
"""
from pyscipopt import Model, quicksum
import math
"""
"""
def retail(n,m,P, d, p0, l, u, phi0, phiMax, R0, elast, mu, lmbda, alpha, beta, K, a,b,c,yMin,yMax):
model = Model("retail price")
v, r, g, x, z = {}, {}, {}, {},{}
y = model.addVar(vtype="CONTINUOUS",name="y")
phi = model.addVar(vtype="CONTINUOUS",name="phi")
w = model.addVar(vtype="CONTINUOUS",name="w")
for i in range(1,n+1):
for j in range(1,m+1):
v[i,j] = math.exp(mu[i]+lmbda[i]*P[i,j])
r[i,j] = v[i,j] * P[i,j]
g[i,j] = v[i,j] * (P[i,j] - d[i])
x[i,j] = model.addVar(vtype="CONTINUOUS", name="x(%s,%s)"%(i,j))
z[i,j] = model.addVar(vtype="BINARY",name="z(%s,%s)"%(i,j))
model.addCons(quicksum(v[i,j]*x[i,j] for (i,j) in x) == phi) # 3b
model.addCons( w == math.log(phi0) + elast/(1.0*n) * quicksum( math.log(P[i,j]/p0[i])*z[i,j] for (i,j) in z ) ) # 3d
model.addCons( alpha*phi0 <= phi ) #3e
model.addCons( phi <= phiMax ) # 3e
model.addCons( math.log(alpha*phi0) <= w ) # 3e
model.addCons( w <= math.log(phiMax) ) # 3e
model.addCons( quicksum( r[i,j]*x[i,j] for (i,j) in x ) >= beta * R0 ) #3f
for k in K.keys():
model.addCons( a[k] * quicksum( P[k,l]*z[k,l] for l in range(1,m+1) ) <= b[k] * quicksum( P[k,l] * z[k,l] for l in range(1,m+1) ) + c[k] ) # 3g
for k in K.keys():
model.addCons( a[k] * quicksum( P[k,l]*x[k,l] for l in range(1,m+1) ) <= b[k] * quicksum( P[k,l] * x[k,l] for l in range(1,m+1) ) + c[k]*y ) # 3h
for i in range(1,n+1):
model.addCons( quicksum( z[i,j] for j in range(1,m+1)) == 1) # 3i
for i in range(1,n+1):
model.addCons( quicksum( x[i,j] for j in range(1,m+1)) == y) # 3j
for i in range(1,n+1):
for j in range(1,m+1):
model.addCons( x[i,j] <= yMax * z[i,j] ) # 3k
model.addCons( x[i,j] >= yMin * z[i,j] ) # 3l
model.addCons( yMin <= y ) # 3m
model.addCons( y <= yMax ) # 3m
model.addCons( phi >= alpha*phi0*(1+w-math.log(alpha*phi0)) ) # 4b. T=2
model.addCons( phi >= phiMax*(1+w-math.log(phiMax)) ) # 4b . T = 2
model.addCons( phi <= (phiMax-alpha*phi0)/(math.log(phiMax)-math.log(alpha*phi0))*(w-math.log(alpha*phi0))+alpha*phi0 )# 4a, S= 1
model.setObjective(quicksum(g[i,j]*x[i,j] for (i,j) in x), "maximize") # 3a
print g
return model
def generateExample():
n = 2
m = 3
P = { (1,1):2.49, (1,2):2.59, (1,3) : 2.69,
(2,1):8.99, (2,2):9.09, (2,3) : 9.19 }
d = { 1 : 1.49, 2 : 7.49 }
p0 ={ 1 : 2.50, 2 : 9.05 }
l = { 1 : 2.00, 2 : 8.49 }
u = { 1 : 3.00, 2 : 9.99 }
phi0 = 500
phiMax = 700
R0 = 50
elast = -2.0
mu = { 1 : 0.1, 2: 0.2}
lmbda = { 1: 0.02, 2: 0.01 }
alpha = 0.8
beta = 0.9
K = { 1 : 1}
a = { 1 : 0.5}
b = { 1 : 1.5}
c = { 1 : 0.5}
yMin = 0.0
yMax = 1000.0
return n,m,P, d, p0, l, u, phi0, phiMax, R0, elast, mu, lmbda, alpha, beta, K, a,b,c, yMin, yMax
if __name__ == "__main__":
n,m,P, d, p0, l, u, phi0, phiMax, R0, elast, mu, lmbda, alpha, beta, K, a,b,c,yMin,yMax = generateExample()
model = retail( n,m,P, d, p0, l, u, phi0, phiMax, R0, elast, mu, lmbda, alpha, beta, K, a,b,c,yMin,yMax )
model.hideOutput() # silent mode
model.optimize()
cost = model.getObjVal()
print()
print("Optimized:")
print("Optimal value:", cost)
#model.printAttr("X")
for v in model.getVars():
print(v.name, "=", model.getVal(v))
Please refer to the article for the meaning of the variables.
Freitag, 21. Oktober 2016
Zusammenhang zwischen Rang und Tore im Fussball
Im folgenden Post wollen wir die Daten aus Kaggle analysieren.
In dem Datensatz sind über 25000 Spiele mit 300 Mannschaften vorhanden. Ich habe die Mannschaften absteigend sortiert nach der Gesamtanzahl der Tore, die sie geschossen haben. Diese Sortierung wollen wir Rang nennen. Eine Mannschaft mit vielen Toren steht vor einer anderen Mannschaft mit wenigen Toren, d.h. die erste Mannschaft hat einen niedrigeren Rang als die zweite.
Zwischen dem Rang und der Gesamtanzahl an Toren besteht ein mathematischer Zusammenhang. Dazu wollen wir uns zuerst eine Grafik anschauen.
Wie man sieht werden die Punkte (Rang/Gesamtanzahl an Toren) gut beschrieben durch die grüne Funktion.
Der Zusammenhang ist wie folgt:
\[ T = a_0 + a_1 \cdot \log(R) + a_2 \cdot \log(R)^2 + \epsilon\]
\[ T = 706.037 + 24.20481 \cdot \log(R) - 25.32589 \cdot \log(R)^2 +\epsilon \]
wobei T die Gesamtanzahl der Tore und R der Rang der Mannschaft ist und epsilon eine normalverteilte Zufallsvariable mit Mittelwert 0 und Standardabweichung 21.49643 ist.
Die Berechnungen und die Grafik wurden mit R und MySQL gemacht:
#!/usr/bin/Rscript
source("connectToDB.R")
query <- "select sum(tg) as stg from
(
select home_team_api_id as team_api_id, sum(home_team_goal) tg from game where home_team_api_id is not null and away_team_api_id is not null group by home_team_api_id
union
select away_team_api_id as team_api_id, sum(away_team_goal) tg from game where home_team_api_id is not null and away_team_api_id is not null group by away_team_api_id
) as tmp group by team_api_id order by stg desc;";
x <- dbGetQuery(con, query);
Y <- x$stg
X <- seq(1:length(x$stg))
d <- data.frame(log(X),Y)
names(d) <- c("x","y")
m <- lm(d$y ~ poly(d$x,2,raw=TRUE))
png("rang-tore.png", width = 16, height = 8, units = 'in', res = 600)
plot(d$y,type="p",xlab="Rang der Mannschaft",ylab="Gesamtanzahl an Toren",main="Fussball: Rang und Tore",cex=0.3)
lines(predict(m,d),col="green")
dev.off()
Wir testen, mit dem Kolmogorov-Smirnov-Test in R, ob der Fehler \[\epsilon\] normalverteilt ist:
r <- m$residuals; ks.test(r,"pnorm",mean=mean(r),sd=sd(r))Der Test ergibt:
> ks.test(r,"pnorm",mean=mean(r),sd=sd(r)) One-sample Kolmogorov-Smirnov test data: r D = 0.0721, p-value = 0.08732 alternative hypothesis: two-sidedDa der p-Wert >= 0.05 ist, kann man davon ausgehen, dass epsilon normalverteilt mit Mittelwert 0 = mean(r) und Standardabweichung = sd(r) = 21.49643 ist.
Donnerstag, 20. Oktober 2016
Gebrauchtwagenpreise auf Ebay-Kleinanzeigen
Ich habe mit Scrapy Daten zu Gebrauchtwagenpreisen auf Ebay-Kleinanzeigen gesammelt. Hier ist eine grafische Auswertung nach Erstzulassungsjahr und Preis in Euro:
Die Grafik wurde mit R erzeugt:
#!/usr/bin/Rscript
x <- read.csv("jahr_preise.csv")
ll <- lm(log(x$preis) ~ poly(x$jahr, 93, raw=TRUE))
png("jahr-preis.png", width = 16, height = 8, units = 'in', res = 600)
plot(x$jahr,x$preis,type="p",xlab="Erstzulassungsjahr",ylab="Preis in EURO",main="Gebrauchtwagenpreise auf Ebay-Kleinanzeigen",cex=0.3)
lines(x$jahr, exp(ll$fitted.values))
dev.off()
Um die Kurve in der Grafik darstellen zu können wurde folgende Regression verwendet:
\[ \text{preis} = \exp(\sum_{n=0}^{n=93}{a_n\cdot\text{jahr}^n}) \]
Mittwoch, 13. Juli 2016
Breadth First Search in Python and Java
Breadth-First-Search-Algorithm:
Here is the python code:
You can call it like this:
./bfs.py graph.txt vThe file graph.txt:
v:r r:s,v s:r,w w:t,x,s t:w,x,u x:w,t,u,y u:t,x,y y:x,u
#!/usr/bin/python
import sys
def read_graph(filename):
f = open(filename,'r')
graph = {}
for line in f:
line = line.replace('\n','')
fromVertex, toList = line.split(':')
toList = toList.split(',')
graph[fromVertex] = toList
return graph
def bfs(graph,s):
color = {}
dist = {}
pi = {}
for u in graph.keys():
if u != s:
color[u] = 'white'
dist[u] = None
pi[u] = None
color[s] = 'gray'
dist[s] = 0
pi[s] = None
Q = []
Q.append(s)
while len(Q) > 0:
u = Q.pop(0)
for v in graph[u]:
if color[v] == 'white':
color[v] = 'gray'
dist[v] = dist[u]+1
pi[v] = u
Q.append(v)
color[u] = 'black'
for x in graph.keys():
print x,color[x],dist[x],pi[x]
print "---------------"
return color,dist,pi
def print_path(graph,s,v,pi):
if v == s:
print s
elif pi[v] is None:
print "no path from ", s , " to ", v, " exists"
else:
print_path(graph, s, pi[v], pi)
print v
if __name__ == '__main__':
graph = read_graph(sys.argv[1])
s = sys.argv[2]
color,dist,pi = bfs(graph,s)
Here is the java code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class BFS {
private static HashMap color;
private static HashMap dist;
private static HashMap pi;
public static void bfs(HashMap graph, String s)
{
color = new HashMap();
dist = new HashMap();
pi = new HashMap();
for(String u : graph.keySet())
{
if( !u.equals(s)){
color.put(u, "white");
dist.put(u, null);
pi.put(u, null);
}
}
color.put(s, "gray");
dist.put(s, 0);
pi.put(s, null);
ArrayList Q = new ArrayList();
Q.add(s);
while( Q.size() > 0)
{
String u = Q.get(0);
Q.remove(0);
for(String v : graph.get(u))
{
if( color.get(v).equals("white") )
{
color.put(v, "gray");
dist.put(v, dist.get(u)+1);
pi.put(v, u);
Q.add(v);
}
}
color.put(u,"black");
for( String x : graph.keySet())
{
System.out.println( x + " , " + color.get(x) + " , " + dist.get(x) + " , "+pi.get(x));
}
System.out.println("---------------");
}
}
public static HashMap read_graph(String filename)
{
HashMap graph = new HashMap();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
String[] splitted1 = line.split(":");
String fromVertex = splitted1[0];
String[] toVertexList = splitted1[1].split(",");
graph.put(fromVertex, toVertexList);
}
} catch (FileNotFoundException e) {
System.out.println("file not found");
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return graph;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap graph = read_graph(args[0]);
bfs(graph,args[1]);
}
}
You can call the java program like this:
java BFS graph.txt vAfter calling the python / java version, you should get something similar to this:
r , gray , 1 , v s , white , null , null t , white , null , null u , white , null , null v , black , 0 , null w , white , null , null x , white , null , null y , white , null , null --------------- r , black , 1 , v s , gray , 2 , r t , white , null , null u , white , null , null v , black , 0 , null w , white , null , null x , white , null , null y , white , null , null --------------- r , black , 1 , v s , black , 2 , r t , white , null , null u , white , null , null v , black , 0 , null w , gray , 3 , s x , white , null , null y , white , null , null --------------- r , black , 1 , v s , black , 2 , r t , gray , 4 , w u , white , null , null v , black , 0 , null w , black , 3 , s x , gray , 4 , w y , white , null , null --------------- r , black , 1 , v s , black , 2 , r t , black , 4 , w u , gray , 5 , t v , black , 0 , null w , black , 3 , s x , gray , 4 , w y , white , null , null --------------- r , black , 1 , v s , black , 2 , r t , black , 4 , w u , gray , 5 , t v , black , 0 , null w , black , 3 , s x , black , 4 , w y , gray , 5 , x --------------- r , black , 1 , v s , black , 2 , r t , black , 4 , w u , black , 5 , t v , black , 0 , null w , black , 3 , s x , black , 4 , w y , gray , 5 , x --------------- r , black , 1 , v s , black , 2 , r t , black , 4 , w u , black , 5 , t v , black , 0 , null w , black , 3 , s x , black , 4 , w y , black , 5 , x ---------------
Abonnieren
Posts (Atom)


