Files
FYS-STK4155/doc/pub/week36/html/._week36-bs030.html
T
2021-11-03 09:45:48 +01:00

602 lines
47 KiB
HTML

<!--
HTML file automatically generated from DocOnce source
(https://github.com/doconce/doconce/)
doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week36-bs --no_mako
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Week 36: Statistical interpretation of Linear Regression and Resampling techniques">
<title>Week 36: Statistical interpretation of Linear Regression and Resampling techniques</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week36.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week36-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Plans for week 36', 2, None, 'plans-for-week-36'),
('Thursday September 9', 2, None, 'thursday-september-9'),
('Summary from last Week and Examples',
2,
None,
'summary-from-last-week-and-examples'),
('Linear Regression and the SVD',
2,
None,
'linear-regression-and-the-svd'),
('What does it mean?', 2, None, 'what-does-it-mean'),
('And finally $\\boldsymbol{X}\\boldsymbol{X}^T$',
2,
None,
'and-finally-boldsymbol-x-boldsymbol-x-t'),
('Code for SVD and Inversion of Matrices',
2,
None,
'code-for-svd-and-inversion-of-matrices'),
('Inverse of Rectangular Matrix',
2,
None,
'inverse-of-rectangular-matrix'),
('Ridge and LASSO Regression',
2,
None,
'ridge-and-lasso-regression'),
('From OLS to Ridge and Lasso',
2,
None,
'from-ols-to-ridge-and-lasso'),
('Deriving the Ridge Regression Equations',
2,
None,
'deriving-the-ridge-regression-equations'),
('Note on Scikit-Learn', 2, None, 'note-on-scikit-learn'),
('Comparison with OLS', 2, None, 'comparison-with-ols'),
('SVD analysis', 2, None, 'svd-analysis'),
('Interpreting the Ridge results',
2,
None,
'interpreting-the-ridge-results'),
('More interpretations', 2, None, 'more-interpretations'),
('Deriving the Lasso Regression Equations',
2,
None,
'deriving-the-lasso-regression-equations'),
('Simple example to illustrate Ordinary Least Squares, Ridge and '
'Lasso Regression',
2,
None,
'simple-example-to-illustrate-ordinary-least-squares-ridge-and-lasso-regression'),
('Ridge Regression', 2, None, 'ridge-regression'),
('Lasso Regression', 2, None, 'lasso-regression'),
('Yet another Example', 2, None, 'yet-another-example'),
('The OLS case', 2, None, 'the-ols-case'),
('The Ridge case', 2, None, 'the-ridge-case'),
('Writing the Cost Function',
2,
None,
'writing-the-cost-function'),
('Lasso case', 2, None, 'lasso-case'),
('The first Case', 2, None, 'the-first-case'),
('Simple code for solving the above problem',
2,
None,
'simple-code-for-solving-the-above-problem'),
('With Lasso Regression', 2, None, 'with-lasso-regression'),
('Another Example, now with a polynomial fit',
2,
None,
'another-example-now-with-a-polynomial-fit'),
('Using CVXOPT', 2, None, 'using-cvxopt'),
('The simpler Example', 2, None, 'the-simpler-example'),
('Friday September 10', 2, None, 'friday-september-10'),
('Linking the regression analysis with a statistical '
'interpretation',
2,
None,
'linking-the-regression-analysis-with-a-statistical-interpretation'),
('Assumptions made', 2, None, 'assumptions-made'),
('Expectation value and variance',
2,
None,
'expectation-value-and-variance'),
('Expectation value and variance for $\\boldsymbol{\\beta}$',
2,
None,
'expectation-value-and-variance-for-boldsymbol-beta'),
('Deriving OLS from a probability distribution',
2,
None,
'deriving-ols-from-a-probability-distribution'),
('Independent and Identically Distrubuted (iid)',
2,
None,
'independent-and-identically-distrubuted-iid'),
('Maximum Likelihood Estimation (MLE)',
2,
None,
'maximum-likelihood-estimation-mle'),
('A new Cost Function', 2, None, 'a-new-cost-function'),
("More basic Statistics and Bayes' theorem",
2,
None,
'more-basic-statistics-and-bayes-theorem'),
('Marginal Probability', 2, None, 'marginal-probability'),
('Conditional Probability', 2, None, 'conditional-probability'),
("Bayes' Theorem", 2, None, 'bayes-theorem'),
("Interpretations of Bayes' Theorem",
2,
None,
'interpretations-of-bayes-theorem'),
("Example of Usage of Bayes' theorem",
2,
None,
'example-of-usage-of-bayes-theorem'),
('Doing it correctly', 2, None, 'doing-it-correctly'),
("Bayes' Theorem and Ridge and Lasso Regression",
2,
None,
'bayes-theorem-and-ridge-and-lasso-regression'),
('Test Function for what happens with OLS, Ridge and Lasso',
2,
None,
'test-function-for-what-happens-with-ols-ridge-and-lasso'),
("Invoking Bayes' theorem", 2, None, 'invoking-bayes-theorem'),
('Ridge and Bayes', 2, None, 'ridge-and-bayes'),
('Lasso and Bayes', 2, None, 'lasso-and-bayes')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week36-bs.html">Week 36: Statistical interpretation of Linear Regression and Resampling techniques</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week36-bs001.html#plans-for-week-36" style="font-size: 80%;">Plans for week 36</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs002.html#thursday-september-9" style="font-size: 80%;">Thursday September 9</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs003.html#summary-from-last-week-and-examples" style="font-size: 80%;">Summary from last Week and Examples</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs004.html#linear-regression-and-the-svd" style="font-size: 80%;">Linear Regression and the SVD</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs005.html#what-does-it-mean" style="font-size: 80%;">What does it mean?</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs006.html#and-finally-boldsymbol-x-boldsymbol-x-t" style="font-size: 80%;">And finally \( \boldsymbol{X}\boldsymbol{X}^T \)</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs007.html#code-for-svd-and-inversion-of-matrices" style="font-size: 80%;">Code for SVD and Inversion of Matrices</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs008.html#inverse-of-rectangular-matrix" style="font-size: 80%;">Inverse of Rectangular Matrix</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs009.html#ridge-and-lasso-regression" style="font-size: 80%;">Ridge and LASSO Regression</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs010.html#from-ols-to-ridge-and-lasso" style="font-size: 80%;">From OLS to Ridge and Lasso</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs011.html#deriving-the-ridge-regression-equations" style="font-size: 80%;">Deriving the Ridge Regression Equations</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs012.html#note-on-scikit-learn" style="font-size: 80%;">Note on Scikit-Learn</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs013.html#comparison-with-ols" style="font-size: 80%;">Comparison with OLS</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs014.html#svd-analysis" style="font-size: 80%;">SVD analysis</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs015.html#interpreting-the-ridge-results" style="font-size: 80%;">Interpreting the Ridge results</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs016.html#more-interpretations" style="font-size: 80%;">More interpretations</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs017.html#deriving-the-lasso-regression-equations" style="font-size: 80%;">Deriving the Lasso Regression Equations</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs018.html#simple-example-to-illustrate-ordinary-least-squares-ridge-and-lasso-regression" style="font-size: 80%;">Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs019.html#ridge-regression" style="font-size: 80%;">Ridge Regression</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs020.html#lasso-regression" style="font-size: 80%;">Lasso Regression</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs021.html#yet-another-example" style="font-size: 80%;">Yet another Example</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs022.html#the-ols-case" style="font-size: 80%;">The OLS case</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs023.html#the-ridge-case" style="font-size: 80%;">The Ridge case</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs024.html#writing-the-cost-function" style="font-size: 80%;">Writing the Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs025.html#lasso-case" style="font-size: 80%;">Lasso case</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs026.html#the-first-case" style="font-size: 80%;">The first Case</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs027.html#simple-code-for-solving-the-above-problem" style="font-size: 80%;">Simple code for solving the above problem</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs028.html#with-lasso-regression" style="font-size: 80%;">With Lasso Regression</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs029.html#another-example-now-with-a-polynomial-fit" style="font-size: 80%;">Another Example, now with a polynomial fit</a></li>
<!-- navigation toc: --> <li><a href="#using-cvxopt" style="font-size: 80%;">Using CVXOPT</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs031.html#the-simpler-example" style="font-size: 80%;">The simpler Example</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs032.html#friday-september-10" style="font-size: 80%;">Friday September 10</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs033.html#linking-the-regression-analysis-with-a-statistical-interpretation" style="font-size: 80%;">Linking the regression analysis with a statistical interpretation</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs034.html#assumptions-made" style="font-size: 80%;">Assumptions made</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs035.html#expectation-value-and-variance" style="font-size: 80%;">Expectation value and variance</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs036.html#expectation-value-and-variance-for-boldsymbol-beta" style="font-size: 80%;">Expectation value and variance for \( \boldsymbol{\beta} \)</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs037.html#deriving-ols-from-a-probability-distribution" style="font-size: 80%;">Deriving OLS from a probability distribution</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs038.html#independent-and-identically-distrubuted-iid" style="font-size: 80%;">Independent and Identically Distrubuted (iid)</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs039.html#maximum-likelihood-estimation-mle" style="font-size: 80%;">Maximum Likelihood Estimation (MLE)</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs040.html#a-new-cost-function" style="font-size: 80%;">A new Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs041.html#more-basic-statistics-and-bayes-theorem" style="font-size: 80%;">More basic Statistics and Bayes' theorem</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs042.html#marginal-probability" style="font-size: 80%;">Marginal Probability</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs043.html#conditional-probability" style="font-size: 80%;">Conditional Probability</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs044.html#bayes-theorem" style="font-size: 80%;">Bayes' Theorem</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs045.html#interpretations-of-bayes-theorem" style="font-size: 80%;">Interpretations of Bayes' Theorem</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs046.html#example-of-usage-of-bayes-theorem" style="font-size: 80%;">Example of Usage of Bayes' theorem</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs047.html#doing-it-correctly" style="font-size: 80%;">Doing it correctly</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs048.html#bayes-theorem-and-ridge-and-lasso-regression" style="font-size: 80%;">Bayes' Theorem and Ridge and Lasso Regression</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs049.html#test-function-for-what-happens-with-ols-ridge-and-lasso" style="font-size: 80%;">Test Function for what happens with OLS, Ridge and Lasso</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs050.html#invoking-bayes-theorem" style="font-size: 80%;">Invoking Bayes' theorem</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs051.html#ridge-and-bayes" style="font-size: 80%;">Ridge and Bayes</a></li>
<!-- navigation toc: --> <li><a href="._week36-bs052.html#lasso-and-bayes" style="font-size: 80%;">Lasso and Bayes</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0030"></a>
<!-- !split -->
<h2 id="using-cvxopt" class="anchor">Using CVXOPT </h2>
<p>As a small addendum, we note that you can also solve this problem
using the convex optimization package
<a href="https://cvxopt.org/examples/mlbook/l1regls.html" target="_self">CVXOPT</a>. This
requires, in addition to having installed <b>CVXOPT</b>, you need to
download the file <em>l1regl.py</em>. The following code example solves the
simpler problem we discussed above, where we have added the latter
python file.
</p>
<p>We list first the needed file. Thereafter we set up the problem to solve.</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> matrix, spdiag, mul, div, sqrt, normal, setseed
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> blas, lapack, solvers, sparse, spmatrix
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">math</span>
<span style="color: #008000; font-weight: bold">try</span>:
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">mosek</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">sys</span>
__MOSEK <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">True</span>
<span style="color: #008000; font-weight: bold">except</span>: __MOSEK <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>
<span style="color: #008000; font-weight: bold">if</span> __MOSEK:
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">l1regls_mosek</span>(A, b):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Returns the solution of l1-norm regularized least-squares problem</span>
<span style="color: #BA2121; font-style: italic"> minimize || A*x - b ||_2^2 + e&#39;*u</span>
<span style="color: #BA2121; font-style: italic"> subject to -u &lt;= x &lt;= u</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
m, n <span style="color: #666666">=</span> A<span style="color: #666666">.</span>size
env <span style="color: #666666">=</span> mosek<span style="color: #666666">.</span>Env()
task <span style="color: #666666">=</span> env<span style="color: #666666">.</span>Task(<span style="color: #666666">0</span>,<span style="color: #666666">0</span>)
task<span style="color: #666666">.</span>set_Stream(mosek<span style="color: #666666">.</span>streamtype<span style="color: #666666">.</span>log, <span style="color: #008000; font-weight: bold">lambda</span> x: sys<span style="color: #666666">.</span>stdout<span style="color: #666666">.</span>write(x))
task<span style="color: #666666">.</span>appendvars( <span style="color: #666666">2*</span>n) <span style="color: #408080; font-style: italic"># number of variables</span>
task<span style="color: #666666">.</span>appendcons( <span style="color: #666666">2*</span>n) <span style="color: #408080; font-style: italic"># number of constraints</span>
<span style="color: #408080; font-style: italic"># input quadratic objective</span>
Q <span style="color: #666666">=</span> matrix(<span style="color: #666666">0.0</span>, (n,n))
blas<span style="color: #666666">.</span>syrk(A, Q, alpha <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>, trans<span style="color: #666666">=</span><span style="color: #BA2121">&#39;T&#39;</span>)
I <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(n):
I<span style="color: #666666">.</span>extend(<span style="color: #008000">range</span>(i,n))
J <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(n):
J<span style="color: #666666">.</span>extend((n<span style="color: #666666">-</span>i)<span style="color: #666666">*</span>[i])
task<span style="color: #666666">.</span>putqobj(I, J, <span style="color: #008000">list</span>(Q[matrix(I) <span style="color: #666666">+</span> matrix(J)<span style="color: #666666">*</span>n]))
task<span style="color: #666666">.</span>putclist(<span style="color: #008000">range</span>(<span style="color: #666666">2*</span>n), <span style="color: #008000">list</span>(<span style="color: #666666">-2*</span>A<span style="color: #666666">.</span>T<span style="color: #666666">*</span>b) <span style="color: #666666">+</span> n<span style="color: #666666">*</span>[<span style="color: #666666">1.0</span>]) <span style="color: #408080; font-style: italic"># setup linear objective</span>
<span style="color: #408080; font-style: italic"># input constraint matrix row by row</span>
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(n):
task<span style="color: #666666">.</span>putarow( i, [i, n<span style="color: #666666">+</span>i], [<span style="color: #666666">1.0</span>, <span style="color: #666666">-1.0</span>])
task<span style="color: #666666">.</span>putarow( n<span style="color: #666666">+</span>i, [i, n<span style="color: #666666">+</span>i], [<span style="color: #666666">1.0</span>, <span style="color: #666666">1.0</span>])
<span style="color: #408080; font-style: italic"># setup bounds on constraints</span>
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>con,
<span style="color: #666666">0</span>, n, n<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>up], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>])
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>con,
n, <span style="color: #666666">2*</span>n, n<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>lo], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>])
<span style="color: #408080; font-style: italic"># setup variable bounds</span>
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>var,
<span style="color: #666666">0</span>, <span style="color: #666666">2*</span>n, <span style="color: #666666">2*</span>n<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>fr], <span style="color: #666666">2*</span>n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>], <span style="color: #666666">2*</span>n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>])
<span style="color: #408080; font-style: italic"># optimize the task</span>
task<span style="color: #666666">.</span>putobjsense(mosek<span style="color: #666666">.</span>objsense<span style="color: #666666">.</span>minimize)
task<span style="color: #666666">.</span>optimize()
task<span style="color: #666666">.</span>solutionsummary(mosek<span style="color: #666666">.</span>streamtype<span style="color: #666666">.</span>log)
x <span style="color: #666666">=</span> n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>]
task<span style="color: #666666">.</span>getsolutionslice(mosek<span style="color: #666666">.</span>soltype<span style="color: #666666">.</span>itr, mosek<span style="color: #666666">.</span>solitem<span style="color: #666666">.</span>xx, <span style="color: #666666">0</span>, n, x)
<span style="color: #008000; font-weight: bold">return</span> matrix(x)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">l1regls_mosek2</span>(A, b):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Returns the solution of l1-norm regularized least-squares problem</span>
<span style="color: #BA2121; font-style: italic"> minimize w&#39;*w + e&#39;*u</span>
<span style="color: #BA2121; font-style: italic"> subject to -u &lt;= x &lt;= u</span>
<span style="color: #BA2121; font-style: italic"> A*x - w = b</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
m, n <span style="color: #666666">=</span> A<span style="color: #666666">.</span>size
env <span style="color: #666666">=</span> mosek<span style="color: #666666">.</span>Env()
task <span style="color: #666666">=</span> env<span style="color: #666666">.</span>Task(<span style="color: #666666">0</span>,<span style="color: #666666">0</span>)
task<span style="color: #666666">.</span>set_Stream(mosek<span style="color: #666666">.</span>streamtype<span style="color: #666666">.</span>log, <span style="color: #008000; font-weight: bold">lambda</span> x: sys<span style="color: #666666">.</span>stdout<span style="color: #666666">.</span>write(x))
task<span style="color: #666666">.</span>appendvars(<span style="color: #666666">2*</span>n <span style="color: #666666">+</span> m) <span style="color: #408080; font-style: italic"># number of variables</span>
task<span style="color: #666666">.</span>appendcons(<span style="color: #666666">2*</span>n <span style="color: #666666">+</span> m) <span style="color: #408080; font-style: italic"># number of constraints</span>
<span style="color: #408080; font-style: italic"># input quadratic objective</span>
task<span style="color: #666666">.</span>putqobj(<span style="color: #008000">range</span>(<span style="color: #666666">2*</span>n,<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m), <span style="color: #008000">range</span>(<span style="color: #666666">2*</span>n,<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m), m<span style="color: #666666">*</span>[<span style="color: #666666">2.0</span>])
task<span style="color: #666666">.</span>putclist(<span style="color: #008000">range</span>(<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m), n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>] <span style="color: #666666">+</span> n<span style="color: #666666">*</span>[<span style="color: #666666">1.0</span>] <span style="color: #666666">+</span> m<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>]) <span style="color: #408080; font-style: italic"># setup linear objective</span>
<span style="color: #408080; font-style: italic"># input constraint matrix row by row</span>
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(n):
task<span style="color: #666666">.</span>putarow( i, [i, n<span style="color: #666666">+</span>i], [<span style="color: #666666">1.0</span>, <span style="color: #666666">-1.0</span>])
task<span style="color: #666666">.</span>putarow( n<span style="color: #666666">+</span>i, [i, n<span style="color: #666666">+</span>i], [<span style="color: #666666">1.0</span>, <span style="color: #666666">1.0</span>])
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(m):
task<span style="color: #666666">.</span>putarow( <span style="color: #666666">2*</span>n<span style="color: #666666">+</span>i, <span style="color: #008000">range</span>(n) <span style="color: #666666">+</span> [<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>i], <span style="color: #008000">list</span>(A[i,:]) <span style="color: #666666">+</span> [<span style="color: #666666">-1.0</span>])
<span style="color: #408080; font-style: italic"># setup bounds on constraints</span>
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>con,
<span style="color: #666666">0</span>, n, n<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>up], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>])
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>con,
n, <span style="color: #666666">2*</span>n, n<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>lo], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>], n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>])
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>con,
<span style="color: #666666">2*</span>n, <span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m, m<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>fx], <span style="color: #008000">list</span>(b), <span style="color: #008000">list</span>(b))
<span style="color: #408080; font-style: italic"># setup variable bounds</span>
task<span style="color: #666666">.</span>putboundslice(mosek<span style="color: #666666">.</span>accmode<span style="color: #666666">.</span>var, <span style="color: #666666">0</span>, <span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m, (<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m)<span style="color: #666666">*</span>[mosek<span style="color: #666666">.</span>boundkey<span style="color: #666666">.</span>fr],
(<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m)<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>], (<span style="color: #666666">2*</span>n<span style="color: #666666">+</span>m)<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>])
<span style="color: #408080; font-style: italic"># optimize the task</span>
task<span style="color: #666666">.</span>putobjsense(mosek<span style="color: #666666">.</span>objsense<span style="color: #666666">.</span>minimize)
task<span style="color: #666666">.</span>optimize()
task<span style="color: #666666">.</span>solutionsummary(mosek<span style="color: #666666">.</span>streamtype<span style="color: #666666">.</span>log)
x <span style="color: #666666">=</span> n<span style="color: #666666">*</span>[<span style="color: #666666">0.0</span>]
task<span style="color: #666666">.</span>getsolutionslice(mosek<span style="color: #666666">.</span>soltype<span style="color: #666666">.</span>itr, mosek<span style="color: #666666">.</span>solitem<span style="color: #666666">.</span>xx, <span style="color: #666666">0</span>, n, x)
<span style="color: #008000; font-weight: bold">return</span> matrix(x)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">l1regls</span>(A, b):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> </span>
<span style="color: #BA2121; font-style: italic"> Returns the solution of l1-norm regularized least-squares problem</span>
<span style="color: #BA2121; font-style: italic"> </span>
<span style="color: #BA2121; font-style: italic"> minimize || A*x - b ||_2^2 + || x ||_1.</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
m, n <span style="color: #666666">=</span> A<span style="color: #666666">.</span>size
q <span style="color: #666666">=</span> matrix(<span style="color: #666666">1.0</span>, (<span style="color: #666666">2*</span>n,<span style="color: #666666">1</span>))
q[:n] <span style="color: #666666">=</span> <span style="color: #666666">-2.0</span> <span style="color: #666666">*</span> A<span style="color: #666666">.</span>T <span style="color: #666666">*</span> b
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">P</span>(u, v, alpha <span style="color: #666666">=</span> <span style="color: #666666">1.0</span>, beta <span style="color: #666666">=</span> <span style="color: #666666">0.0</span> ):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> v := alpha * 2.0 * [ A&#39;*A, 0; 0, 0 ] * u + beta * v </span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
v <span style="color: #666666">*=</span> beta
v[:n] <span style="color: #666666">+=</span> alpha <span style="color: #666666">*</span> <span style="color: #666666">2.0</span> <span style="color: #666666">*</span> A<span style="color: #666666">.</span>T <span style="color: #666666">*</span> (A <span style="color: #666666">*</span> u[:n])
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">G</span>(u, v, alpha<span style="color: #666666">=1.0</span>, beta<span style="color: #666666">=0.0</span>, trans<span style="color: #666666">=</span><span style="color: #BA2121">&#39;N&#39;</span>):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> v := alpha*[I, -I; -I, -I] * u + beta * v (trans = &#39;N&#39; or &#39;T&#39;)</span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
v <span style="color: #666666">*=</span> beta
v[:n] <span style="color: #666666">+=</span> alpha<span style="color: #666666">*</span>(u[:n] <span style="color: #666666">-</span> u[n:])
v[n:] <span style="color: #666666">+=</span> alpha<span style="color: #666666">*</span>(<span style="color: #666666">-</span>u[:n] <span style="color: #666666">-</span> u[n:])
h <span style="color: #666666">=</span> matrix(<span style="color: #666666">0.0</span>, (<span style="color: #666666">2*</span>n,<span style="color: #666666">1</span>))
<span style="color: #408080; font-style: italic"># Customized solver for the KKT system </span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># [ 2.0*A&#39;*A 0 I -I ] [x[:n] ] [bx[:n] ]</span>
<span style="color: #408080; font-style: italic"># [ 0 0 -I -I ] [x[n:] ] = [bx[n:] ].</span>
<span style="color: #408080; font-style: italic"># [ I -I -D1^-1 0 ] [zl[:n]] [bzl[:n]]</span>
<span style="color: #408080; font-style: italic"># [ -I -I 0 -D2^-1 ] [zl[n:]] [bzl[n:]]</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># where D1 = W[&#39;di&#39;][:n]**2, D2 = W[&#39;di&#39;][:n]**2.</span>
<span style="color: #408080; font-style: italic"># </span>
<span style="color: #408080; font-style: italic"># We first eliminate zl and x[n:]:</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># ( 2*A&#39;*A + 4*D1*D2*(D1+D2)^-1 ) * x[:n] = </span>
<span style="color: #408080; font-style: italic"># bx[:n] - (D2-D1)*(D1+D2)^-1 * bx[n:] + </span>
<span style="color: #408080; font-style: italic"># D1 * ( I + (D2-D1)*(D1+D2)^-1 ) * bzl[:n] - </span>
<span style="color: #408080; font-style: italic"># D2 * ( I - (D2-D1)*(D1+D2)^-1 ) * bzl[n:] </span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># x[n:] = (D1+D2)^-1 * ( bx[n:] - D1*bzl[:n] - D2*bzl[n:] ) </span>
<span style="color: #408080; font-style: italic"># - (D2-D1)*(D1+D2)^-1 * x[:n] </span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># zl[:n] = D1 * ( x[:n] - x[n:] - bzl[:n] )</span>
<span style="color: #408080; font-style: italic"># zl[n:] = D2 * (-x[:n] - x[n:] - bzl[n:] ).</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># The first equation has the form</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># (A&#39;*A + D)*x[:n] = rhs</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># and is equivalent to</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># [ D A&#39; ] [ x:n] ] = [ rhs ]</span>
<span style="color: #408080; font-style: italic"># [ A -I ] [ v ] [ 0 ].</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># It can be solved as </span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># ( A*D^-1*A&#39; + I ) * v = A * D^-1 * rhs</span>
<span style="color: #408080; font-style: italic"># x[:n] = D^-1 * ( rhs - A&#39;*v ).</span>
S <span style="color: #666666">=</span> matrix(<span style="color: #666666">0.0</span>, (m,m))
Asc <span style="color: #666666">=</span> matrix(<span style="color: #666666">0.0</span>, (m,n))
v <span style="color: #666666">=</span> matrix(<span style="color: #666666">0.0</span>, (m,<span style="color: #666666">1</span>))
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">Fkkt</span>(W):
<span style="color: #408080; font-style: italic"># Factor </span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># S = A*D^-1*A&#39; + I </span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># where D = 2*D1*D2*(D1+D2)^-1, D1 = d[:n]**-2, D2 = d[n:]**-2.</span>
d1, d2 <span style="color: #666666">=</span> W[<span style="color: #BA2121">&#39;di&#39;</span>][:n]<span style="color: #666666">**2</span>, W[<span style="color: #BA2121">&#39;di&#39;</span>][n:]<span style="color: #666666">**2</span>
<span style="color: #408080; font-style: italic"># ds is square root of diagonal of D</span>
ds <span style="color: #666666">=</span> math<span style="color: #666666">.</span>sqrt(<span style="color: #666666">2.0</span>) <span style="color: #666666">*</span> div( mul( W[<span style="color: #BA2121">&#39;di&#39;</span>][:n], W[<span style="color: #BA2121">&#39;di&#39;</span>][n:]),
sqrt(d1<span style="color: #666666">+</span>d2) )
d3 <span style="color: #666666">=</span> div(d2 <span style="color: #666666">-</span> d1, d1 <span style="color: #666666">+</span> d2)
<span style="color: #408080; font-style: italic"># Asc = A*diag(d)^-1/2</span>
Asc <span style="color: #666666">=</span> A <span style="color: #666666">*</span> spdiag(ds<span style="color: #666666">**-1</span>)
<span style="color: #408080; font-style: italic"># S = I + A * D^-1 * A&#39;</span>
blas<span style="color: #666666">.</span>syrk(Asc, S)
S[::m<span style="color: #666666">+1</span>] <span style="color: #666666">+=</span> <span style="color: #666666">1.0</span>
lapack<span style="color: #666666">.</span>potrf(S)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">g</span>(x, y, z):
x[:n] <span style="color: #666666">=</span> <span style="color: #666666">0.5</span> <span style="color: #666666">*</span> ( x[:n] <span style="color: #666666">-</span> mul(d3, x[n:]) <span style="color: #666666">+</span>
mul(d1, z[:n] <span style="color: #666666">+</span> mul(d3, z[:n])) <span style="color: #666666">-</span> mul(d2, z[n:] <span style="color: #666666">-</span>
mul(d3, z[n:])) )
x[:n] <span style="color: #666666">=</span> div( x[:n], ds)
<span style="color: #408080; font-style: italic"># Solve</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># S * v = 0.5 * A * D^-1 * ( bx[:n] - </span>
<span style="color: #408080; font-style: italic"># (D2-D1)*(D1+D2)^-1 * bx[n:] + </span>
<span style="color: #408080; font-style: italic"># D1 * ( I + (D2-D1)*(D1+D2)^-1 ) * bzl[:n] - </span>
<span style="color: #408080; font-style: italic"># D2 * ( I - (D2-D1)*(D1+D2)^-1 ) * bzl[n:] )</span>
blas<span style="color: #666666">.</span>gemv(Asc, x, v)
lapack<span style="color: #666666">.</span>potrs(S, v)
<span style="color: #408080; font-style: italic"># x[:n] = D^-1 * ( rhs - A&#39;*v ).</span>
blas<span style="color: #666666">.</span>gemv(Asc, v, x, alpha<span style="color: #666666">=-1.0</span>, beta<span style="color: #666666">=1.0</span>, trans<span style="color: #666666">=</span><span style="color: #BA2121">&#39;T&#39;</span>)
x[:n] <span style="color: #666666">=</span> div(x[:n], ds)
<span style="color: #408080; font-style: italic"># x[n:] = (D1+D2)^-1 * ( bx[n:] - D1*bzl[:n] - D2*bzl[n:] ) </span>
<span style="color: #408080; font-style: italic"># - (D2-D1)*(D1+D2)^-1 * x[:n] </span>
x[n:] <span style="color: #666666">=</span> div( x[n:] <span style="color: #666666">-</span> mul(d1, z[:n]) <span style="color: #666666">-</span> mul(d2, z[n:]), d1<span style="color: #666666">+</span>d2 )\
<span style="color: #666666">-</span> mul( d3, x[:n] )
<span style="color: #408080; font-style: italic"># zl[:n] = D1^1/2 * ( x[:n] - x[n:] - bzl[:n] )</span>
<span style="color: #408080; font-style: italic"># zl[n:] = D2^1/2 * ( -x[:n] - x[n:] - bzl[n:] ).</span>
z[:n] <span style="color: #666666">=</span> mul( W[<span style="color: #BA2121">&#39;di&#39;</span>][:n], x[:n] <span style="color: #666666">-</span> x[n:] <span style="color: #666666">-</span> z[:n] )
z[n:] <span style="color: #666666">=</span> mul( W[<span style="color: #BA2121">&#39;di&#39;</span>][n:], <span style="color: #666666">-</span>x[:n] <span style="color: #666666">-</span> x[n:] <span style="color: #666666">-</span> z[n:] )
<span style="color: #008000; font-weight: bold">return</span> g
<span style="color: #008000; font-weight: bold">return</span> solvers<span style="color: #666666">.</span>coneqp(P, q, G, h, kktsolver <span style="color: #666666">=</span> Fkkt)[<span style="color: #BA2121">&#39;x&#39;</span>][:n]
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._week36-bs029.html">&laquo;</a></li>
<li><a href="._week36-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._week36-bs022.html">23</a></li>
<li><a href="._week36-bs023.html">24</a></li>
<li><a href="._week36-bs024.html">25</a></li>
<li><a href="._week36-bs025.html">26</a></li>
<li><a href="._week36-bs026.html">27</a></li>
<li><a href="._week36-bs027.html">28</a></li>
<li><a href="._week36-bs028.html">29</a></li>
<li><a href="._week36-bs029.html">30</a></li>
<li class="active"><a href="._week36-bs030.html">31</a></li>
<li><a href="._week36-bs031.html">32</a></li>
<li><a href="._week36-bs032.html">33</a></li>
<li><a href="._week36-bs033.html">34</a></li>
<li><a href="._week36-bs034.html">35</a></li>
<li><a href="._week36-bs035.html">36</a></li>
<li><a href="._week36-bs036.html">37</a></li>
<li><a href="._week36-bs037.html">38</a></li>
<li><a href="._week36-bs038.html">39</a></li>
<li><a href="._week36-bs039.html">40</a></li>
<li><a href="">...</a></li>
<li><a href="._week36-bs052.html">53</a></li>
<li><a href="._week36-bs031.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="https://..."><img width="250" align=right src="https://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>