text:
This section is intended to provide the minimally sufficient set of mathematical details to understand the tuning parameters of ChiRP’s regression functions. If you need a refresher on Dirichlet Processes, check out this interactive tutorial.

## The Generative Model

The ChiRP package implements the following generative Chinese Restaurant Process (CRP) regression mixture. Each regression function takes in training data $$D = \{D_i\}_{i=1:n} = (y_i, x_i)_{i=1:n}$$ (and a test set, if desired), where $$x_i$$ is a $$p \times 1$$ covariate vector (including an intercept) and $$y_i$$ is a scalar outcome observed for $$n$$ subjects indexed by $$i$$. The regression functions returns, for each subject in the training (and testing, if applicable) data set, a posterior distribution of predicted outcome $$y$$. It also returns cluster assignments for each subject.

The full probability model is given by \begin{aligned} y_i | x_i, \eta_i & \sim f_y(y_i|x_i, \eta_i) \\ x_i | \theta_i & \sim f_x(x_i | \theta_i ) \\ \eta_i, \theta_i | G & \sim G \\ G & \sim DP(\alpha G_0) \end{aligned} Above, $$f_y$$ is the conditional distribution of the outcome. It is governed by parameters $$\eta_i$$. Below, we outline three currently supported outcome types: zero-inflated continuous outcomes, continuous outcomes, and binary outcomes.

The joint distribution of the covariates, $$f_x$$, is governed by $$\theta_i$$. These parameters are distributed according to an unknown distribution $$G$$, to which we assign a Dirichlet Process (DP) prior. For compactness, we will sometimes denote $$\omega_i = (\eta_i, \theta_i)$$. Since draws, $$G$$, from a DP are discrete, there are bound to be ties among the subject specific parameters $$\omega_i$$. That is, subjects will tend to cluster together. The number of clusters is not set a priori, but rather infinitely many clusters are assumed.

This induced clustering partitions complex data into more homogenous subgroups - each with their own set of regression parameters. This allows CRP models to obtain flexible fits where parametric models may fail.

Package implementation details:

• All models accommodate any number of binary and continuous covariates only. Categorical variables are not supported (yet).
• All models assume independence among covariates: $$f_x(x_i) = \prod_{j=1}^p f_{x^p}(x_i^p|\theta_i^p)$$
• $$f_{x^p}$$ is set to be Bernoulli for binary covariates and Gaussian, $$N(\lambda, \tau)$$ for continuous covariates.
• Hyperparameters
• Concentration parameter, $$\alpha$$, is estimated with a $$Gam(1,1)$$ prior.
• For continuous covariates, we use hyperpriors $$\lambda \sim N(\mu, var=\sigma^2)$$ and $$\tau \sim InvGamma(shape=g_2, rate=b_2)$$.
• In an Empirical Bayes fashion, we set $$\mu = \bar{x}$$ and $$\sigma^2 = \text{mu_scale} \cdot var(x)$$, where mu_scale is an optional tuning parameter.
• We set $$g_2=2$$ and $$b2 = \text{tau_scale} \cdot var(x)$$ for all continuous covariates, where tau_scale is an optional tuning parameter. If tau_scale=1, the prior for the variance is centered around the empirical. For tau_scale>1, we allow for larger variances a priori .

## Supported Outcome Models

### Zero-Inflated Outcomes with ZDPMix()

