Algorithms for Uncertainty Quantification

Similar documents
2 Getting Started with Numerical Computations in Python

epistasis Documentation

Python. Tutorial. Jan Pöschko. March 22, Graz University of Technology

A Glimpse at Scipy FOSSEE. June Abstract This document shows a glimpse of the features of Scipy that will be explored during this course.

IAST Documentation. Release Cory M. Simon

ECE521 W17 Tutorial 1. Renjie Liao & Min Bai

Algorithms for Uncertainty Quantification

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

Introduction to Python

I. Numerical Computing

Uniform and constant electromagnetic fields

Lab Objective: Introduce some of the basic optimization functions available in the CVXOPT package

CSC321 Lecture 2: Linear Regression

Lectures about Python, useful both for beginners and experts, can be found at (

NumPy 1.5. open source. Beginner's Guide. v g I. performance, Python based free open source NumPy. An action-packed guide for the easy-to-use, high

MAS212 Scientific Computing and Simulation

Research Computing with Python, Lecture 7, Numerical Integration and Solving Ordinary Differential Equations

pyiast Documentation Release Cory M. Simon

Python. chrysn

Outline Python, Numpy, and Matplotlib Making Models with Polynomials Making Models with Monte Carlo Error, Accuracy and Convergence Floating Point Mod

TECHNISCHE UNIVERSITÄT MÜNCHEN. Uncertainty Quantification in Fluid Flows via Polynomial Chaos Methodologies

NUMERICAL ANALYSIS WEEKLY OVERVIEW

High-Performance Scientific Computing

Computational Methods for Nonlinear Systems

Introduction to Python

HW5 computer problem: modelling a dye molecule Objectives By the end of this lab you should: Know how to manipulate complex numbers and matrices. Prog

Department of Chemical Engineering University of California, Santa Barbara Spring Exercise 2. Due: Thursday, 4/19/09

MAS212 Assignment #2: The damped driven pendulum

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 18 - Linear Algebra

Welcome to the Machine Learning Practical Deep Neural Networks. MLP Lecture 1 / 18 September 2018 Single Layer Networks (1) 1

Satellite project, AST 1100

5615 Chapter 3. April 8, Set #3 RP Simulation 3 Working with rp1 in the Notebook Proper Numerical Integration 5

Monte Python. the way back from cosmology to Fundamental Physics. Miguel Zumalacárregui. Nordic Institute for Theoretical Physics and UC Berkeley

QuTiP: Quantum Toolbox in Python. Circuit-QED

Eigenvalue problems III: Advanced Numerical Methods

Scikit-learn. scikit. Machine learning for the small and the many Gaël Varoquaux. machine learning in Python

pygaps Release 1.2.0

spiderman Documentation

Computational Methods for Nonlinear Systems

Practical Information

DATA SCIENCE SIMPLIFIED USING ARCGIS API FOR PYTHON

Scripting Languages Fast development, extensible programs

Skriptsprachen. Numpy und Scipy. Kai Dührkop. Lehrstuhl fuer Bioinformatik Friedrich-Schiller-Universitaet Jena

Chaospy: A modular implementation of Polynomial Chaos expansions and Monte Carlo methods

MATH36061 Convex Optimization

Hidden Markov Model Documentation

Lorenz Equations. Lab 1. The Lorenz System

Krylov Subspaces. Lab 1. The Arnoldi Iteration

Applied Machine Learning Lecture 2, part 1: Basic linear algebra; linear algebra in NumPy and SciPy. Richard Johansson

Lab 6: Linear Algebra

spiderman Documentation

Python & Numpy A tutorial

Scarce Documentation. Release David-Leon Pohl

Apex Python library. Release 1.0.1

Physics 584 Computational Methods

fidimag Documentation

Krylov Subspaces. The order-n Krylov subspace of A generated by x is

Image Processing in Numpy

AMLD Deep Learning in PyTorch. 2. PyTorch tensors

Driven, damped, pendulum

Introduction to FARGO3D

Introduction to phonopy

Ordinary Differential Equations

Introduction to computational modelling

sisl Documentation Release Nick R. Papior

1 Overview of Simulink. 2 State-space equations

VPython Class 2: Functions, Fields, and the dreaded ˆr

MultiDop: An open-source, Python-powered, multi-doppler radar analysis suite

Hurricane Prediction with Python

Numerical Methods for Initial Value Problems; Harmonic Oscillators

YYT-C3002 Application Programming in Engineering GIS I. Anas Altartouri Otaniemi

Line Search Algorithms

ES205 Analysis and Design of Engineering Systems: Lab 1: An Introductory Tutorial: Getting Started with SIMULINK

Cloud-based WRF Downscaling Simulations at Scale using Community Reanalysis and Climate Datasets

Modeling and Simulation in Python. Version 1.1

Chapter 3 - Derivatives

1-D Optimization. Lab 16. Overview of Line Search Algorithms. Derivative versus Derivative-Free Methods

An Introduction to MAGMA

Multilevel stochastic collocations with dimensionality reduction

Pysolar Documentation

Introduction to Python and its unit testing framework

A Practitioner s Guide to MXNet

/home/thierry/columbia/msongsdb/pyreport_tutorials/tutorial1/tutorial1.py January 23, 20111

Python for Operational Research in Healthcare

Numerical Solutions of Partial Differential Equations

Iris. A python package for the analysis and visualisation of Meteorological data. Philip Elson

Neural Networks Teaser

CS177 Spring Final exam. Fri 05/08 7:00p - 9:00p. There are 40 multiple choice questions. Each one is worth 2.5 points.

Experiment 1: Linear Regression

Here we consider soliton solutions of the Korteweg-de Vries (KdV) equation. This equation is given by u t + u u x + 3 u

Using the EartH2Observe data portal to analyse drought indicators. Lesson 4: Using Python Notebook to access and process data

ECE 5615/4615 Computer Project

Ordinary Differential Equations II: Runge-Kutta and Advanced Methods

PETSc for Python. Lisandro Dalcin

Automatic Reverse-Mode Differentiation: Lecture Notes

Reminders. HW1 out, due 10/19/2017 (Thursday) Group formations for course project due today (1 pt) Join Piazza (

Computational modeling

pygmm Documentation Release Albert Kottke

Data Mining algorithms

Machine Learning for Wind Energy Prediction - Possible Improvements over Traditional Methods

Transcription:

Technische Universität München SS 2017 Lehrstuhl für Informatik V Dr. Tobias Neckel M. Sc. Ionuț Farcaș April 26, 2017 Algorithms for Uncertainty Quantification Tutorial 1: Python overview In this worksheet, we do a short overview of programming in python and we show how to install the uncertainty quantification library chaospy. Note: Everything that follows is a lot easier if you use a Unix-based operating system on your machine. We assume that you are familiar with the basics concepts of computer programming: data types variables loops arrays functions etc. If you are new to programming and you want to cover the basic concepts, see e.g. tutorialspoint.com/computer programming/computer programming basics.htm. Short overview of programming with python Throughout these tutorials, we use python 2.7. How to use python 1? directly in the command line: simply type python 1 extensive python 2.7 documentation at https://docs.python.org/2/index.html 1

via Jupyter 2 notebooks via an IDE, e.g. Spyder, Eclipse + PyDev, PyCharm python is strongly typed: types are enforced dynamically, implicitly typed: you don t need to declare variables case sensitive: a and A are different variables object-oriented: everything is an object Documentation Via help(<object >), <object >. E.g. d o c >>> [ a ]. d o c >>> l i s t ( ) > new empty l i s t \ n l i s t ( i t e r a b l e ) > new l i s t i n i t i a l i z e d from i t e r a b l e s items Data structures Build-in data structures lists: similar to 1D arrays dictionaries: hash-tables (key-value pairs) tuples: immutable 1D arrays sets: since python 2.5 2 http://jupyter.org/ 2

>>> # l i s t example >>> m y l i s t = [ a, 1, 2, [ 1, 2 ] ] >>> m y l i s t [ 1 ] = 22 >>> m y l i s t [ 1, 22, a, [ 1, 2 ] ] >>> # t u p l e example >>> mytuple = (1, 2) >>> mytuple [ 1 ] = 2 Traceback ( most r e c e n t c a l l l a s t ) : F i l e <stdin >, l i n e 1, in <module> TypeError : t u p l e object does not support item assignment >>> # d i c t i o n a r y example >>> mydict = { 0 : a, 1 : b } >>> mydict [ 0 ] a >>> # s e t example >>> m y l i s t = [ 1, 2, 2, 3 ] >>> set ( m y l i s t ) set ( [ 1, 2, 3 ] ) >>> # l i s t s l i c i n g >>> m y l i s t = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] >>> m y l i s t [0: 1] [ 1, 2, 3, 4, 5, 6, 7, 8 ] >>> m y l i s t [ 2 : 4 ] [ 3, 4 ] >>> m y l i s t [ 1: 4: 1] [ 9, 8, 7 ] >>> m y l i s t [ : 5 ] [ 1, 2, 3, 4, 5 ] Flow control statements Flow control statements in python: if, for, and while. To obtain a list of numbers, use range<number> (or better, xrange<number>) >>> for i in xrange ( 5 ) : i f i % 2 : print i 3

else : print i 1 1 1 1 3 3 Functions Functions are declared with the def keyword. Optional arguments are set in the function declaration. In python, functions can return a tuple (this way, you can effectively return multiple values) >>> def i n c r e a s e b y o n e ( x ) : return x + 1 >>> i n c r e a s e b y o n e ( 2 ) 3 >>> i n c r e a s e b y o n e ( 2. 3 ) 3. 3 >>> n ew i n c r e a s e b y o n e = lambda x : x + 1 >>> n ew i n c r e a s e b y o n e ( 1 ) 2 Use of external libraries >>> # import e n t i r e l i b r a r y >>> import [ libname ] >>> # import s p e c i f i c f u n c t i o n s / packages >>> from [ libame ] import [ funcname packagename ] Object-oriented programming Classes are declared with the class keyword. Private variables and methods can be declared, however, by convention, this is not enforced by the language. 4

>>> class MyClass : v a r i a b l e = 10 def i n i t ( s e l f ) : print t h i s i s an i n s t a n c e o f MyClass def myfunction ( s e l f, x ) : return 2 x >>> i n s t a n c e = MyClass ( ) t h i s i s an i n s t a n c e o f MyClass >>> i n s t a n c e. myfunction (10) 20 >>> i n s t a n c e. v a r i a b l e 10 >>> # i n h e r i t a n c e example >>> class SubClass ( MyClass ) : def i n i t ( s e l f ) : print t h i s i s an i n s t a n c e o f the s u b c l a s s >>> i n s t a n c e=subclass ( ) t h i s i s an i n s t a n c e o f the s u b c l a s s Polymorphism is also possible >>> class Animal : def i n i t ( s e l f, name) : s e l f. name = name def t a l k ( s e l f ) : raise NotImplementedError ( Abstract method must be implemented ) >>> class Cat ( Animal ) : def t a l k ( s e l f ) : return Meow! >>> class Dog( Animal ) : def t a l k ( s e l f ) : return Woof! Woof! >>> class Human( Animal ) : 5

def bark ( s e l f ) : print UQ i s the best >>> animals = [ Cat ( Mr. P u r r i f y ), Dog( L a s s i e ), Human( me ) ] >>> for animal in animals : print animal. name + : + animal. t a l k ( ) Mr. P u r r i f y : Meow! L a s s i e : Woof! Woof! Traceback ( most r e c e n t c a l l l a s t ) : F i l e <stdin >, l i n e 2, in <module> F i l e <stdin >, l i n e 5, in t a l k NotImplementedError : A b s t r a c t method must be implemented Scientific computing with python Since python is an interpreted and dynamically typed programming language, python programs can be slow compared to compiled statically typed programming languages, such as C and Fortran. However, using suitable libraries, python can be used for scientific computing 3. Numpy - multidimensional data arrays package that provides high-performance vector, matrix and higher-dimensional data structures for python. >>> import numpy as np >>> # do c l e v e r t h i n g s SciPy - Library of scientific algorithms It includes Special functions (scipy.special) Integration (scipy.integrate) Optimization (scipy.optimize) 3 some materials on scientific computing https://github.com/jrjohansson/scientific-python-lectures 6

Interpolation (scipy.interpolate) Fourier Transforms (scipy.fftpack) Signal Processing (scipy.signal) Linear Algebra (scipy.linalg) Sparse Eigenvalue Problems (scipy.sparse) Statistics (scipy.stats) Multi-dimensional image processing (scipy.ndimage) File IO (scipy.io) matplotlib - 2D and 3D plotting Using Fortran and C code In python it is relatively easy to call out to libraries with compiled C or Fortran code Fortran: F2PY C: ctypes, Cython Tools for high-performance computing applications thread parallelism: the multiprocessing package IPython parallel MPI: mpi4py opencl: pyopencl etc. 7

Assignment A linear damped oscillator is a modeled by a second order ODE d 2 y (t) + c dy (t) + ky(t) = f cos(ωt) dt 2 dt y(0) = y 0 dy (0) = y dt 1, (1) where c is the damping coefficient, k the spring constant, f the forcing amplitude, ω the frequency, y 0 represents the initial position, whereas y 1 is the initial velocity. Assignment 1 Considering t [0, 20], c = 0.5, k = 2.0, f = 0.5, ω = 1.05, y 0 = 0.5, y 1 = 0.0, discretize the above equation using finite differences (central difference schemes on a Cartesian grid) with t = 0.1, 0.05, 0.01 and standard python lists. Assignment 2 Considering the setup as in Assignment 1, discretize the oscillator using finite differences and numpy lists. What do you observe? Assignment 3 With the same setup, solve (1) using the odeint 4 function from scipy.integrate. chaospy installation/building from source Throughout these tutorials, we will use the chaospy library. chaospy 5 is a library designed to perform uncertainty quantification using (but not restricted to) polynomial chaos expansions and advanced Monte Carlo methods implemented in Python 2 and 3. Assuming that you have a Unix-based operating system on your machine, to install chaospy you could use the pip package management system install pip: sudo apt install python-pip 4 see https://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.integrate.odeint.html 5 online documentation http://chaospy.readthedocs.io/en/master/ 8

install numpy, scipy, matplotlib: pip install numpy scipy matplotlib 6 install scikit-learn 7 : pip install scikit-learn install chaospy: (sudo) 8 pip install chaospy or you could build chaospy directly from source install git: sudo apt install git clone the chaospy repository: git clone https://github.com/jonathf/chaospy.git build chaospy cd /path to chaospy/chaospy sudo apt install python-numpy python-scipy python-matplotlib 9 sudo apt install python-scikit-learn (sudo) 10 python setup.py install add /path to chaospy/chaospy/build to your PYTHON PATH type export $PYTHON PATH=/path to chaospy/chaospy/build:$python PATH in your terminal or add $PYTHON PATH=/path to chaospy/chaospy/build:$python PATH into your.bashrc file at the end test the build locally pip install -r requirements-dev.txt python setup.py test 6 on systems where you don t have root access, use pip install --user 7 to support more regression methods in chaospy; optional 8 it might work without root access 9 alternatively, you could use pip install -r requirements-dev.txt 10 it might work without root access 9

chaospy installation on Windows Disclaimer: this was not tried out, hence, some steps might be needed to be adjusted download and install anaconda: following the steps from https://docs.continuum.io/anaconda/install-windows.html install numpy, scipy, matplotlib via conda install numpy scipy matplotlib install scikit-learn, optional via conda install scikit-learn add \path\to\anaconda\lib to the pythonpath or pip very.py file to anaconda s python (in cmd): assoc.py=python.file ftype Python.File=C:path\to\Anaconda\python.exe "%1" %* 10