In this implementation, $f_y(y_i|x_i, \eta_i) = \pi(x_i'\gamma_i)\cdot \delta_0(y) + (1 - \pi(x_i'\gamma_i) ) \cdot N(x_i'\beta, \phi)$

Where $$\pi(x_i'\gamma_i) = expit(x_i'\gamma_i)$$ is the probability of the outcome being zero and $$\delta_0(y)$$ is the point-mass distribution at $$0$$. See this paper for methodological details.

The function call looks like this:

zDP_res<-ZDPMix(d_train = d, ## training data.frame() object
d_test = dt, ## test data.frame() object
formula = y ~ L1 + X1 + X2 + X3 + X4 + A,
## specify total number of mcmc draws (iter) and burnin (burnin)
burnin=2000, iter = 3000)
# this will return iter - burnin posterior draws.

ZDPMix()A Gaussian prior $$N_p( \beta_{OLS} , \text{beta_var_scale}\cdot\Sigma )$$ is chosen for $$\beta$$. $$\beta_{OLS}$$ is a linear regression estimated using observations where $$y>0$$. $$\Sigma$$ is a diagonal matrix with estimates parameter variances from this regression along the diagonal. The optional parameter beta_var_scale allows the user to make the prior more or less dispersed around the observed data.

A Gaussian prior $$N_p(0 , 2\cdot I_p )$$. Note that this is actually fairly flat on the logit scale putting substantial prior probability on odds ratios in the range of $$.01-.70$$.

Sampling for this model involves a Metropolis step to propose a draw of $$\gamma$$. The optional tuning parameter prop_z_sigma is a length $$p$$ vector specifying the variance of the Gaussian proposal distribution used in the Metropolis step.

Finally, we place an Inverse Gamma prior on $$\phi \sim InvGamma(g_1, b_1)$$ on the data variance. The optional tuning parameter phi_y is a length two vector phi_y=$$(g_1, b_1)$$ that allows the user to specify this parameters.

### Continuous Outcomes NDPMix()

In this implementation, $$f_y(y_i|x_i, \eta_i) = N(x_i'\beta, \phi)$$. All the same applies as in the zero-inflated case above.

### Binary Outcomes PDPMix()

In this implementation, $$f_y(y_i|x_i, \eta_i) = Ber( expit( x_i'\beta))$$, where $$expit()$$ is the inverse logistic function.

## Posterior Clustering

### The Chinese Restaurant Posterior

Let $$c_{1:n}$$ represent a vector of cluster indicators for the $$n$$ subjects. Since the DP prior induces a clustering on patients, we can make inference on $$c_{1:n}$$. Consider clustering each patient one at a time in a given Gibbs iteration, $$t$$. At the time we cluster subject $$i$$, $$i-1$$ subjects have already been clustered to assignments $$c_{1:i-1}^{(t)}$$ and subject specific parameters $$\omega_{1:i-1}^{(t)}$$, then the conditional posterior distribution of $$c_i^{(t)}$$ is,

$p(c_i^{(t)} = k| c_{1:i-1}, \omega_{i:i-1} ) \propto \begin{cases} \frac{1}{\alpha + i -1} \sum_{j<i} I(c_j^{(t)}=k)p(D_i | \omega_j) & \text{for } k \in c_{1:i-1}^{(t)} \\ \frac{\alpha}{\alpha + i -1} \int p(D_i | \omega ) dG_0(\omega) & \text{for } k \not \in c^{(t)}_{1:i-1} \end{cases}$

These equations characterize the Chinese Restaurant Process posterior. At every Gibbs iterations, $$t$$, a subject’s cluster assignment can be updated to an existing cluster with probability proportional to the number of subjects in that cluster times how well well subject $$i$$’s data, $$D_i$$, fits the likelihood under that cluster’s parameters. However, a subject can also be assigned to a new cluster with probability proportional to $$\alpha$$ times how well $$D_i$$ fits the likelihood under a new set of parameters drawn from the prior. If a patient’s observed data is very unique and doesn’t fit well with existing parameters, a new cluster is likely to open up to accommodate this subject. However, for large $$n$$, as $$i\rightarrow n$$ the probability of a new cluster opening up becomes less and less likely. Thus, the model naturally prevents overfitting (i.e. every subject getting assigned to their own, singleton cluster).

The functions ZDPMix,NDPMix, and PDPMix all output a $$n \times T$$ matrix for $$T$$ post-burnin posterior draws. This matrix gives the cluster label for each subject, at each iteration. This is done for both training and testing datasets. These matrices are stored in the function output as cluster_inds$train and cluster_inds$test.

ChiRP makes posterior inference via MCMC sampling as in Neal 2000. We introduce latent cluster indicators $$c_{i:n}$$ and alternative between updating these assignments conditional on parameters $$\omega_{i:n}$$ and then updating parameters $$\omega_{i:n}$$ conditional on assignments.

### Posterior Mode Clustering

The function cluster_assign_mode takes as input either cluster_inds$train or cluster_inds$test and computes the posterior mode cluster assignment associated with each subject while accounting for label switching. See this paper for a more precise reference on the deterministic relabeling procedure implemented in ChiRP.

In short, we use the fact that at every Gibbs iteration we can construct an $$n\times n$$ adjacency matrix with a $$1$$ in the $$i-j$$ entry indicating that subject $$i$$ and $$j$$ were clustered together and a $$0$$ otherwise. We can then take an elementwise posterior mean of these matrices to estimate the posterior mode adjacency matrix where the $$i-j$$ entry is the proportion of times that subject $$i$$ and $$j$$ were clustered together . We then do a search to see which of the posterior adjacency matrices is closest to the posterior mode adjacency matrix in the $$L_2$$ sense. The indicators from this matrix are used as the posterior mode cluster assignment.

## Posterior Predictions

Having observed training data $$D$$, we may wish to predict an outcome $$\tilde y$$ given testing covariates $$\tilde x \in \mathcal R^p$$. Given $$T$$ posterior draws of $$\omega_{1:n}^{(t)}$$, indexed by $$t$$, ChiRP implements a Monte Carlo procedure to obtain $$T$$ draws from the conditional outcome distribution of test observation with covariates $$\tilde x$$.

• Draw $$\tilde c \sim Categorical( p(\tilde x | \omega_1^{(t)}) , p(\tilde x | \omega_2^{(t)}, \dots , p(\tilde x | \omega_n^{(t)}), p(\tilde x | \omega_{new}) )$$, where $$\omega_{new}$$ is a new set of cluster parameters drawn from the prior $$G_0$$.
• Set $$\tilde \omega$$ to be the parameter corresponding to $$\tilde c$$ drawn above.
• Finally, draw $$\tilde y^{(t)} \sim f_y( y | \tilde x, \tilde \omega)$$ from the generative model defined at the top of this page.

In this way we get $$T$$ posterior predictive draws for each subject in the test set. The functions ZDPMix,NDPMix, and PDPMixall output an $$n\times T$$ matrix of these posterior draws. They are called predictions$train and predictions$test. Each row is the posterior predictive distribution for a given subject. We can take the mean of each row to be a posterior predictive mean estimate. We can use percentiles to form credible intervals around the point estimate.


text:
# Search by Topic

#### Resources tagged with Working systematically similar to The Spider and the Fly:

Filter by: Content type:
Stage:
Challenge level:

### There are 129 results

Broad Topics > Using, Applying and Reasoning about Mathematics > Working systematically

### Warmsnug Double Glazing

##### Stage: 3 Challenge Level:

How have "Warmsnug" arrived at the prices shown on their windows? Which window has been given an incorrect price?

### Cuboids

##### Stage: 3 Challenge Level:

Find a cuboid (with edges of integer values) that has a surface area of exactly 100 square units. Is there more than one? Can you find them all?

### One Out One Under

##### Stage: 4 Challenge Level:

Imagine a stack of numbered cards with one on top. Discard the top, put the next card to the bottom and repeat continuously. Can you predict the last card?

### Pole Star Sudoku 2

##### Stage: 3 and 4 Challenge Level:

This Sudoku, based on differences. Using the one clue number can you find the solution?

### Number Daisy

##### Stage: 3 Challenge Level:

Can you find six numbers to go in the Daisy from which you can make all the numbers from 1 to a number bigger than 25?

### More on Mazes

##### Stage: 2 and 3

There is a long tradition of creating mazes throughout history and across the world. This article gives details of mazes you can visit and those that you can tackle on paper.

### Twinkle Twinkle

##### Stage: 2 and 3 Challenge Level:

A game for 2 people. Take turns placing a counter on the star. You win when you have completed a line of 3 in your colour.

### Medal Muddle

##### Stage: 3 Challenge Level:

Countries from across the world competed in a sports tournament. Can you devise an efficient strategy to work out the order in which they finished?

### Maths Trails

##### Stage: 2 and 3

The NRICH team are always looking for new ways to engage teachers and pupils in problem solving. Here we explain the thinking behind maths trails.

### LOGO Challenge - Triangles-squares-stars

##### Stage: 3 and 4 Challenge Level:

Can you recreate these designs? What are the basic units? What movement is required between each unit? Some elegant use of procedures will help - variables not essential.

### Oranges and Lemons, Say the Bells of St Clement's

##### Stage: 3 Challenge Level:

Bellringers have a special way to write down the patterns they ring. Learn about these patterns and draw some of your own.

### Reach 100

##### Stage: 2 and 3 Challenge Level:

Choose four different digits from 1-9 and put one in each box so that the resulting four two-digit numbers add to a total of 100.

### Difference Sudoku

##### Stage: 4 Challenge Level:

Use the differences to find the solution to this Sudoku.

### 9 Weights

##### Stage: 3 Challenge Level:

You have been given nine weights, one of which is slightly heavier than the rest. Can you work out which weight is heavier in just two weighings of the balance?

### Two and Two

##### Stage: 3 Challenge Level:

How many solutions can you find to this sum? Each of the different letters stands for a different number.

### Sticky Numbers

##### Stage: 3 Challenge Level:

Can you arrange the numbers 1 to 17 in a row so that each adjacent pair adds up to a square number?

### The Naked Pair in Sudoku

##### Stage: 2, 3 and 4

A particular technique for solving Sudoku puzzles, known as "naked pair", is explained in this easy-to-read article.

### Introducing NRICH TWILGO

##### Stage: 1, 2, 3, 4 and 5 Challenge Level:

We're excited about this new program for drawing beautiful mathematical designs. Can you work out how we made our first few pictures and, even better, share your most elegant solutions with us?

### Seasonal Twin Sudokus

##### Stage: 3 and 4 Challenge Level:

This pair of linked Sudokus matches letters with numbers and hides a seasonal greeting. Can you find it?

### Cinema Problem

##### Stage: 3 Challenge Level:

A cinema has 100 seats. Show how it is possible to sell exactly 100 tickets and take exactly £100 if the prices are £10 for adults, 50p for pensioners and 10p for children.

### Inky Cube

##### Stage: 2 and 3 Challenge Level:

This cube has ink on each face which leaves marks on paper as it is rolled. Can you work out what is on each face and the route it has taken?

### Magnetic Personality

##### Stage: 2, 3 and 4 Challenge Level:

60 pieces and a challenge. What can you make and how many of the pieces can you use creating skeleton polyhedra?

### Intersection Sudoku 1

##### Stage: 3 and 4 Challenge Level:

A Sudoku with a twist.

##### Stage: 3 and 4 Challenge Level:

Four small numbers give the clue to the contents of the four surrounding cells.

##### Stage: 3 Challenge Level:

How many different symmetrical shapes can you make by shading triangles or squares?

### Building with Longer Rods

##### Stage: 2 and 3 Challenge Level:

A challenging activity focusing on finding all possible ways of stacking rods.

##### Stage: 3 Challenge Level:

You need to find the values of the stars before you can apply normal Sudoku rules.

### Summing Consecutive Numbers

##### Stage: 3 Challenge Level:

Many numbers can be expressed as the sum of two or more consecutive integers. For example, 15=7+8 and 10=1+2+3+4. Can you say which numbers can be expressed in this way?

### Tea Cups

##### Stage: 2 and 3 Challenge Level:

Place the 16 different combinations of cup/saucer in this 4 by 4 arrangement so that no row or column contains more than one cup or saucer of the same colour.

### Integrated Product Sudoku

##### Stage: 3 and 4 Challenge Level:

This Sudoku puzzle can be solved with the help of small clue-numbers on the border lines between pairs of neighbouring squares of the grid.

### Special Numbers

##### Stage: 3 Challenge Level:

My two digit number is special because adding the sum of its digits to the product of its digits gives me my original number. What could my number be?

### Weights

##### Stage: 3 Challenge Level:

Different combinations of the weights available allow you to make different totals. Which totals can you make?

### A First Product Sudoku

##### Stage: 3 Challenge Level:

Given the products of adjacent cells, can you complete this Sudoku?

### Ratio Sudoku 2

##### Stage: 3 and 4 Challenge Level:

A Sudoku with clues as ratios.

### More Children and Plants

##### Stage: 2 and 3 Challenge Level:

This challenge extends the Plants investigation so now four or more children are involved.

##### Stage: 3 Challenge Level:

Rather than using the numbers 1-9, this sudoku uses the nine different letters used to make the words "Advent Calendar".

### Twin Line-swapping Sudoku

##### Stage: 4 Challenge Level:

A pair of Sudoku puzzles that together lead to a complete solution.

### Ben's Game

##### Stage: 3 Challenge Level:

Ben passed a third of his counters to Jack, Jack passed a quarter of his counters to Emma and Emma passed a fifth of her counters to Ben. After this they all had the same number of counters.

##### Stage: 3 and 4 Challenge Level:

Four numbers on an intersection that need to be placed in the surrounding cells. That is all you need to know to solve this sudoku.

### Multiples Sudoku

##### Stage: 3 Challenge Level:

Each clue in this Sudoku is the product of the two numbers in adjacent cells.

### Twin Corresponding Sudoku III

##### Stage: 3 and 4 Challenge Level:

Two sudokus in one. Challenge yourself to make the necessary connections.

### Consecutive Negative Numbers

##### Stage: 3 Challenge Level:

Do you notice anything about the solutions when you add and/or subtract consecutive negative numbers?

### Colour Islands Sudoku

##### Stage: 3 Challenge Level:

An extra constraint means this Sudoku requires you to think in diagonals as well as horizontal and vertical lines and boxes of nine.

### Colour Islands Sudoku 2

##### Stage: 3, 4 and 5 Challenge Level:

In this Sudoku, there are three coloured "islands" in the 9x9 grid. Within each "island" EVERY group of nine cells that form a 3x3 square must contain the numbers 1 through 9.

### Bochap Sudoku

##### Stage: 3 and 4 Challenge Level:

This Sudoku combines all four arithmetic operations.

##### Stage: 3 and 4 Challenge Level:

This is a variation of sudoku which contains a set of special clue-numbers. Each set of 4 small digits stands for the numbers in the four cells of the grid adjacent to this set.

### Star Product Sudoku

##### Stage: 3 and 4 Challenge Level:

The puzzle can be solved by finding the values of the unknown digits (all indicated by asterisks) in the squares of the $9\times9$ grid.

### Rectangle Outline Sudoku

##### Stage: 3 and 4 Challenge Level:

Each of the main diagonals of this sudoku must contain the numbers 1 to 9 and each rectangle width the numbers 1 to 4.

### Twin Corresponding Sudokus II

##### Stage: 3 and 4 Challenge Level:

Two sudokus in one. Challenge yourself to make the necessary connections.

format: plain


text:
# Selecting ML algorithm for music composition

I'm a composer and programmer. I'd like to use ML for composing music. There's already research on the general problem of composing by machine in known musical styles. I'm more interested in the specific problem of using a computer to assist myself in composing... to speed up certain repetitive tasks, or to delight me with unexpected ideas.

First, I want to design ML that can make a good/bad categorization on a single short passage. (Not looking to generate music at first.)

It's pretty easier to decompose such a passage into significant features. I can say things like

• maybe there are particular "intervals" (harmony) present, and others absent
• maybe notes are spread out evenly from low to high, or maybe not
• maybe notes are clustered around a certain register rather than spread out
• maybe the rhythm is particularly uneven
• maybe a particular note occurs a lot

I can easily write algorithms that produce a list of such features for any given passage, and I can even be pretty sure that I'm including all of the relevant features. For an "art form," music is surprisingly amenable to analysis.

The trick is, first of all, there will be hundreds of observations, and second, that no one of these observations is good or bad in itself. Say we have three factors A, B, and C. A is not bad alone, but perhaps when A and B occur together, that's bad. But then maybe if A, B, and C all occur together, it becomes good again! And then maybe a fourth factor D, changes the context so that A, B, and C no longer matter much.

This seems like an ML problem, but I wonder what the best approach is?

I can construct or compose hundreds of examples of music labeled good and bad, and identify hundreds of "features" in each example. Some of the features are "present/not present" categories, while others are numbers that can be scaled from 0 to 1.

Is there a general and simple learning algorithm that even an ML novice such as myself (but I'm an experienced programmer) can implement?

I have to generate my own training examples and would be limited to around 300 to 1000, as I'm training it on simple passages in my style. This may actually be good problem to have... my job is not that complex compared to general musical intelligence.

I will contrast this task with image recognition. An image has millions of pixels and the features are spread out, occurring in endless permutations. It makes my head hurt to try to figure out what's going on in a deep neural net.

However, in music, there will be at most a few hundred features. The patterns will not occur in endless permutations. In fact, in stripped-down situations my wild guess is that you could make an 80% accurate classifier by looking at 10 features (assuming you have the weights and combinations well-trained). I'm aiming somewhat higher than that (I need more features and a bit more accurate).

To give you some idea of my data set: I'm in the early stages of writing the software to extract the features, but I have a pretty good mental idea of what I'm aiming for (been composing by hand for years and years). To make this more concrete I created a web page showing what the data will look like. This is very simple and just totally made up.

http://theunexpectedpearl.com/dataset.html

This shows four rows. Each row is an example passage of music that I will compose or generate automatically. Each row is classified "good" or "bad" by my own ear. The rest of the row is a long list of features, each of which is either "yes/no" or a single number scaled from 0.0 to 1.0.

I don't think a Naive Bayes classifier will work for this (if I understand correctly) because features are neither good nor bad in and of themselves... it is only combinations of them (context) that matter. In fact, the very same feature can contribute to "goodness" in one context, and be an immediate strong indication of "bad" in another context.

The data could possibly be structured differently. In practice, this spreadsheet would be "sparse" and maybe features should be grouped in some way, or arranged in a tree.

• To do good/bad categorization you will need labeled data. Do you have access this kind of data? How many instances do you have? How many features do you have? – JahKnows May 23 '18 at 3:54
• Edited to make this clear. I can make hundreds of examples labeled good/bad, maybe thousands by making slight variations, and I can compute the values of maybe 100 to 300 features in each example (depending on what proves to be useful or practical). – composerMike May 23 '18 at 4:04
• Ideally you want to maximize the information entropy of your features, that means the smaller the number of features, but the most information in those features. For a problem this complex you will need in the tens of thousands of instances. Can you not get more data from publicly available datasets? – JahKnows May 23 '18 at 4:07
• No public data because I'm training it on my own style, and also because I'm working with a simplified representation of music. The problem may be less complex than you anticipate.. there aren't that many patterns that make music good or bad, especially in my simplified representation. But no features are meaningful alone--I need some way to recognize the ways that simultaneous occurrence of features is significant. – composerMike May 23 '18 at 4:34
• Also even a less audacious first goal, such as identifying the "top ten" features and experimenting with that (rather than aiming for a fully accurate classifier), would be helpful as a way of assisting me with composition. – composerMike May 23 '18 at 4:37

Let us formulate this problem in such a way that it can be understood from a machine learning perspective. You have a set of instances $X$ where each instance $x_i \in \mathbb{R}^m$ where $m$ is the dimensionality of the instance. In other words $m$ is the number of features that describe the instance. Your problem intends to go from a set of features to a class label good or bad. Thus, this is a mapping from $\mathbb{R}^m$ to $y \in \{0, 1\}$.

# How to achieve this mapping?

This is when we will use the machine learning algorithms. We will train a model to effectively approximate the function which gives the output label from a set of inputs. It is evident that sparse features (low information entropy) will complicate the mapping function and will thus provide worse results. This is why feature engineering is of upmost importance for machine learning. It is probably the hardest part of the machine learning pipeline, however it is the lead factor in dictating your results.

You can use some feature reduction techniques in order to remove features which are uninformative with respect to the output label. Some techniques that I use frequently are principle component analysis (PCA), linear discriminant analysis (LDA). Alternatively, you can use some projection methods to reduce the dimensionality of the data whilst maintaining separation between the classes. Such techniques are Isomap, MDS, Spectral Embeddings and TSNE. You can check to see which is best suited for your type of data.

# How to choose a model?

Firstly, your problem is a supervised classification problem. This already narrows the types of models you can use. Furthermore, model selection is based on some key factors such as: the number of instances you have, the number of features per instance and the number of output nodes. You should also keep in consideration that the separability of the probability distribution between the output classes will impact the performance of the model directly. For example discriminating between cars and oranges is much easier than oranges and clementines.

In your case, you have 1,000 instances and around 13 features. This means that deep learning based techniques are possible but discouraged. You do not have enough data. You can then attempt the following popular classification models

• Support Vector Classifier
• Naive Bayes
• K-Nearest Neighbors
• Decision Trees
• Random Forests

To evaluate which model performs the best you will use the accuracy attained with a trained model on a test set. This set should be drawn independently from the training set as to catch overfitting. This is when the model cannot generalize to new data.

# In code

Assuming matrix $X$ contains the data where rows are the instances and columns are the features, and matrix $Y$ contains the labels.

First we split our data into a training and testing set

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33)

from sklearn.svm import SVC
clf = SVC()
clf.fit(X_train, y_train)
print('Score: ', clf.score(X_test, y_test))

from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, y_train)
print('Score: ', neigh.score(X_test, y_test))

from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf.fit(X_train, y_train)
print('Score: ', clf.score(X_test, y_test))

from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators = 100)
forest.fit(X_train, y_train)
print('Score: ', forest.score(X_test, y_test))

This should be a starting point. Let us know if you fall into any problems, and let us know what accuracy you are getting we can then look deeper into these models and better suit them to your data source.

• Thanks! It will be a lot of work now, to write the code that generates my examples and extracts the features, so I'll get crackin. – composerMike May 23 '18 at 7:45
• Good luck! Let us know when questions arise! We'll be happy to give some pointers. – JahKnows May 23 '18 at 7:55
• I had one thought. In some ways it's easier to write an algorithm that compares a test instance with a training instance. It would be an expensive computation until I optimize it, but it would be, in some ways, easier to methodically check similarity and rack up a score, by looping or recursing over tiny similarities and never having to design a fixed feature extraction system. Then I categorize the test instance as the same as the closest training example. Is this kind of the "gestalt" of the K-Nearest Neighbors method, even if inefficient? – composerMike May 23 '18 at 8:22
• Yes this is very much how K-NN works. K-NN is a computationally expensive method. – JahKnows May 23 '18 at 8:24

I'll offer up a few suggestions to get the creative juices flowing.

First, since you have two tasks, you could consider two models configured as a Generative Adversarial Network where

• One model generates musical passages
• One model classifies musical passages

The output from the generative model is then evaluated by the classifier. The output from the classifier can then be used to improve (train) the generative model.

I imagine these would be trained on the same labeled data set. I would use a Recurrent Neural Network to train the classifier and tune the snot out of it (so to speak). I would use a Generative Neural Network for the generative model.

Maybe you were looking for more direction. If so, sorry to disappoint. I figured you may have been looking for better ways to search for help.

Best of luck.

• This answer does not address the question. Why would you use a GAN for classifying passages as good/bad? – JahKnows May 23 '18 at 4:26


text:
# Current in Diode.

My question is related to the flow of current in a diode.
In a metal, the current is completely due to the flow of electrons (Eventhough, we always consider the conventional direction for current). Whereas, the current in diode is said to be the sum of the component of current due to holes and due to electrons. The electronic current can easily pass through the wire through which the diode is connected but where does the hole current go?


text:
# Math Help - Logarithms - help please!

1. ## Logarithms - help please!

Log3x – log3 (x-5) = log3 A
Express A in terms of x.
Hence, solve the equation
Log3 x – log3 (x-5) = 1

Thank you so much.

2. every single 3 is a base, it just didn't show up.

3. Originally Posted by kayteee92
Log3x – log3 (x-5) = log3 A
Express A in terms of x.
Hence, solve the equation
Log3 x – log3 (x-5) = 1

Thank you so much.
Recall that $\log_a X - \log_a Y = \log_a \frac XY$

So that,

$\log_3 x - \log_3 (x - 5) = \log_3 A$

$\Rightarrow \log_3 \frac x{x - 5} = \log_3 A$

Now what?

4. oh okay, so next i did

A = x/x-5

and i crossed multiplied to get x = A(x-5)

but for the next part of the problem, i replaced this for x and now i am stuck.

5. Originally Posted by kayteee92
oh okay, so next i did

A = x/(x-5)
stop there. that is A in terms of x.

but for the next part of the problem, i replaced this for x and now i am stuck.


text:
# Intersection of Open Intervals of Positive Reals is Empty

## Theorem

Let $\R_{> 0}$ be the set of strictly positive real numbers.

For all $x \in \R_{> 0}$, let $A_x$ be the open real interval $\openint 0 x$.

Then:

$\displaystyle \bigcap_{x \mathop \in \R_{> 0} } A_x = \O$

## Proof

Let $\displaystyle A = \bigcap_{x \mathop \in \R_{> 0} } A_x$.

Aiming for a contradiction, suppose $A \ne \O$.

Then:

$\exists y \in \R_{> 0}: y \in A$

By definition of open interval:

$y \notin \openint 0 y = A_y$

and so by definition of intersection of family:

$y \notin A$

From this contradiction it follows that $A$ has no elements.

That is:

$\displaystyle \bigcap_{x \mathop \in \R_{> 0} } A_x = \O$

$\blacksquare$


text:
# What is the standard form of a polynomial (4x^2+2x+5)+(7x^2-5x+2)?

The standard form would be $11 {x}^{2} - 3 x + 7$.
$\left(4 {x}^{2} + 2 x + 5\right) + \left(7 {x}^{2} - 5 x + 2\right)$
$= \left(4 + 7\right) {x}^{2} + \left(2 - 5\right) x + \left(5 + 2\right)$
$= 11 {x}^{2} - 3 x + 7$


text:
# Revision history [back]

You could make a list of all your points and then use the function point or you could make a point for each one and then add them (by syntax like point((1,2,3))+point((2,3,4))+point((3,4,5))).


text:
# Balance the following skeletal equation: Pb(NO_3)_2 (aq) + KI (aq) \to PbI_2 (s) + KNO_3 (aq)...

## Question:

Balance the following skeletal equation:

{eq}\rm{Pb}(NO_3)_2\ (aq) + KI\ (aq) \to PbI_2\ (s) + KNO_3\ (aq) {/eq}

Explain how to get the products and reactants.

## Double Displacement Reaction:

The chemical reaction in which one component of each of the reacting molecules is exchanged to form the products. In this reaction, two chemical species are produced. The bonds formed during the reaction may be either covalent or ionic.


text:
# SRFI 122: Nonempty Intervals and Generalized Arrays

status: final (2016-12-24)

## Abstract

This SRFI specifies an array mechanism for Scheme. Arrays as defined here are quite general; at their most basic, an array is simply a mapping, or function, from multi-indices of exact integers $i_0,\ldots,i_{d-1}$ to Scheme values. The set of multi-indices $i_0,\ldots,i_{d-1}$ that are valid for a given array form the domain of the array. In this SRFI, each array's domain consists of a rectangular interval $[l_0,u_0)\times[l_1,u_1)\times\cdots\times[l_{d-1},u_{d-1})$, a subset of $\mathbb Z^d$, $d$-tuples of integers. Thus, we introduce a data type called intervals, which encapsulate the cross product of nonempty intervals of exact integers. Specialized variants of arrays are specified to provide portable programs with efficient representations for common use cases.


text:
outline image over entire page width

I am probably not the first to ask this question but I have tried every answer I could find before asking it here. The problem is that I want an image (eps) be on the top of the page.

I want to add a header on the top of the page starting at an inch from the top and starting at the side of the page (no margin). I have tried \noindent , raisebox & makebox, outlining it to the left.... All the answers I could find.

However, I keep getting this. I want it to be higher and starting from the left side of the paper.

Code:

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage[a4paper]{geometry}
\usepackage{epstopdf}
\begin{document}
\begin{titlepage}
\begin{center}
\raisebox{100pt}{\includegraphics[width=\paperwidth]{figures/eb.eps}}
\end{center}
\end{titlepage}
\end{document}

-
Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. –  hugovdberg Mar 31 '14 at 12:10

\documentclass{article}
\usepackage{graphicx}
\usepackage{eso-pic}

\begin{document}
\AtPageUpperLeft{\raisebox{-\height}{\includegraphics{example-image-a}}}}

blub
\end{document}

-
Works like a charm. Thank you very much! –  user2883786 Mar 31 '14 at 13:07

With tikz

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikz[remember picture,overlay]\node[anchor=north,inner sep=0pt,outer sep=0pt] at
([yshift=-1in]current page.north)
{\includegraphics[width=\paperwidth,height=0.75in]{example-image-a}};

blub
\end{document}

Adjust yshift, width and height suitable as you need.

-


text:
Relative velocities of decay products

1. Sep 8, 2009

w3390

1. The problem statement, all variables and given/known data

A particle of mass M decays into two identical particles each of mass m, where m = 0.3M. Prior to the decay, the particle of mass M has a total energy of 5Mc^2 in the laboratory reference frame. The velocities of the decay product are along the direction of motion M. Find the velocities of the decay products in the laboratory reference frame.

2. Relevant equations

E=$$\frac{mc^2}{\sqrt{1-\frac{u^2}{c^2}}}$$

3. The attempt at a solution

I think I have the concept down in my head. I know I will get two different velocities for the two identical particles. I think it will similar to a doppler effect where the particle formed in the direction of motion gets a speed boost, whereas the particle formed in the direction opposite of motion has a slower speed. So far I have used the equation above to find that the initial particle is traveling at .98c. However, this is where I am confused because how do I figure out how fast the particles resulting from the decay of the original particle move?

2. Sep 8, 2009

Staff: Mentor

Hint: The velocities of the decay product are along the direction of motion M.

Two particles of equal mass. Think conservation of momentum.

3. Sep 8, 2009

w3390

Ya, that's what I was going with. So I set up MV=mv+mv. So MV=.6Mv. Since V=.98c and the M's all cancel, I am getting v=1.63c. Have I made an incorrect assumption or maybe some mathematical error?

4. Sep 8, 2009

Staff: Mentor

Realize that it's relativistic. And the velocities are vectors.

In the COM, the inertial frame of the initial particle of M, the net momentum = 0. One particle moves forward in along the initial trajectory and the other particle moves backward.

In COM frame, the net momentum must still be zero for the two particles.

and don't forget $\gamma{mv}$

It's been a while since I've done relativistic mechanics, so I have to pull the cobwebs out of my belfrey.

5. Sep 9, 2009

w3390

If I set it up so the momentum of the two particles is zero, then:

$$p_{1}$$+$$p_{2}$$=0

$$p_{1}$$=-$$p_{2}$$

Using $$\vec{p}$$=$$\frac{m\vec{u}}{\sqrt{1-\frac{u^2}{c^2}}}$$

$$\frac{mu}{\sqrt{1-\frac{u^2}{c^2}}}$$= -$$\frac{mu}{\sqrt{1-\frac{u^2}{c^2}}}$$

This is where I am confused. Is the u in the numerator the same as the u in the denominator. I do know that the u in the numerator on the left side is equal in magnitude as the u on the right side but opposite in sign. It seems like I need another equation. Can anybody help me out by pointing me in the right direction. Any input is much appreciated.


text:
# 飞轮储能

NASA G2飞轮

## 物理特性

### 能量密度

${\displaystyle {\frac {E}{m}}=K\left({\frac {\sigma }{\rho }}\right)}$,

${\displaystyle E}$ - 轉子的動能[J]
${\displaystyle m}$ - 轉子的質量[kg]
${\displaystyle K}$ - 轉子的幾何形狀係數[無量綱]
${\displaystyle \sigma }$ - 材料的拉伸強度[Pa]
${\displaystyle \rho }$ - 材料的密度[kg/m^3]

## 应用

### 脉冲电源

Flywheel Energy Storage Systems (FESS) are found in a variety of applications ranging from grid-connected energy management to uninterruptible power supplies. With the progress of technology, there is fast renovation involved in FESS application. Examples include high power weapons, aircraft powertrains and shipboard power systems, where the system requires a very high-power for a short period in order of a few seconds and even milliseconds.Compensated pulsed alternator (compulsator) is one of the most popular choices of pulsed power supplies for fusion reactors, high-power pulsed lasers, and hypervelocity electromagnetic launchers because of its high energy density and power density, which is designed for FESS[5].

## 参考

1. ^ Torotrak Toroidal variable drive CVT 互联网档案馆存檔，存档日期2011-05-16., retrieved June 7, 2007.
2. Castelvecchi, Davide. Spinning into control: High-tech reincarnations of an ancient way of storing energy. Science News. May 19, 2007, 171 (20): 312–313 [2013-02-01]. doi:10.1002/scin.2007.5591712010. （原始内容存档于2014-06-06）.
3. ^ Flybrid System KERS using carbon fiber flywheel 互联网档案馆存檔，存档日期2016-03-03.
4. ^ Genta, Giancarlo. Kinetic Energy Storage. London: Butterworth & Co. Ltd. 1985.
5. ^ https://ieeexplore.ieee.org/document/7065239/


text:
# svmtrain

Train support vector machine classifier

`svmtrain` will be removed in a future release. See `fitcsvm`, `ClassificationSVM`, and `CompactClassificationSVM` instead.

## Syntax

`SVMStruct = svmtrain(Training,Group)SVMStruct = svmtrain(Training,Group,Name,Value)`

## Description

`SVMStruct = svmtrain(Training,Group)` returns a structure, `SVMStruct`, containing information about the trained support vector machine (SVM) classifier.

`SVMStruct = svmtrain(Training,Group,Name,Value)` returns a structure with additional options specified by one or more `Name,Value` pair arguments.

## Input Arguments

 `Training` Matrix of training data, where each row corresponds to an observation or replicate, and each column corresponds to a feature or variable. `svmtrain` treats `NaN`s or empty strings in `Training` as missing values and ignores the corresponding rows of `Group`. `Group` Grouping variable, which can be a categorical, numeric, or logical vector, a cell vector of strings, or a character matrix with each row representing a class label. Each element of `Group` specifies the group of the corresponding row of `Training`. `Group` should divide `Training` into two groups. `Group` has the same number of elements as there are rows in `Training`. `svmtrain` treats each `NaN`, empty string, or `'undefined'` in `Group` as a missing value, and ignores the corresponding row of `Training`.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

`'autoscale'`

Boolean specifying whether `svmtrain` automatically centers the data points at their mean, and scales them to have unit standard deviation, before training.

Default: `true`

`'boxconstraint'`

Value of the box constraint `C` for the soft margin. `C` can be a scalar, or a vector of the same length as the training data.

If `C` is a scalar, it is automatically rescaled by `N/(2*N1)` for the data points of group one and by `N/(2*N2)` for the data points of group two, where `N1` is the number of elements in group one, `N2` is the number of elements in group two, and `N = N1 + N2`. This rescaling is done to take into account unbalanced groups, that is cases where `N1` and `N2` have very different values.

If `C` is an array, then each array element is taken as a box constraint for the data point with the same index.

Default: `1`

`'kernelcachelimit'`

Value that specifies the size of the kernel matrix cache for the SMO training method. The algorithm keeps a matrix with up to `kernelcachelimit` × `kernelcachelimit` double-precision, floating-point numbers in memory.

Default: `5000`

`'kernel_function'`

Kernel function `svmtrain` uses to map the training data into kernel space. The default kernel function is the dot product. The kernel function can be one of the following strings or a function handle:

• `'linear'` — Linear kernel, meaning dot product.

• `'quadratic'` — Quadratic kernel.

• `'polynomial'` — Polynomial kernel (default order 3). Specify another order with the `polyorder` name-value pair.

• `'rbf'` — Gaussian Radial Basis Function kernel with a default scaling factor, sigma, of 1. Specify another value for sigma with the `rbf_sigma` name-value pair.

• `'mlp'` — Multilayer Perceptron kernel with default scale `[1 –1]`. Specify another scale with the `mlp_params` name-value pair.

• `@kfun` — Function handle to a kernel function. A kernel function must be of the form

`function K = kfun(U, V)`

The returned value, `K`, is a matrix of size `M`-by-`N`, where `U` and `V` have `M` and `N` rows respectively.

If `kfun` has extra parameters, include the extra parameters via an anonymous function. For example, suppose that your kernel function is:

```function k = kfun(u,v,p1,p2) k = tanh(p1*(u*v')+p2);```

Set values for `p1` and `p2`, and then use an anonymous function:

`@(u,v) kfun(u,v,p1,p2)`

Default: `'linear'`

`'kktviolationlevel'`

Value that specifies the fraction of variables allowed to violate the Karush-Kuhn-Tucker (KKT) conditions for the SMO training method. Set any value in [0,1). For example, if you set `kktviolationlevel` to `0.05`, then 5% of the variables are allowed to violate the KKT conditions.

 Tip   Set this option to a positive value to help the algorithm converge if it is fluctuating near a good solution.

Default: `0`

`'method'`

Method used to find the separating hyperplane. Options are:

• `'QP'` — Quadratic programming (requires an Optimization Toolbox™ license). The classifier is a 2-norm soft-margin support vector machine. Give quadratic programming options with the `options` name-value pair, and create `options` with `optimset`.

• `'SMO'` — Sequential Minimal Optimization. Give SMO options with the `options` name-value pair, and create `options` with `statset`.

• `'LS'` — Least squares.

Default: `SMO`

`'mlp_params'`

Parameters of the Multilayer Perceptron (`mlp`) kernel. The `mlp` kernel requires two parameters, ```[P1 P2]```. The kernel `K = tanh(P1*U*V' + P2)`, where `P1  > 0` and `P2 < 0`.

Default: `[1 –1]`

`'options'`

Options structure for training.

• When you set `'method'` to `'SMO'` (default), create the `options` structure using `statset`. Options are:

 `Display` String that specifies the level of information about the optimization iterations that is displayed as the algorithm runs. Choices are:`off` (default) — Reports nothing.`iter` — Reports every 500 iterations.`final` — Reports only when the algorithm finishes. `MaxIter` Integer that specifies the maximum number of iterations of the main loop. If this limit is exceeded before the algorithm converges, then the algorithm stops and returns an error. Default is `15000`.

The other name-value pairs that relate specifically to the `'SMO'` method are `kernelcachelimit`, `kktviolationlevel`, and `tolkkt`.

• When you set `method` to `'QP'`, create the options structure using `optimset`. For details of applicable option choices, see `quadprog` options. SVM uses a convex quadratic program, so you can choose the `'interior-point-convex'` `quadprog` algorithm. In limited testing, the `'interior-point-convex'` algorithm was the best `quadprog` option for `svmtrain`, in both speed and memory utilization.

`'polyorder'`

Order of the polynomial kernel.

Default: `3`

`'rbf_sigma'`

Scaling factor (sigma) in the radial basis function kernel.

Default: `1`

`'showplot'`

Boolean indicating whether to plot the grouped data and separating line. Creates a plot only when the data has two columns (features).

Default: `false`

`'tolkkt'`

Value that specifies the tolerance with which the Karush-Kuhn-Tucker (KKT) conditions are checked for the SMO training method. For a definition of KKT conditions, see Karush-Kuhn-Tucker (KKT) Conditions.

Default: `1e-3`

## Output Arguments

 `SVMStruct` Structure containing information about the trained SVM classifier in the following fields: `SupportVectors` — Matrix of data points with each row corresponding to a support vector in the normalized data space. This matrix is a subset of the `Training` input data matrix, after normalization has been applied according to the `'AutoScale'` argument. `Alpha` — Vector of weights for the support vectors. The sign of the weight is positive for support vectors belonging to the first group, and negative for the second group.`Bias` — Intercept of the hyperplane that separates the two groups in the normalized data space (according to the `'AutoScale'` argument). `KernelFunction` — Handle to the function that maps the training data into kernel space.`KernelFunctionArgs` — Cell array of any additional arguments required by the kernel function.`GroupNames` — Categorical, numeric, or logical vector, a cell vector of strings, or a character matrix with each row representing a class label. Specifies the group identifiers for the support vectors. It has the same number of elements as there are rows in `SupportVectors`. Each element specifies the group to which the corresponding row in `SupportVectors` belongs.`SupportVectorIndices` — Vector of indices that specify the rows in `Training`, the training data, that were selected as support vectors after the data was normalized, according to the `AutoScale` argument.`ScaleData` — Field containing normalization factors. When `'AutoScale'` is set to `false`, it is empty. When `AutoScale` is set to `true`, it is a structure containing two fields:`shift` — Row vector of values. Each value is the negative of the mean across an observation in `Training`, the training data.`scaleFactor` — Row vector of values. Each value is `1` divided by the standard deviation of an observation in `Training`, the training data.Both `svmtrain` and `svmclassify` apply the scaling in `ScaleData`.`FigureHandles` — Vector of figure handles created by `svmtrain` when using the `'Showplot'` argument.

## Examples

collapse all

### Train an SVM Classifier

Find a line separating the Fisher iris data on versicolor and virginica species, according to the petal length and petal width measurements. These two species are in rows 51 and higher of the data set, and the petal length and width are the third and fourth columns.

```load fisheriris xdata = meas(51:end,3:4); group = species(51:end); svmStruct = svmtrain(xdata,group,'ShowPlot',true); ```

collapse all

### Karush-Kuhn-Tucker (KKT) Conditions

The Karush-Kuhn-Tucker (KKT) conditions are analogous to the condition that the gradient must be zero at a minimum, modified to take constraints into account. The difference is that the KKT conditions hold for constrained problems. The KKT conditions use the auxiliary Lagrangian function:

$L\left(x,\lambda \right)=f\left(x\right)+\sum {\lambda }_{g,i}{g}_{i}\left(x\right)+\sum {\lambda }_{h,i}{h}_{i}\left(x\right).$

Here f(x) is the objective function, g(x) is a vector of constraint functions g(x) ≤ 0, and h(x) is a vector of constraint functions h(x) = 0. The vector λ, which is the concatenation of λg and λh, is the Lagrange multiplier vector. Its length is the total number of constraints.

The KKT conditions are:

### Algorithms

The `svmtrain` function uses an optimization method to identify support vectors si, weights αi, and bias b that are used to classify vectors x according to the following equation:

$c=\sum _{i}{\alpha }_{i}k\left({s}_{i},x\right)+b,$

where k is a kernel function. In the case of a linear kernel, k is the dot product. If c ≥ 0, then x is classified as a member of the first group, otherwise it is classified as a member of the second group.

### Memory Usage and Out of Memory Error

When you set `'Method'` to `'QP'`, the `svmtrain` function operates on a data set containing `N` elements, and it creates an `(N+1)`-by-`(N+1)` matrix to find the separating hyperplane. This matrix needs at least `8*(n+1)^2` bytes of contiguous memory. If this size of contiguous memory is not available, the software displays an "out of memory" error message.

When you set `'Method'` to `'SMO'` (default), memory consumption is controlled by the `kernelcachelimit` option. The SMO algorithm stores only a submatrix of the kernel matrix, limited by the size specified by the `kernelcachelimit` option. However, if the number of data points exceeds the size specified by the `kernelcachelimit` option, the SMO algorithm slows down because it has to recalculate the kernel matrix elements.

When using `svmtrain` on large data sets, and you run out of memory or the optimization step is very time consuming, try either of the following:

• Use a smaller number of samples and use cross-validation to test the performance of the classifier.

• Set `'Method'` to `'SMO'`, and set the `kernelcachelimit` option as large as your system permits.

## References

[1] Kecman, V., Learning and Soft Computing, MIT Press, Cambridge, MA. 2001.

[2] Suykens, J.A.K., Van Gestel, T., De Brabanter, J., De Moor, B., and Vandewalle, J., Least Squares Support Vector Machines, World Scientific, Singapore, 2002.

[3] Scholkopf, B., and Smola, A.J., Learning with Kernels, MIT Press, Cambridge, MA. 2002.

[4] Cristianini, N., and Shawe-Taylor, J. (2000). An Introduction to Support Vector Machines and Other Kernel-based Learning Methods, First Edition (Cambridge: Cambridge University Press). `http://www.support-vector.net/`


text:
Question about computing Jacobians of transformations

by Boorglar
Tags: computing, jacobians, transformations
 P: 173 Suppose I have the following transformation: $$u = \frac{x}{x^2+y^2+z^2}$$ $$v = \frac{y}{x^2+y^2+z^2}$$ $$w = \frac{z}{x^2+y^2+z^2}$$ Is there a fast way to calculate the determinant jacobian without having to deal with the whole 3x3 determinant? I noticed that the inverse transformation is the same (switching x,y,z with u,v,w gives the equality again) but the determinant is not 1, so I don't really know if this can help. Or would I really have to do it the long and boring way?
 HW Helper P: 2,263 Your inverse is wrong. Sure there is a fast way, consider the slight generalization where the denominator is a function R so your case is R=x^2+y^2+z^2 then we have $$J= \left| \begin{array}{ccc} u_x & u_y & u_z \\ v_x & v_y & v_z \\ w_x & w_y & w_z \end{array} \right| =\frac{1}{R^3} \left| \begin{array}{ccc} 1 - x R_x/R & -x R_y/R & -x R_z/R \\ -y R_x/R & 1 - y R_y/R & -y R_z/R \\ -z R_x/R & -z R_y/R & 1 - z R_z/R \end{array} \right| =\frac{1-(x R_x+y R_y+z R_z)/R}{R^3}$$ The determinant is closely related to $$\left| \begin{array}{ccc} a-U & -V & -W \\ -U & b-V & -W \\ -U & -V & c-W \end{array} \right| =abc-bc U-ac V-abW$$


text:
# Exploring pygame 5 - Movement and Collision

Published on

Movement is part of a large portion of games. When jumping between platforms, shooting against a horde of enemies, piloting a space ship and running through the streets, we are causing movement and interacting with the game environment, applying action and causing reactions.

This chapter is to describe the basics of moving objects across the screen and their interaction with other elements through collision detection.

## Movement

If you are following this series of posts, you already saw an example of movement at the post about the game loop, where we implemented a ball that moves through the screen bouncing around.

This time we'll go through a similar code sample, adding more details to it and also, adding some new concepts:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  import pygame BLACK = pygame.Color(0, 0, 0) WHITE = pygame.Color(255, 255, 255) pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('Simple Movement') position_x = 0 while True: event = pygame.event.poll() if event.type == pygame.QUIT: break # moves the square one pixel per cycle position_x += 1 screen.fill(BLACK) # draws the square with the incremented position pygame.draw.rect(screen, WHITE, [position_x, 230, 20, 20]) pygame.display.flip()

The code is pretty straight forward. We create a variable position_x to hold the square position on x.

Inside the loop, we increment its position by one pixel per cycle and then it's drawn again at its new position.

The drawing loop creates a cinematic effect of movement on the screen, but this implementation has a problem.

You can't control the object movement velocity, and powerful the computer, more loops are processed, causing results like that:

To fix this issue, we need to go back to physics class, when we learned about linear motion. To ensure constant velocity, we'll use the following formula:

$d = d_{i} + v \Delta t$

Applying it to the code, we'll have this:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  import time import pygame BLACK = pygame.Color(0, 0, 0) WHITE = pygame.Color(255, 255, 255) pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('Velocity') position_x = 0 # 100 pixels per second velocity_x = 100 # capture the initial time ti = time.time() while True: # gets the time for # this cycle tf = time.time() # calculate the delta dt = (tf - ti) # sets final time as the initial time ti = tf event = pygame.event.poll() if event.type == pygame.QUIT: break # moves the square at the velocity defined position_x += velocity_x * dt screen.fill(BLACK) pygame.draw.rect(screen, WHITE, [position_x, 230, 20, 20]) pygame.display.flip()

We start defining the velocity at the x-axis to 100 pixels per second.

Then we get the initial time to be used at the delta time calculation.

Inside the loop, we capture the final time and at the next line, the dt (delta time) subtracting the final time by the initial time.

At line 28, the initial time receives the final time to be used at the next loop cycle.

In the end, we calculate the displacement at line 36.

As we can see, it is possible to control the object movement velocity, but that solves only the visible part of the issue, the loop executes much more than the necessary generating noting more than overhead.

## FPS

These drawing cycles are also known as frames, and the frame rate control is a common practice among the audio-visual professionals. The measuring unit used on this kind of control is the FPS (Frames Per Second).

Implementing this frame control gives you some benefits:

• Reduces de unnecessary machine overload
• Makes the multiplayer game sync easier to implement
• Reduces the error propagation of the floating-point operation (there are techniques to mitigate this kind of issue, but if you are starting, don't spend time on that yet)
• Increases the predictability and facilitates the process of planning your game. Now you can calculate how many processing you can have during a frame interval once you know the minimum requirements of your game.

The traditional refresh rate for movies is 24fps, and in games, it can change from 30 to 60fps. In our case, we'll use the 30fps refresh rate.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37  import pygame BLACK = pygame.Color(0, 0, 0) WHITE = pygame.Color(255, 255, 255) pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('FPS') position_x = 0 # since pygame clock returns its value # in milliseconds, we divide the velocity # by 1000 to keep the 100 pixel per seconds velocity_x = 0.1 # create pygame clock clock = pygame.time.Clock() while True: # call the clock tick for 30 fps # and store the delta time dt = clock.tick(30) event = pygame.event.poll() if event.type == pygame.QUIT: break position_x += velocity_x * dt screen.fill(BLACK) pygame.draw.rect(screen, WHITE, [position_x, 230, 20, 20]) pygame.display.flip()

Thanks to pygame's Clock implementation we could simplify things a little.

Different than Python's time library that works on a scale of seconds, pygame's Clock works on milliseconds, so we changed the velocity from 100 to 0.1 to ensure the same velocity of a hundred pixels per second.

Next, we instantiate Clock before entering the loop, and then, inside the loop, we call the tick function passing as argument the amount of FPS to limit the loop.

The tick function must be called on each cycle, so if a cycle runs faster than the expected rate, it sleeps for a brief moment to ensure the defined rate. As a result, the function returns the delta time between this loop and the previous one.

Take a look at the tick function documentation. It has a difference of precision across platforms, but there is an alternate version of this function that is more precise (unfortunately uses much CPU), and you can use it if this level of precision is important to your game.

We have the square sliding through the screen on a constant speed, so let's proceed to the collision detection.

## Collision

The collision is the product of the interaction between the objects of your game. Their interaction can produce it, or the interaction with the environment.

The collision detection tends to grow on complexity as long as you add more elements with different formats to the scene.

At the example below, we'll see its basic concept by making the square interact with two pads, changing direction after colliding with them.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  import pygame BLACK = pygame.Color(0, 0, 0) WHITE = pygame.Color(255, 255, 255) pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('Collision') # create the square Rect square = pygame.Rect(300, 230, 20, 20) # create the pads Rect left_pad = pygame.Rect(20, 210, 20, 60) right_pad = pygame.Rect(600, 210, 20, 60) pads = [left_pad, right_pad] velocity_x = 0.1 clock = pygame.time.Clock() while True: dt = clock.tick(30) event = pygame.event.poll() if event.type == pygame.QUIT: break # use the move function inplace square.move_ip(velocity_x * dt, 0) # check for collision with the pads if square.collidelist(pads) >= 0: velocity_x = -velocity_x screen.fill(BLACK) # draw using the rect pygame.draw.rect(screen, WHITE, square) # draw the pads for pad in pads: pygame.draw.rect(screen, WHITE, pad) pygame.display.flip()

The most straightforward collision detection consists of treating all elements as rectangular areas. Pygame has it implemented on the Rect class. It was used at the code to create a rectangular area to the square and for the pads.

With the creation of Rect we start to use the function move_ip to move it. This function changes the caller position in place, different from the move function that produces a new object with the position changed.

At line 37 the collidelist function checks if any collision happened to return the collided object index from the list passed. In case there is no collision, it returns -1.

In the end, the square and the pads are drawn to the screen using their Rect instances to produce the following result:

## Conclusion

With these concepts of movement and collision, you can create some exciting games like Pong. I'll end this post with a challenge to you. Use these concepts to implement the Pong game.

The code used to make this post is available at exploring-pygame.


text:
# Estimate the power of the laser on board of Darth Vader's Death Star

1. Apr 3, 2008

### Count Iblis

The laser is able to vaporize an Earth sized planet in about a second.

2. Apr 3, 2008

### mgb_phys

Then you just need the mass of Earth and the heat of vaporisation of rock ( or the crust)

3. Apr 3, 2008

### Count Iblis

We need to do more than that. The Earth has to be destroyed in just a second. When a huge amount of energy is delivered to the surface a big schockwave develops. This has to travel through the entire planet in a second.

4. Apr 3, 2008

### mgb_phys

Speed of sound through earth is only about 8-10km/s so to put a mach 1000 hypersonic shockwave through earth is going to take some serious energy into the crust on one side.
Still if you intending to convert 6x10^24kg of rock to gas that's going to take quite a bit of energy anyway leaving aside the dynamics ;-)

5. Apr 3, 2008

### leftyguitarjo

allright, the mass of the earth is 5.97x10^24

I'd like to see this solved. It would also be a neat problem to show to my physics teacher.

6. Apr 3, 2008

### vanesch

Staff Emeritus
Ok, let us estimate: latent heat of evaporation, say 1000 J/g (see http://en.wikipedia.org/wiki/Latent_heat) or 1 MJ/kg. That brings us to about 6 10^30 J, or let's say, 10^31 J.

Now, a supernova has an energy output of something (if I remember well) like 10^42 J.
So if the death star runs on supernovas, one supernova has more than 100 billion "bullets" that can blow up the earth...

7. Apr 3, 2008

### Staff: Mentor

Or 2x10^15 megatons, which is somewhere around 200 billion times the nuclear warheads on earth.

8. Apr 3, 2008

### mgb_phys

Russ - you've obviously been planning this, should we be worried?

9. Apr 5, 2008

### Crosson

(1000th post!)

There is a neat method for doing problems like this that doesn't get taught to undergraduates as I feel it shoud be:

http://en.wikipedia.org/wiki/Buckingham_π_theorem

It's called Buckingham's pi theorem, where pi refers not to Archimedes's constant in this case but to the defacto appearance of the greek character in the notation. Anyway, Buckingham's theorem formalizes the use of dimensional analysis to generate relevant equations out of no where. When I saw this question I thought of the classic example whereby the energy released in the trinity test of the atomic bomb was estimated before the actual value was declassified. Fortunately Wikipedia has this example:

http://en.wikipedia.org/wiki/Buckingham_π_theorem#The_atomic_bomb

The equation we get is:

$$R = (\frac{E t^2}{\rho})^{\frac{1}{5}}$$

This equation describes the expansion of the radius of the blast as a function of time in terms of the energy of the blast and the density of the environment. If we use the average density of the earth, the radius of the earth, and a destruction time of 0.5 seconds (maybe someone with a stop watch and a new hope DVD can do a better estimate) I calculate a total energy of 2*10^32 Joules, which would be over a trillion megatons!

10. Apr 5, 2008

### neu

This is such a nerdy thread.

What about the power ouput of captain kirk's gun when set to stun?

btw, Crosson thanks for posting that, I hadn't heard of Buckingham's pi theorem, I'm finding it very interesting

Last edited: Apr 5, 2008
11. Apr 7, 2008

### Minich

It is very difficult problem. A screen of ultrarelativistic plasma will be created in the first moment and diisipation of energy will be mainly in this plasma screen

At such power densities vacuum will be olso plasma of particle-antiparticle

And what is the laser freaquency and width of the ray? How do we focus it?

12. Jun 21, 2008

### Count Iblis

Perhaps it could be focussed using a black hole as a gravitational lens?

13. Jun 21, 2008

### Janus

Staff Emeritus
Interesting that that number works out to very close to the gravitational binding energy of the Earth (2.24*10^32 J)

14. Jun 22, 2008

### swraman

Well.. since this is for a High school class (im giessing since leftyguitarjo said to show his teacher), I think it could be simplified, couldnt it? I mean just not overthought.

The original Q is to vaporize teh earth - so use the melting point and heat of vaporization of Rock and the mass of the earth as your data. Then its a simple thermo problem.

When you find the total energy needed, that is the power since the time taken to output that much energy is 1 second.

15. Jun 25, 2008

### DrClapeyron

Assume increased pressure as you go down into the earth's mantle almost 11 times atmospheric level and that the mantle is a plastic moving solid mainly peridotite rock composition, the outer core is liquid iron also under heavy pressure and inner core solid iron.

16. Jun 25, 2008

### Staff: Mentor

Alderaan was not evaporated. There was a lot of debris left.

17. Jun 27, 2008

### Arnan

yeah but you dont know how large Alderaan was, might have been slightly larger than earth.

18. Jul 6, 2008

### per.sundqvist

An estimation using these numbers gives:

$$P=2*10^{32}W,\; <S>=\frac{\epsilon_0c}{2}E^2, \;W/m^2,\; P=A*<S>\; A=\pi R^2,\; R=R_{earth}/100,\; E_{ion,Hydro}=13eV/0.5 Angstrom$$

This gives that the electric field over the bohr-radii is abot 13 times bigger than the "break-down" of an hydrogen atom (E_{ion,Hydro}=13eV/0.5Å). reosonable?


text:
# Bibliotek

Musik » Missy Higgins »

## This Is How It Goes

49 spelade låtar | Gå till låtsida

Låtar (49)
Låt Album Längd Datum
This Is How It Goes 3:32 6 sep 2012, 06:50
This Is How It Goes 3:32 1 sep 2012, 09:57
This Is How It Goes 3:32 24 aug 2012, 07:37
This Is How It Goes 3:32 29 jun 2012, 06:12
This Is How It Goes 3:32 25 feb 2012, 11:26
This Is How It Goes 3:32 18 feb 2012, 09:52
This Is How It Goes 3:32 6 nov 2011, 10:14
This Is How It Goes 3:32 3 nov 2011, 11:07
This Is How It Goes 3:32 9 aug 2011, 17:30
This Is How It Goes 3:32 23 jun 2011, 07:35
This Is How It Goes 3:32 3 jun 2011, 22:51
This Is How It Goes 3:32 30 apr 2011, 23:15
This Is How It Goes 3:32 30 apr 2011, 13:00
This Is How It Goes 3:32 28 apr 2011, 18:10
This Is How It Goes 3:32 25 apr 2011, 19:50
This Is How It Goes 3:32 25 apr 2011, 19:47
This Is How It Goes 3:32 23 apr 2011, 14:14
This Is How It Goes 3:32 17 mar 2011, 12:28
This Is How It Goes 3:32 17 mar 2011, 12:28
This Is How It Goes 3:32 12 mar 2011, 15:15
This Is How It Goes 3:32 20 feb 2011, 22:53
This Is How It Goes 3:32 2 feb 2011, 23:46
This Is How It Goes 3:32 2 feb 2011, 01:13
This Is How It Goes 3:32 1 feb 2011, 23:42
This Is How It Goes 3:32 9 jan 2011, 14:57
This Is How It Goes 3:32 28 jan 2009, 23:53
This Is How It Goes 3:32 30 nov 2008, 16:38
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:04
This Is How It Goes 3:32 13 nov 2008, 01:01
This Is How It Goes 3:32 6 nov 2008, 19:13
This Is How It Goes 3:32 2 nov 2008, 01:09
This Is How It Goes 3:32 26 sep 2008, 09:12
This Is How It Goes 3:32 26 aug 2008, 17:03
This Is How It Goes 3:32 16 aug 2008, 12:15
This Is How It Goes 3:32 3 aug 2008, 18:59
This Is How It Goes 3:32 3 aug 2008, 16:23
This Is How It Goes 3:32 24 jul 2008, 21:18
This Is How It Goes 3:32 24 jul 2008, 21:14
This Is How It Goes 3:32 13 jul 2008, 12:09
This Is How It Goes 3:32 29 jun 2008, 19:27
This Is How It Goes 3:32 26 maj 2008, 19:33