text:
The world’s only live instant tutoring platform
Question

Solving time: 4 mins

# For (the set of natural numbers), the integral is equal to (where is a constant of integration ). (a) (b) (c) (d)

## Solutions

(2)
Exp. (b) Let Now, ,
83

## 1 student asked the same question on Filo

Learn from their 1-to-1 discussion with Filo tutors.

10 mins

Taught by

Connect instantly with this tutor

Total classes on Filo by this tutor - 536

Teaches : Physics, Mathematics, Biology

117
0

Connect with 50,000+ expert tutors in 60 seconds, 24X7

format: plain


text:
## Functions Modeling Change: A Preparation for Calculus, 5th Edition

Multiplying a function by a positive constant greater than $1$ stretches the function vertically. Therefore, the statement is true


text:
I want to put my glossary at the end of my document. I succeeded to do that. But the problem is within the table of contents (TOC): It shows the glossary twice (at the beginning and at the end).

It appears at:

1. the begining because of "\loadglsentries" (I am using an external file):
2. the end where I placed "\printglossary" while using the option "toc" with the "glossaries" package:

My question is: how could I just show the glossary at the end of TOC, in its right place?

Thank you and sorry (it is my first post!)

• That's not normal behaviour. Perhaps you have `\addcontentsline` line somewhere in your document that's confusing things. (You also have two bibliography entries for page 45, so there seems to be some duplication in your document.) Sep 5, 2018 at 18:06
• @NicolaTalbot , yes, I had two entries for the bibliography because I used \addcontentsline and added the "tocbibind" package. I solved it by adding the option 'notbib" like this: \usepackage[nottoc,notbib,notindex]{tocbibind}. For the glossary, I checked the whole code (main.tex) but there is no \addcontentsline for it. Finally I checked my glossary.tex and surprise! There is an \addcontentsline for the glossary! I spend hours on this problem. Really, thank you very much !
– Lyes
Sep 6, 2018 at 7:35


text:
Mind Matters Natural and Artificial Intelligence News and Analysis

# Can You Really Be a Card Counter Without Resorting to Magic?

Math nerd (and successful gambler) Salvador Cordova explains how card counters improve their odds in blackjack

Walter Bradley Center director Robert J. Marks is back withSalvador Cordova, mathematician, engineer — and gambling ace. In previous episodes, they discussed why most players lose and how “advantage players” — those who understand the game, the players, the management, and the mathematical probabilities — sometimes clean out casinos. Of course, they also sometimes get kicked out, as has happened to Cordova, and new rules and precautions ensue. And, doubtless, new ways are found around them.

In this new podcast episode, “Can a good hustler count cards like a computer?”, Cordova says a bit more about how the pros improve their odds from pure chance by card counting. He made his living, inpart, that way from about 2005 through 2014.

This portion begins at 00:36 min. A partial transcript and notes, Show Notes, and Additional Resources follow.

Sal Cordova: On the weekends, I would go to the casinos. My father had passed away and it was a chance for me to just decompress, driving the long distances, my way of coping. It really wasn’t that profitable; it was more or less a side hustle. The professionals, they have to be good con artists. I was not.

Robert J. Marks: They have to be actors, I guess. Right?

Sal Cordova: Very good actors. Persuading the casinos that they’re degenerate gamblers. I would walk in there and I think people knew right away. I kind of stood out. I looked like a scholar and I spoke like one.

If you wander in there and you look like you have brains, it’s like, yeah you’re automatically under suspicion.

Robert J. Marks: I want you to give us a tutorial in card counting, but before I do… how difficult is it? We talked before and you said, “It’s just like playing the piano. You just got to figure out which keys to touch at what time.” But you don’t get to be a concert pianist grand master by figuring out which keys to touch at the right time. It’s not as simple as that.

Sal Cordova: That’s correct. It’s like saying, “Playing tennis is just returning the ball that’s hit to you.”

I’m exaggerating obviously, but if one wanted to card count, there are computer simulations out there that will teach you, examine your ability and grade you…

Robert J. Marks: So they use computers to generate card counting… I thought Thorp did it in 1961 just by looking at the math.

Sal Cordova: Correct. And that counting system was brutally difficult… Now, one of the major roles of the computer was to find an estimation system that a human could actually execute.

Robert J. Marks: You have to have a good short term and maybe even long term memory in order to do this. Is that right?

Sal Cordova: A good short term memory. [You] actually have to be reasonable at arithmetic.

So I’m going to give you the counting system that I used. But this was developed by the computer. It’ll estimate the optimality of your heuristic. Maybe, since you’re a computer guy, you could explain what heuristics are.

Robert J. Marks: It’s just an intuitive algorithm. It’s one you make up by the seat of your pants and your experience, your life experience.

Sal Cordova: Right. So it was just an estimation system. In terms of what would be the optimal bet to put forward, it’s 99% accurate. In terms of the optimal play of your hands, it’s 75%. …

Again, with the Law of Large Numbers, and expectation value, if the theoretical advantage were, say 2%, you could get 1.5% percent, which is enough to put the casinos on their knees if they let you play long enough.

Because what ends up happening is, as you keep winning money, you have an exponential growth law. And so that’s why they want to nip it in the bud. You could start out with $10,000 in what they call your bankroll… They’re not worried if you’re still at the small scale, but some of these guys will grow their bankrolls from like$10,000 to millions. And then they start to become a real threat to the casinos.

Robert J. Marks: One of the challenges is if you have a bankroll, you have to be very careful in the way that you bet it. For example, you don’t want to put your entire bankroll down on one bet. And I learned this… I was in a business called Financial Neural networks, and there’s this risk, security trade off. You have a balance between the risk that you take and the amount of money you make. And the risk was a big choppy curve. It went up, but it shot up, it shot down, it shot up, it shot down and you made lots of money with lots of risk. Whereas if you erred on the side of security, it went up very gradually, but not as much.

And one of the things I found out, which is obvious once you know it, is that if you’re jumping up and down and that curve hits zero, you’re done.

Sal Cordova: Right. That’s called Gambler’s Ruin.

Sal Cordova: And actually this was an important theoretical result by John Kelly who also worked with Bell Labs — an MIT guy, so there must have been some connection in that era between Bell Labs and MIT.

This was an achievement. And it’s actually very simple. If your advantage is, say, 1%, approximately the most you should bet on any one bet is 1%. Beyond that you start to lose efficiency. And if you double it, you’ll go to ruin. So we call it not overbetting.

And preferably, if you wanted to limit that variability, you would only do fractions of what they call “the Kelly.” So when they say “full Kelly,” you’re at your maximum amount of growth but you’re going to have too much variance.

Sal Cordova: So the professionals will often operate at quarter Kelly or one eighth Kelly. Then hedge fund managers realized they could actually import those ideas into management of their hedge funds. This is why it’s really nice to see the casino math play out like that … Just Google Kelly criterion.

And another way of framing it, in terms of just raw metrics, is the expected value versus the variance. They call that the sharp ratio: You divide the expected value by the variance or vice versa…

So back to the actual card counting, they formulated using the computers all these various counting methods, but I’ll give you the one that I used.

Robert J. Marks: So there’s a number of ways to card count, right? And probably some are more difficult and require more memory than others.

Sal Cordova: Right. The simpler ones are just a single count. And I’ll explain how that is. So let’s say you keep a running sum. So if you see a certain card, like an ace or an eight in the Advanced Omega II System … that’s the one that I’ve used.

Robert J. Marks: That’s the card counting system you use, Omega II?

Sal Cordova: Right. If you see an ace or an eight, you just add zero to your running total…

Next: With basic theory out of the way, the arithmetic of card counting

Note: This information is intended for math nerds and people entertained by probability theory and statistics. It is not directed at anyone who has a gambling addiction. For help, see Gamblers Anonymous.

You may also wish to read: Can casinos ban would-be customers who might get TOO “lucky”? Sal Cordova was good enough at card counting that his photo was circulated and the casino nabbed his driver’s licence… But as casinos slowly eliminate loopholes, “math whiz” advantage players seem to find others.

Here are the writeups (with partial transcripts, notes, and links) of the previous podcasts with gambling maven Sal Cordova:

Gambling: WHY the house always wins in the long run… The casinos are not cheating. They rely on the Law of Large Numbers, part of the mathematical structure underlying our universe. Robert J. Marks and fellow engineer and mathematician Sal Cordova look at the many creative methods gamblers use to improve their odds.

Casinos: How nerds gamble and win, using the Law of Large Numbers The American Physical Society created Las Vegas’s worst week in history and Don Johnson cleaned out Atlantic City. How? Sal Cordova explains to Robert J. Marks how nerd gamble and win, mainly by deciding whether to play at all and, if so, how to manipulate the house’s strategies.

The struggle between casinos and advantage players The scene is enlivened by assorted other characters who use romance to help in the struggle for a big win. Sal Cordova explains. Often the advantage player, who astounds us by seemingly beating probability, is taking advantage of the casino’s upper management’s weaknesses.

## Show Notes

• 00:06 | Introducing Sal Cordova
• 00:36 | What Does It Take to be a Good Hustler?
• 01:45 | A Lesson In Card Counting
• 15:07 | You Have to Be a Good Actor
• 16:58 | How To Use Your Teeth As a Keyboard
• 19:28 | Blackjack Books
• 21:56 | If You Try to Beat The Game, You Could be Arrested


text:
A simple Python library to provide an API to implement the Reactive Object Pattern (ROP).

# AlleyCat - Reactive

A part of the AlleyCat project which supports the Reactive Object Pattern.

## Introduction

AlleyCat Reactive is a project to explore the possibility of bridging the gap between the two most widely used programming paradigms, namely, the object-oriented programming (OOP), and functional programming (FP).

It aims to achieve its goal by proposing a new design pattern based on the Reactive Extensions (Rx).

Even though it is already available on PyPI repository as alleycat-reactive package, the project is currently at a proof-of-concept stage and highly experimental.

As such, there can be significant changes in the API at any time in future. Furthermore, the project may even be discontinued in case the idea it is based upon proves to be an infeasible one.

So, please use it at your discretion and consider opening an issue if you encounter a problem.

## Reactive Object Pattern

AlleyCat Reactive provides an API to implement what we term as Reactive Object Pattern or ROP for short. Despite its rather pretentious name, it merely means defining class properties which can also serve as an Observable in Rx.

But why do we need such a thing?

We won't delve into this subject too much since you can learn about the concept from many other websites.

In short, it's much better to define data in a declarative manner, or as "data pipelines", especially when it's changing over time. And Rx is all about composing and manipulating such pipelines in a potentially asynchronous context.

But what if the data do not come from an asynchronous source, like Tweets or GUI events, but are just properties of an object? Of course, Rx can handle synchronous data as well, but the cost of using it may outweigh the benefits in such a scenario.

In a traditional OOP system, properties of an object are mere values which are often mutable, but not observable by default. To observe the change of a property over time, we must use an observer pattern, usually in the form of a separate event.

Some implementation of Rx allows converting such an event into an Observable. Still, it can be a tedious task to define an event for every such property, and it requires a lot of boilerplate code to convert them into Rx pipelines.

More importantly, the resulting pipelines and their handling code don't have any clear relationship with the original object or its properties from which they got derived. They are just a bunch of statements which you can put anywhere in the project, and that's not how you want to design your business logic in an OOP project.

In a well-designed OOP project, classes form a coherent whole by participating in inheritance hierarchies. They may reference, extend, or redefine properties of their parents or associated objects to express the behaviours and traits of the domain concepts they represent.

And there is another problem with using Observables in such a manner. Once you build an Rx pipeline, you can't retrieve the value flowing inside unless you write still more boilerplate code to subscribe to the stream and store its value to an outside variable.

This practice is usually discouraged as an anti-pattern, and so is the use of Subjects. However, the object is not observing some outside data (e.g. Tweets) but owns it, which is one of the few cases where the use of a Subject can be justified. In such a context, it's perfectly reasonable to assume that an object always has access to a snapshot of all the states it owns.

So, what if we can define such state data of an object as Observables which can also behave like ordinary properties? Wouldn't it be nice if we can easily access them as in OOP while still being able to observe and compose them like in Rx?

And that is what this project is trying to achieve.

## Usage

To achieve the goal outlined in the previous section, we provide a way to define a property which can also turn into an Observable. And there are two different types of such property classes you can use for that purpose.

### Reactive Property

Firstly, there is a type of property that can manage its state, which is implemented by ReactiveProperty[T] class. To define such a property using an initial value, you can use a helper function from_value as follows:

from alleycat.reactive import RP
from alleycat.reactive import functions as rv

class Toto:

# Create an instance of ReactiveProperty[int] with an initial value of '99':
value: RP[int] = rv.from_value(99) # You know the song, don't you?

Note that RP[T] is just a convenient alias for ReactiveProperty[T] which you can use for type hinting. As with other type annotations in Python, it is not strictly necessary. But it can be particularly useful when you want to lazily initialize the property.

You can declare an 'empty' property using new_property and initialize it later as shown below. Because it may be difficult to see what type of data the property expects without an initial value, using an explicit type annotation can make the code more readable:

from alleycat.reactive import RP
from alleycat.reactive import functions as rv

class MyClass:

# Declare an empty property first.
value: RP[int] = rv.new_property()

def __init__(self, init_value: int):

# Then assign a value as you would do to an ordinary property.
self.value = init_value

A ReactiveProperty can be can be read and modified like an ordinary class attribute. And also you can make it a read-only property by setting the read_only argument to True like the following example:

from alleycat.reactive import RP
from alleycat.reactive import functions as rv

writeable: RP[str] = rv.from_value("life is strange")

print(place.writeable) # "life is strange"

place.writeable = "It's awesome"

print(place.writeable) # "It's awesome"

place.read_only = "Let me rewind." # Throws an AttributeError

# Of course, you can't change the past. But the game is hella cool!

But haven't we talked about Rx? Of course, we have! And that's the whole point of the library, after all.

To convert a reactive property into an Observable of the same type, you can use observe method like this:

from alleycat.reactive import RP
from alleycat.reactive import functions as rv

class Nena:

ballons: RP[int] = rv.from_value(98)

nena = Nena()

luftballons = []

rv.observe(nena.ballons).subscribe(luftballons.append) # Returns a Disposable. See Rx API.

print(luftballons) # Returns [98].

nena.ballons = nena.ballons + 1

print(luftballons) # [98, 99] # Ok, I lied before. It's about Nena. not Toto :P

If you are familiar with Rx, you may notice the similarities between ReactiveProperty with BehaviorSubject. In fact, the former is a wrapper around the latter, and observe returns an Observable instance backed by such a subject.

To learn about all the exciting things we can do with an Observable, you may want to read the official documentation of Rx. We will introduce a few examples later, but before that, we better learn about the other variant of the reactive value first.

### Reactive View

ReactiveView[T](or RV[T] for short) is another derivative of ReactiveValue, from which ReactiveProperty is also derived (hence, the alias of functions module used above, "rv").

The main difference is that while the latter owns a state value itself, a reactive view reflects it from an outside source specified as an Observable. To create a reactive view from an instance of Observable, you can use from_observable function like this:

import rx
from alleycat.reactive import RV
from alleycat.reactive import functions as rv

class Joni:

big: RV[str] = rv.from_observable(rx.of("Yellow", "Taxi"))

If you are familiar with Rx, you may see it as a wrapper around an Observable, while a reactive property can be seen as one around a Subject.

Like its counterpart, you can initialize a reactive view either eagerly or lazily. In order to create a lazy-initializing view, you can use new_view, and later provide an Observable as shown below:

import rx
from alleycat.reactive import RV
from alleycat.reactive import functions as rv

class BothSides:

love: RV[str] = rv.new_view()

def __init__(self):
self.love = rx.of("Moons", "Junes", "Ferris wheels")

It also accepts read_only option from its constructor (default to True, in contrast to the case with ReactiveProperty) setting of which will make it 'writeable'.

It may sound unintuitive since a 'view' usually implies immutability. However, what changes when you set a value of a reactive view is the source Observable that the view monitors, not the data itself, as is the case with a reactive property.

Lastly, you can convert a reactive property into a view by calling its as_view method. It's a convenient shortcut to call observe to obtain an Observable of a reactive property so that it can be used to initialize an associated view.

The code below shows how you can derive a view from an existing reactive property:

from alleycat.reactive import RP, RV
from alleycat.reactive import functions as rv

class Example:

value: RP[str] = rv.from_value("Boring!") # I know. But it's not easy to make it interesting, alright?

view: RV[str] = value.as_view()

### Operators

As we know how to create reactive properties and values, now it's time to learn how to transform them. Both variants of ReactiveValue provides map method, with which you can map an arbitrary function or lambda expression over each value in the pipeline:

from alleycat.reactive import RP, RV
from alleycat.reactive import functions as rv

class Counter:

word: RP[str] = rv.new_property()

count: RV[str] = word.as_view().map(len).map(lambda o, c: f"The word has {c} letter(s)")
# The first argument 'o' is the current instance of Counter class.

counter = Counter()

counter.word = "Supercalifragilisticexpialidocious!"

print(counter.count) # Prints "The word has 35 letter(s)". Wait, did you actually count that?

You can also use pipe to chain arbitrary Rx operators to build a more complex pipeline like this:

from rx import operators as ops
from alleycat.reactive import RP, RV
from alleycat.reactive import functions as rv

class CrowsCounter:

animal: RP[str] = rv.new_property()

# The first argument 'o' is the current instance of Counter class.
crows: RV[str] = animal.as_view().pipe(lambda o: (
ops.map(str.lower),
ops.filter(lambda v: v == "crow"),
ops.map(lambda _: 1),
ops.scan(lambda v1, v2: v1 + v2, 0)))

counting = CrowsCounter()

counting.animal = "cat"
counting.animal = "Crow"
counting.animal = "CROW"
counting.animal = "dog"

print(counting.crows) # Returns 2.

There are also convenient counterparts to merge, combine_latest, and zip from Rx API, which you can use to combine two or more reactive values in a more concise manner:

from alleycat.reactive import RP, RV
from alleycat.reactive import functions as rv

class Rectangle:

width: RP[int] = rv.from_value(100)

height: RP[int] = rv.from_value(200)

area: RV[int] = rv.combine_latest(width, height)(lambda v: v[0] * v[1])

rectangle = Rectangle()

print(rectangle.area) # Prints 20,000... you do the math!

rectangle.width = 150

print(rectangle.area) # Prints 30,000.

rectangle.height = 50

print(rectangle.area) # Prints 750.

## Install

The library can be installed using pip as follows:

pip install alleycat-reactive

This project is provided under the terms of MIT License.

## Project details

Uploaded source
Uploaded py3


text:
# Math Help - sorry but i need quick help alagebra and equations

1. ## sorry but i need quick help alagebra and equations

you fill an empty hot tub that is 54 inches tall. you started putting water in at 11 am and check to find 2 inches of water in the tub. you check again at 1 pm to find 5 inches total of water.
assume the water will fill at the same rate.
1. how many inches of water will be in the hot tub at 5 pm?

EDIT:you have 100 tickets, you are selling them at a rate of 10 per week. each is 50 cents.
1. write an equation to detemine the total value of the reamining ticketss after w weeks.
-----------------------------------------------------------------------

solve for x:

3(x+2)=5

6=-2(8x-4)

-2(-4x+7)=18

sorry but my teacher is a meanie who wont help me.

2. Originally Posted by enyaz11
you fill an empty hot tub that is 54 inches tall. you started putting water in at 11 am and check to find 2 inches of water in the tub. you check again at 1 pm to find 5 inches total of water.
assume the water will fill at the same rate.
1. how many inches of water will be in the hot tub at 5 pm?
Hi enyaz11,

I'm sorry, but where did the 2 inches of water come from? If you started filling at 11 am, at what time did you check to find the 2 inches of water? At 1 pm (2 hrs later), you measured 5 inches in the tub. So, you are either putting 3 inches or 5 inches of water in every 2 hrs. You have a span of 6 hours from 11 am to 5 pm, so go figure.

Originally Posted by enyaz11
You have 100 tickets, you are selling them at a rate of 10 per week. each is 50 cents.
1. write an equation to detemine the total value of the reamining tickets after w weeks.
$v = .50(100-10w)$

Originally Posted by enyaz11
solve for x:

3(x+2)=5

3x + 6 = 5
3x = -1
x = -1/3

6=-2(8x-4)

6 = -16x + 8
-2 = -16x
1\8 = x

x = 1/8

-2(-4x+7)=18

8x - 14 = 18
8x = 32
x = 4

Originally Posted by enyaz11
sorry but my teacher is a meanie who wont help me.


text:
# GATE ME 2005

 Question 1
Strokes theorem connects
 A a line integral and a surface integral B a surface integral and a volume integral C a line integral and a volume integral D gradient of a function and its surface integral
Engineering Mathematics   Calculus
Question 1 Explanation:
A line integral and a surface integral is related by stroke's theorem
 Question 2
A lot has 10% defective items. Ten items are chosen randomly from this lot. The probability that exactly 2 of the chosen items are defective is
 A 0.0036 B 0.1937 C 0.2234 D 0.3874
Engineering Mathematics   Probability and Statistics
Question 2 Explanation:
Probability of defective item
$P=0.1$
Probability of non-defective item
$Q=1-p=1-0.1=0.9$
Probability that exactly 2 of the chosen items are defective
$=^{10} C_{2}(P)^{2}(Q)^{8}$
$=^{10} C_{2}(0.1)^{2}(0.9)^{8}=0.1937$
 Question 3
$\int_{-a}^{a}(\sin ^{6} x + \sin ^{7} x)dx$ is equal to
 A $2\int_{0}^{a}(\sin ^{6} x)dx$ B $2\int_{0}^{a}(\sin ^{7} x)dx$ C $2\int_{0}^{a}(\sin ^{6} x + \sin ^{7} x)dx$ D zero
Engineering Mathematics   Calculus
Question 3 Explanation:
$I=\int_{-a}^{a}\left(\sin ^{6} x+\sin ^{7} x \right) dx$
$= 2 \int_{0}^{a} \sin ^{6} x d x+0$
( because $\int_{0}^{a} \sin ^{7}x\cdot d x=0$)
 Question 4
A is a 3 x 4 real matrix and Ax=b is an inconsistent system of equations. The highest possible rank of A is
 A 1 B 2 C 3 D 4
Engineering Mathematics   Linear Algebra
Question 4 Explanation:
$C =[A: B]_{3 \times 5}$
$\therefore \rho\left[C_{3 \times 5}\right] \leq \min \{3,5\}$
$\because$ The system is inconsistent
$\rho(A) \lt \rho(C)$
$\therefore \rho(A)\lt 3$
Hence maximum possible rank of
$A=2$
 Question 5
Changing the order of the integration in the double integral $I=\int_{0}^{8}\int_{\frac{x}{4}}^{2}f(x,y)dydx$ leads to $I=\int_{r}^{s}\int_{p}^{q}f(x,y)dxdy$. What is q?
 A 4y B $16y^{2}$ C x D 8
Engineering Mathematics   Calculus
Question 5 Explanation:
$\text { When } \quad I=\int_{0}^{8} \int_{x / 4}^{2} f(x \cdot y) dydx$

$I=\int_{0}^{2} \int_{0}^{4 y} f(x)dydx$
 Question 6
The time variation of the position of a particle in rectilinear motion is given by $x-2t^{3}+t^{2}+2t$. If v is the velocity and a the acceleration of the particle in consistent units, the motion started with
 A v=0, a=0 B v=0, a=2 C v=2 a=0 D v=2 a=2
Engineering Mechanics   Translation and Rotation
Question 6 Explanation:
\begin{aligned} \text{Given }\quad x&=2 t^{3}+t^{2}+2 t \\ Velocity: \quad V&=\frac{d x}{d t}=6 t^{2}+2 t+2 \\ \text{At }\quad t&=0 \quad (\text{start of motion}) \\ V &=0+0+2 \\ V &=2 \\ \text { Acceleration: } a&=\frac{d V}{d t}=12 t+2 \\ \text{At }\quad t&=0 \\ a&=0+2 \\ \therefore a&=2 \\ \end{aligned}
 Question 7
A simple pendulum of length 5m, with a bob of mass 1 kg, is in simple harmonic motion. As it passes through its mean position, the bob has a speed of 5 m/s. the net force on the bob at the mean position is
 A zero B 2.5 N C 5 N D 25 N
Engineering Mechanics   Translation and Rotation
Question 7 Explanation:
Mean position $\rightarrow$ It is the position where acceleration is zero and hence force is zero
 Question 8
A uniform, slender cylindrical rod is made of a homogeneous and isotropic material. The rod rests on a frictionless surface. The rod is heated uniformly. If the radial and longitudinal thermal stresses are represented by $\sigma _{r}$ and $\sigma _{z}$, respectively, then
 A $\sigma _{r}=0,\sigma _{z}=0$ B $\sigma _{r}\neq 0,\sigma _{z}=0$ C $\sigma _{r}= 0,\sigma _{z}\neq 0$ D $\sigma _{r}\neq 0,\sigma _{z}\neq 0$
Strength of Materials   Strain Energy and Thermal Stresses
Question 8 Explanation:
If a body is allowed to expand or contract freely
with rise or fall in temperature then no stress are
induced in body. i.e.
$\sigma_{r}=0 \text { and } \sigma_{z}=0$
 Question 9
Two identical cantilever beams are supported as shown, with their free ends in contact through a rigid roller. After the load P is applied, the free ends will have
 A equal deflections but not equal slopes B equal slopes but not equal deflections C equal slopes as well as equal deflections D neither equal slopes nor equal deflections
Strength of Materials   Bending of Beams
Question 9 Explanation:
From the figure, we can say that load P applies a force on upper cantilever and the reaction force also applied on upper cantilever by the rigid roller. Due to this, deflections are occur in both the cantilever, which are equal in amount. But because of different forces applied by the P and rigid roller, the slopes are unequal.
 Question 10
The number of degrees of freedom of a planar linkage with 8 links and 9 simple revolute joints is
 A 1 B 2 C 3 D 4
Theory of Machine   Planar Mechanisms
Question 10 Explanation:
No. of links: 1=8
No. of revolute joints,
$J=9$
No. of higher pair,
$h=0$
$\therefore$ Number of degree of freedom,
\begin{aligned} n &=3(1-1)-2 J-h \\ &=3(8-1)-2 \times 9-0\\ \therefore \quad &=3 \end{aligned}
There are 10 questions to complete.


text:
Main Content

# Train a Siamese Network for Dimensionality Reduction

This example shows how to train a Siamese network to compare handwritten digits using dimensionality reduction.

A Siamese network is a type of deep learning network that uses two or more identical subnetworks that have the same architecture and share the same parameters and weights. Siamese networks are typically used in tasks that involve finding the relationship between two comparable things. Some common applications for Siamese networks include facial recognition, signature verification [1], or paraphrase identification [2]. Siamese networks perform well in these tasks because their shared weights mean there are fewer parameters to learn during training and they can produce good results with a relatively small amount of training data.

Siamese networks are particularly useful in cases where there are large numbers of classes with small numbers of observations of each. In such cases, there is not enough data to train a deep convolutional neural network to classify images into these classes. Instead, the Siamese network can determine if two images are in the same class. The network does this by reducing the dimensionality of the training data and using a distance-based cost function to differentiate between the classes.

This example uses a Siamese network for dimensionality reduction of a collection of images of handwritten digits. The Siamese architecture reduces the dimensionality by mapping images with the same class to nearby points in a low-dimensional space. The reduced-feature representation is then used to extract images from the dataset that are most similar to a test image. The training data in this example are images of size 28-by-28-by-1, giving an initial feature dimensionality of 784. The Siamese network reduces the dimensionality of the input images to two features and is trained to output similar reduced features for images with the same label.

You can also use Siamese networks to identify similar images by directly comparing them. For an example, see Train a Siamese Network to Compare Images.

### Load and Preprocess Training Data

Load the training data, which consists of images of handwritten digits. The function digitTrain4DArrayData loads the digit images and their labels.

[XTrain,TTrain] = digitTrain4DArrayData;

XTrain is a 28-by-28-by-1-by-5000 array containing 5000 single-channel images, each of size 28-by-28. The values of each pixel are between 0 and 1. TTrain is a categorical vector containing the labels for each observation, which are the numbers from 0 to 9 corresponding to the value of the written digit.

Display a random selection of the images.

perm = randperm(numel(TTrain),9);
imshow(imtile(XTrain(:,:,:,perm),ThumbnailSize=[100 100]));

### Create Pairs of Similar and Dissimilar Images

To train the network, the data must be grouped into pairs of images that are either similar or dissimilar. Here, similar images are defined as having the same label, while dissimilar images have different labels. The function getSiameseBatch (defined in the Supporting Functions section of this example) creates randomized pairs of similar or dissimilar images, pairImage1 and pairImage2. The function also returns the label pairLabel, which identifies if the pair of images is similar or dissimilar to each other. Similar pairs of images have pairLabel = 1, while dissimilar pairs have pairLabel = 0.

As an example, create a small representative set of five pairs of images

batchSize = 10;
[pairImage1,pairImage2,pairLabel] = getSiameseBatch(XTrain,TTrain,batchSize);

Display the generated pairs of images.

figure
tiledlayout("flow")
for i = 1:batchSize
nexttile
imshow([pairImage1(:,:,:,i) pairImage2(:,:,:,i)]);
if pairLabel(i) == 1
s = "similar";
else
s = "dissimilar";
end
title(s)
end

In this example, a new batch of 180 paired images is created for each iteration of the training loop. This ensures that the network is trained on a large number of random pairs of images with approximately equal proportions of similar and dissimilar pairs.

### Define Network Architecture

The Siamese network architecture is illustrated in the following diagram.

In this example, the two identical subnetworks are defined as a series of fully connected layers with ReLU layers. Create a network that accepts 28-by-28-by-1 images and outputs the two feature vectors used for the reduced feature representation. The network reduces the dimensionality of the input images to two, a value that is easier to plot and visualize than the initial dimensionality of 784.

For the first two fully connected layers, specify an output size of 1024 and use the He weight initializer.

For the final fully connected layer, specify an output size of two and use the He weights initializer.

layers = [
imageInputLayer([28 28],Normalization="none")
fullyConnectedLayer(1024,WeightsInitializer="he")
reluLayer
fullyConnectedLayer(1024,WeightsInitializer="he")
reluLayer
fullyConnectedLayer(2,WeightsInitializer="he")];

To train the network with a custom training loop and enable automatic differentiation, convert the layer array to a dlnetwork object.

net = dlnetwork(layers);

### Define Model Loss Function

Create the function modelLoss (defined in the Supporting Functions section of this example). The modelLoss function takes the Siamese dlnetwork object net and a mini-batch of input data X1 and X2 with their labels pairLabels. The function returns the loss values and the gradients of the loss with respect to the learnable parameters of the network.

The objective of the Siamese network is to output a feature vector for each image such that the feature vectors are similar for similar images, and notably different for dissimilar images. In this way, the network can discriminate between the two inputs.

Find the contrastive loss between the outputs from the last fully connected layer, the feature vectors features1 and features1 from pairImage1 and pairImage2, respectively. The contrastive loss for a pair is given by [3]

$\mathrm{loss}=\frac{1}{2}y{\mathit{d}}^{2}+\frac{1}{2}\left(1-y\right)\mathrm{max}{\left(margin-d,0\right)}^{2},$

where $\mathit{y}$ is the value of the pair label ($\mathit{y}=1$ for similar images;$\mathit{y}=0$ for dissimilar images), and $\mathit{d}$ is the Euclidean distance between two features vectors $\mathit{f1}$ and $\mathit{f2}$: $\mathit{d}={‖\mathit{f1}-\mathit{f2}‖}_{2}$.

The $margin$ parameter is used for constraint: if two images in a pair are dissimilar, then their distance should be at least $margin$, or a loss will be incurred.

The contrastive loss has two terms, but only one is ever non-zero for a given image pair. In the case of similar images, the first term can be non-zero and is minimized by reducing the distance between the image features $\mathit{f1}$ and $\mathit{f2}$. In the case of dissimilar images, the second term can be non-zero, and is minimized by increasing the distance between the image features, to at least a distance of $margin$. The smaller the value of $margin$, the less constraining it is over how close a dissimilar pair can be before a loss is incurred.

### Specify Training Options

Specify the value of $margin$ to use during training.

margin = 0.3;

Specify the options to use during training. Train for 3000 iterations.

numIterations = 3000;
miniBatchSize = 180;

Specify the options for Adam optimization:

• Set the learning rate to 0.0001.

• Initialize the trailing average gradient and trailing average gradient-square decay rates with [].

• Set the gradient decay factor to 0.9 and the squared gradient decay factor to 0.99.

learningRate = 1e-4;
trailingAvg = [];
trailingAvgSq = [];
gradDecay = 0.9;
gradDecaySq = 0.99;

Initialize the plot parameters for the training loss progress plot.

figure
C = colororder;
lineLossTrain = animatedline(Color=C(2,:));
ylim([0 inf])
xlabel("Iteration")
ylabel("Loss")
grid on

### Train Model

Train the model using a custom training loop. Loop over the training data and update the network parameters at each iteration.

For each iteration:

• Extract a batch of image pairs and labels using the getSiameseBatch function defined in the section Create Batches of Image Pairs.

• Convert the image data to dlarray objects with underlying type single and specify the dimension labels "SSCB" (spatial, spatial, channel, batch).

• Train on a GPU, if one is available. Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, see GPU Support by Release (Parallel Computing Toolbox).

• Evaluate the model loss and gradients using dlfeval and the modelLoss function.

• Update the network parameters using the adamupdate function.

start = tic;

% Loop over mini-batches.
for iteration = 1:numIterations

% Extract mini-batch of image pairs and pair labels
[X1,X2,pairLabels] = getSiameseBatch(XTrain,TTrain,miniBatchSize);

% Convert mini-batch of data to dlarray. Specify the dimension labels
% "SSCB" (spatial, spatial, channel, batch) for image data
X1 = dlarray(single(X1),"SSCB");
X2 = dlarray(single(X2),"SSCB");

% If training on a GPU, then convert data to gpuArray.
if canUseGPU
X1 = gpuArray(X1);
X2 = gpuArray(X2);
end

% Evaluate the model loss and gradients using dlfeval and the modelLoss
% function listed at the end of the example.
[loss,gradients] = dlfeval(@modelLoss,net,X1,X2,pairLabels,margin);

% Update the Siamese network parameters.
[net.Learnables,trailingAvg,trailingAvgSq] = ...
adamupdate(net.Learnables,gradients, ...
trailingAvg,trailingAvgSq,iteration,learningRate,gradDecay,gradDecaySq);

% Update the training loss progress plot.
D = duration(0,0,toc(start),Format="hh:mm:ss");
loss = double(loss);
addpoints(lineLossTrain,iteration,loss)
title("Elapsed: " + string(D))

drawnow
end

### Visualize Image Similarities

To evaluate how well the network is doing at dimensionality reduction, compute and plot the reduced features of a set of test data. Load the test data, which consists of images of handwritten digits similar to the training data. Convert the test data to dlarray and specify the dimension labels "SSCB" (spatial, spatial, channel, batch). If you are using a GPU, convert the test data to gpuArray.

[XTest,TTest] = digitTest4DArrayData;
XTest = dlarray(single(XTest),"SSCB");

if canUseGPU
XTest = gpuArray(XTest);
end

Compute the reduced features of the test data.

FTest = predict(net,XTest);

For each group, plot the first two reduced features of the test data.

uniqueGroups = unique(TTest);
colors = hsv(length(uniqueGroups));

figure
hold on
for k = 1:length(uniqueGroups)
ind = TTest==uniqueGroups(k);

plot(FTest(1,ind),FTest(2,ind),".",Color=colors(k,:));
end
hold off

xlabel("Feature 1")
ylabel("Feature 2")
title("2-D Feature Representation of Digits Images.");

legend(uniqueGroups,Location="eastoutside");

### Use the Trained Network to Find Similar Images

You can use the trained network to find a selection of images that are similar to each other out of a group. Extract a single test image from the test data and display it.

testIdx = randi(5000);
testImg = XTest(:,:,:,testIdx);

trialImgDisp = extractdata(testImg);

figure
imshow(trialImgDisp,InitialMagnification=500);

Create a group of images containing the test data but does not include the extracted test image.

groupX = XTest;
groupX(:,:,:,testIdx) = [];

Find the reduced features of the test image using predict.

trialF = predict(net,testImg);

Find the 2-D reduced feature representation of each of the images in the group using the trained network.

FGroupX = predict(net,groupX);

Use the reduced feature representation to find the nine images in the group that are closest to the test image, using the Euclidean distance metric. Display the images.

distances = vecnorm(extractdata(trialF - FGroupX));
[~,idx] = sort(distances);
sortedImages = groupX(:,:,:,idx);
sortedImages = extractdata(sortedImages);

figure
imshow(imtile(sortedImages(:,:,:,1:9)),InitialMagnification=500);

By reducing the images to a lower dimensionality, the network is able to identify images that are similar to the trial image. The reduced feature representation allows the network to discriminate between images that are similar and dissimilar. Siamese networks are often used in the context of facial or signature recognition. For example, you can train a Siamese network to accept an image of a face as an input, and return a set of the most similar faces from a database.

### Supporting Functions

#### Model Loss Function

The function modelLoss takes the Siamese dlnetwork object net, a pair of mini-batch input data X1 and X2, and the label pairLabels. The function returns the contrastive loss between the reduced dimensionality features of the paired images and the gradients of the loss with respect to the learnable parameters in the network. Within this example, the function modelLoss is introduced in the section Define Model Loss Function.

function [loss,gradients] = modelLoss(net,X1,X2,pairLabel,margin)
% The modelLoss function calculates the contrastive loss between the
% paired images and returns the loss and the gradients of the loss with
% respect to the network learnable parameters

% Pass first half of image pairs forward through the network
F1 = forward(net,X1);
% Pass second set of image pairs forward through the network
F2 = forward(net,X2);

% Calculate contrastive loss
loss = contrastiveLoss(F1,F2,pairLabel,margin);

% Calculate gradients of the loss with respect to the network learnable
% parameters
gradients = dlgradient(loss,net.Learnables);

end

function loss = contrastiveLoss(F1,F2,pairLabel,margin)
% The contrastiveLoss function calculates the contrastive loss between
% the reduced features of the paired images

% Define small value to prevent taking square root of 0
delta = 1e-6;

% Find Euclidean distance metric
distances = sqrt(sum((F1 - F2).^2,1) + delta);

% label(i) = 1 if features1(:,i) and features2(:,i) are features
% for similar images, and 0 otherwise
lossSimilar = pairLabel.*(distances.^2);

lossDissimilar = (1 - pairLabel).*(max(margin - distances, 0).^2);

loss = 0.5*sum(lossSimilar + lossDissimilar,"all");

end

#### Create Batches of Image Pairs

The following functions create randomized pairs of images that are similar or dissimilar, based on their labels. Within this example, the function getSiameseBatch is introduced in the section Create Pairs of Similar and Dissimilar Images.

function [X1,X2,pairLabels] = getSiameseBatch(X,Y,miniBatchSize)
% getSiameseBatch returns a randomly selected batch of paired images.
% On average, this function produces a balanced set of similar and
% dissimilar pairs.
pairLabels = zeros(1, miniBatchSize);
imgSize = size(X(:,:,:,1));
X1 = zeros([imgSize 1 miniBatchSize]);
X2 = zeros([imgSize 1 miniBatchSize]);

for i = 1:miniBatchSize
choice = rand(1);
if choice < 0.5
[pairIdx1, pairIdx2, pairLabels(i)] = getSimilarPair(Y);
else
[pairIdx1, pairIdx2, pairLabels(i)] = getDissimilarPair(Y);
end
X1(:,:,:,i) = X(:,:,:,pairIdx1);
X2(:,:,:,i) = X(:,:,:,pairIdx2);
end

end

function [pairIdx1,pairIdx2,pairLabel] = getSimilarPair(classLabel)
% getSimilarPair returns a random pair of indices for images
% that are in the same class and the similar pair label = 1.

% Find all unique classes.
classes = unique(classLabel);

% Choose a class randomly which will be used to get a similar pair.
classChoice = randi(numel(classes));

% Find the indices of all the observations from the chosen class.
idxs = find(classLabel==classes(classChoice));

% Randomly choose two different images from the chosen class.
pairIdxChoice = randperm(numel(idxs),2);
pairIdx1 = idxs(pairIdxChoice(1));
pairIdx2 = idxs(pairIdxChoice(2));
pairLabel = 1;
end

function  [pairIdx1,pairIdx2,pairLabel] = getDissimilarPair(classLabel)
% getDissimilarPair returns a random pair of indices for images
% that are in different classes and the dissimilar pair label = 0.

% Find all unique classes.
classes = unique(classLabel);

% Choose two different classes randomly which will be used to get a dissimilar pair.
classesChoice = randperm(numel(classes), 2);

% Find the indices of all the observations from the first and second classes.
idxs1 = find(classLabel==classes(classesChoice(1)));
idxs2 = find(classLabel==classes(classesChoice(2)));

% Randomly choose one image from each class.
pairIdx1Choice = randi(numel(idxs1));
pairIdx2Choice = randi(numel(idxs2));
pairIdx1 = idxs1(pairIdx1Choice);
pairIdx2 = idxs2(pairIdx2Choice);
pairLabel = 0;
end

### References

1. Bromley, J., I. Guyon, Y. LeCun, E. Säckinger, and R. Shah. "Signature Verification using a "Siamese" Time Delay Neural Network." In Proceedings of the 6th International Conference on Neural Information Processing Systems (NIPS 1993), 1994, pp737-744. Available at Signature Verification using a "Siamese" Time Delay Neural Network on the NIPS Proceedings website.

2. Wenpeg, Y., and H Schütze. "Convolutional Neural Network for Paraphrase Identification." In Proceedings of 2015 Conference of the North American Cahapter of the ACL, 2015, pp901-911. Available at Convolutional Neural Network for Paraphrase Identification on the ACL Anthology website.

3. Hadsell, R., S. Chopra, and Y. LeCun. "Dimensionality Reduction by Learning an Invariant Mapping." In Proceedings of the 2006 IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR 2006), 2006, pp1735-1742.


text:
# Stochastic population model in R, Rcpp, Racket, and Typed Racket

On my journey to learn Racket, I look for small pieces of R code to try to implement in Racket. A blog post about speeding up population simulations in R with the Rcpp package seemed like a good candidate for implementing in Racket. I was particularly interested in the (superficial?) parallels between R/Rcpp and Racket/Typed Racket.

### Running a single simulation

First, let’s get some of the setup code1 out of the way.2 In this chunk of code, we are loading the math library and stochastic-logistic-typed.rkt, which contains the typed versions of the functions. Using require with a file is similar to loading a script with source in R. Racket is a programming language laboratory with the nice feature that it is easy to use code from different languages (#langs) in the same file.

#lang racket

(require math
"stochastic-logistic-typed.rkt")

(define YINIT 1.0)        ; initial population size
(define R 1.4)            ; maximum population growth rate
(define K 20.0)           ; carrying capacity
(define THETASD 0.1)      ; standard deviation for adding noise to population
(define T 100)            ; number of years to run simulation
(define REPS 1000)        ; number of replications
(define T2 (* T REPS))    ; used to compare difference between long-running simulation and many calls to simulation
(define TIME-SAMPLES 100) ; number of samples to run when timing functions

In the spirit of learning, I wanted to try to write the Racket version of the logmodr function with recursion. I managed to work out a clunky version (not shown here) and reached out to the Racket mailing list for help. Here is one suggested alternative:

(define (logmod t y r k thetasd)

(define (calc y)
(define theta (flvector-ref (flnormal-sample 0.0 thetasd 1) 0))
(* y (- r (* r (/ y k))) (exp theta)))

(define (loop y i)
(if (= i t)
(list y)
(cons y (loop (calc y) (add1 i)))))
(loop y 1))

My clunky version had calc and loop as separate functions and I struggled to figure out how to pass the extra function arguments (i.e., r, k, and thetasd) to the standalone loop function. Several solutions were suggested on the mailing list. I gravitated to the nested functions approach shown above,3 but, as I write this, I’m having trouble articulating why I liked that option the best. Check out the thread on the mailing list and decide for yourself.

I also wrote a version, logmod-vec, that is a direct translation of the R code in the logmodr function. This version was easy to write because I had already invested time in learning about for loops in Racket..

(define (logmod-vec t y r k thetasd)
(define y-vec (make-vector t))
(vector-set! y-vec 0 y)
(define theta-vec (flnormal-sample 0.0 thetasd t))
(for ([i (in-range 1 t)])
(define last-y (vector-ref y-vec (sub1 i)))
(define theta (flvector-ref theta-vec i))
(vector-set! y-vec i (* last-y (- r (* r (/ last-y k))) (exp theta)))))

We can create a typed version of logmod simply by adding type annotations. The rest of the code is the same as in the untyped version. The syntax for the type annotations is straightforward. The type of each argument is specified, followed by ->, and then the type of the output. All of the types and their relationships are thoroughly described in the documentation. Moreover, the type-checker was helpful for pointing me to the places where I was misspecifying types.

#lang typed/racket

(require math)

(provide logmod-typed repeat-logmod-typed)

(: logmod-typed : Integer Flonum Flonum Flonum Flonum -> (Listof Flonum))
(define (logmod-typed t y r k thetasd)

(: calc : Flonum -> Flonum)
(define (calc y)
(define theta (flvector-ref (flnormal-sample 0.0 thetasd 1) 0))
(* y (- r (* r (/ y k))) (exp theta)))

(: loop : Flonum Integer -> (Listof Flonum))
(define (loop y i)
(if (= i t)
(list y)
(cons y (loop (calc y) (add1 i)))))
(loop y 1))

We need to make a quick digression to talk about the code4 that I wrote to get sample timings for the different functions. time-apply-cpu is a simple wrapper function to time-apply that runs time-apply repeatedly and prints the min, mean, and max cpu time to the interactions pane. time-apply produces multiple output values that are not contained in a data structure. define-values allows you to capture those outputs and bind them to names (in this case, results, cpu-time, real-time, gc-time). string-append is similar to paste in R, but string-append requires that all arguments are strings and, thus, requires some conversion (e.g., number->string).5

(define (time-apply-cpu proc lst reps)
(define out
(for/list ([i (in-range reps)])
(define-values (results cpu-time real-time gc-time) (time-apply proc lst))
cpu-time))
(displayln (string-append "min: " (number->string (apply min out))
" mean: " (number->string (round (mean out)))
" max: " (number->string (apply max out))
"    function: "
(symbol->string (object-name proc)))))

We will now use time-apply-cpu to compare our 3 functions.

> (time-apply-cpu logmod (list T2 YINIT R K THETASD) TIME-SAMPLES)
min: 84 mean: 148 max: 1385    function: logmod
> (time-apply-cpu logmod-typed (list T2 YINIT R K THETASD) TIME-SAMPLES)
min: 66 mean: 101 max: 828    function: logmod-typed
> (time-apply-cpu logmod-vec (list T2 YINIT R K THETASD) TIME-SAMPLES)
min: 25 mean: 28 max: 59    function: logmod-vec

I want to be cautious not to overinterpret these results because I know just enough to be dangerous. I am intrigued by the good performance of logmod-vec because, generally, I am most comfortable programming in an imperative style. The relatively modest speedup provided by Typed Racket presumably is not representative of the performance bumps that you can get in other situations. For example…

Performance Warning: Matrix values are arrays, as exported by math/array. The same performance warning applies: operations are currently 25-50 times slower in untyped Racket than in Typed Racket, due to the overhead of checking higher-order contracts. We are working on it. Source

I will also cautiously interpret the timings of the R code. Although the Racket and R timing results are all in milliseconds, it seems unlikely that this is an apples-to-apples comparison. Nonetheless, in this simple example, R performance is arguably comparable to Racket.

Unit: milliseconds
expr       min        lq      mean    median        uq      max neval
logmodc(1e+05, yinit, r, k, thetasd)  5.874801  6.568564  6.702172  6.622926  6.732324 10.74266   100
logmodr(1e+05, yinit, r, k, thetasd) 24.786605 25.487129 25.914484 25.654279 26.014754 31.76419   100

One clear result is that the 19x speedup of logmodc over logmodr reported in the original blog post was not reproduced here. The original blog post is only 2 years old but R releases in those two years have targeted performance improvements.

### Running multiple simulations

I took a slightly different approach to running multiple simulations than in the original blog post. Instead of running multiple simulations where one parameter is varied, I repeated the simulation many times with the same paramters. In Racket, I used for/list to loop through the number of replications.

(define (repeat-logmod reps t y r k thetasd)
(for/list ([i (in-range reps)]) (logmod t y r k thetasd)))

(define (repeat-logmod-vec reps t y r k thetasd)
(for/list ([i (in-range reps)]) (logmod-vec t y r k thetasd)))

The timings for the Racket code showed similar results to the single simulation results. Good performance of repeat-logmod-vec and a modest speedup of repeat-logmod-typed over repeat-logmod.

> (time-apply-cpu repeat-logmod (list REPS T YINIT R K THETASD) TIME-SAMPLES)
min: 54 mean: 77 max: 1437    function: repeat-logmod
> (time-apply-cpu repeat-logmod-typed (list REPS T YINIT R K THETASD) TIME-SAMPLES)
min: 43 mean: 58 max: 795    function: repeat-logmod-typed
> (time-apply-cpu repeat-logmod-vec (list REPS T YINIT R K THETASD) TIME-SAMPLES)
min: 25 mean: 29 max: 65    function: repeat-logmod-vec

In R, I only slightly modified the purrr::map examples from the original blog post. Again, results for multiple simulations are similar to single simulation results. R performance is comparable to Racket and logmodc speedup over logmodr is not as large as reported in the original blog post.

> reps <- 1:1000
> mb3 <- microbenchmark(
+   purrr::map(reps, ~logmodc(t, yinit, r, k, thetasd)),
+   purrr::map(reps, ~logmodr(t, yinit, r, k, thetasd))
+ )
> mb3
Unit: milliseconds
expr       min        lq     mean    median       uq      max neval
purrr::map(reps, ~logmodc(t, yinit, r, k, thetasd))  8.863207  9.245297 11.48015 10.048379 11.14099 44.91200   100
purrr::map(reps, ~logmodr(t, yinit, r, k, thetasd)) 28.869978 29.357927 32.00268 30.180079 33.39805 48.81793   100

Update 2019-04-21: All of the previous timings were run in DrRacket. Running from the command line yields much better performance (2-3x).

\$ racket stochastic-logistic.rkt
min: 34 mean: 41 max: 218    function: logmod
min: 27 mean: 33 max: 195    function: logmod-typed
min: 14 mean: 15 max: 16    function: logmod-vec
min: 32 mean: 40 max: 213    function: repeat-logmod
min: 26 mean: 27 max: 28    function: repeat-logmod-typed
min: 14 mean: 15 max: 16    function: repeat-logmod-vec

1. All of the Racket code is a available in this gist.

2. See the original blog post for the R code.

3. Throughout this post, we are using flnormal-sample to draw random numbers. I wrote about generating random numbers in this post.

4. See this post for an improved version.

5. The racket/format library provides functions for formatting strings with more compact code.


text:
# What are the differences between artificial neural networks and other function approximators?

Modern artificial neural networks use a lot more functions than just the classic sigmoid, to the point I'm having a hard time really seeing what classifies something as a "neural network" over other function approximators (such as Fourier series, Bernstein polynomials, Chebyshev polynomials or splines).

So, what makes something an artificial neural network? Is there a subset of theorems that apply only to neural networks?

Backpropagation is classic, but that is the multi-variable chain rule, what else is unique to neural networks over other function approximators?

There are other technical differences. For example, the Taylor expansion of a function is typically done only at a single value of the domain, it assumes that the function to be approximated is differentiable multiple times, and it makes uses of the derivatives of such a function. Fourier series typically approximate functions with a weighted sum of sinusoids. Given appropriate weights, the Fourier series can be used to approximate an arbitrary function in a certain interval or the entire function (if the function you want to approximate is also periodic). On the other hand, neural networks attempt to approximate functions of the form $$f: [0, 1]^n \rightarrow \mathbb{R}$$ (at least, this is the setup in the famous paper that proved the universality of neural networks) in many different ways (for example, weighted sums followed by sigmoids).


text:
# diophantus

Hello, this is beta version of diophantus. If you want to report about a mistake, please, write to hello@diophantus.org

#### Unstable and Stable Galaxy Models

09 Apr 2007 astro-ph, gr-qc, math.AP arxiv.org/abs/0704.1012
Abstract. To determine the stability and instability of a given steady galaxy configuration is one of the fundamental problems in the Vlasov theory for galaxy dynamics. In this article, we study the stability of isotropic spherical symmetric galaxy models $f_{0}(E)$, for which the distribution function $f_{0}$ depends on the particle energy $E$ only. In the first part of the article, we derive the first sufficient criterion for linear instability of $f_{0}(E):$ $f_{0}(E)$ is linearly unstable if the second-order operator $A_{0}\equiv-\Delta+4\pi\int f_{0}^{\prime}(E)\{I-\mathcal{P}\}dv$ has a negative direction, where $\mathcal{P}$ is the projection onto the function space $\{g(E,L)\},$ $L$ being the angular momentum [see the explicit formula (\ref{A0-radial})]. In the second part of the article, we prove that for the important King model, the corresponding $A_{0}$ is positive definite. Such a positivity leads to the nonlinear stability of the King model under all spherically symmetric perturbations.

# Reviews

There are no reviews yet.


text:
# Reduced Temperature

Written by Jerry Ratzlaff. Posted in Fluid Mechanics

Reduced temperature of a fluid is the actual temperature.

## formula

$$T_r = \frac {T}{T_c}$$

Where:

$$T_r$$ = reduced temperature

$$T$$ = temperature

$$T_c$$ = critical temperature


text:
SMS:WAM Grid Options

The Grid Options dialog is accessed by right-clicking on the grid entry in the Project Explorer and selecting Options. The Grid Options dialog contains the following three tabs:

General Tab

WAM Grid Options dialog showing the General

The General tab allows setting parameters for the WAM model run.

Model options

• Water depth model – Choosing a shallow depth model specifies the use of the full dispersion equation. Dispersion equation is ${\displaystyle w^{2}=gk\tanh(kh)}$ where w is radial frequency, g is acceleration due to gravity, k is the wave number, and h is the water depth. Computes all phase and group celerity where there is depth dependence.
• Refraction model – If running in deep water, specify "Not used." It is not recommend to use if grid resolution is greater than 3-min (0.05 deg) on a spherical grid.
• Breaking – It is recommended that breaking be used when the water depth model is "Shallow." This will implement limited breaking within the WAM model.
• Test level – A control for output diagnostics and is mostly used for model debugging. The WAM model includes the capability to simulate refraction of waves caused by interaction with the ocean bottom and interaction with currents. WAM can also simulate wave breaking and create a restart file to continue analysis at a future time. The default value is "0.0", which is the minimal diagnostic output level.
• Create restart file – Causes WAM to create a restart file to continue analysis at a future time.

Model time steps

• Propagation – The Propagation time step is controlled by the Courant–Friedrichs–Lewy (CFL) condition. The CFL condition requires that the numerical solution cannot move faster than the group speed of the waves. It is recommended that the other model time steps and output time steps be multiples of the propagation time step.
• Max CFL – The CFL criteria must be less than 1.0 for WAM to run successfully. The estimated CFL value is shown below the time step. SMS computes the maximum CFL value based on the propagation time step, cell size, refraction model, and the water depth model. The value shows in green if the settings meet the criteria, or in red if they do not. Currently, the CFL estimation in SMS does not account for currents, but this is used within WAM. Therefore, the CFL estimate may show green in SMS but fail when running WAM. If this occurs, further reduce the propagation time step or change some of the other options before rerunning WAM. All of the parameters are interconnected.
• Source – This is the time step for the source term integration time step. Increasing this value can greatly decrease run times. However, it should be used with caution. It is recommended that the source term time step be no greater than 10 times the propagation time step. WAM interpolates between these intervals to attain a smaller forcing time step.
• Output wind – This time step controls how frequently the WAM simulation updates the forcing terms. WAM interpolates between these intervals to attain a smaller forcing time step.

Output Time Steps

• Spatial Datasets – This is the frequency of output that will be generated that can be loaded into SMS as datasets. The WAM file it creates is the "integrated parameter map file."
• Spectra – This is the frequency of output for spectra data at defined observation cells.
• Close/Reopen files – When writing files on a computer, often data being written is stored temporarily in RAM before being written to disk. If a file is in this state and WAM crashes, the final data may not be complete. Closing and reopening the output files forces the file system to complete writing the data to file.

Output Tab

WAM Grid Options dialog showing the Output tab

The Output tab consists of different Model options. Selecting each option will determine the dataset generated in the solution. Options include:

 Wind speed at 10m Wind direction Friction velocity Drag coefficient Significant wave height Wave peak period Wave mean period Wave TM1 period Wave TM2 period Wave direction Directional spread Normalized wave stress Sea significant wave height Sea peak period Sea mean period Sea TM1 period Sea TM2 period Sea direction Swell significant wave height Swell peak period Swell mean period Swell TM1 period Swell TM2 period Swell direction Swell directional spread Spectra of total sea Sea spectra Swell spectra

Sea waves and swell waves are defined as:

• Sea waves: waves generated by the wind blowing at the time, and in the recent past, in the area of observation.
• Swell waves: waves which have traveled into the area of observation after having been generated by previous winds in other areas. These waves may travel thousands of kilometers from their origin before dying away. There may be swell present even if the wind is calm and there are no 'sea' waves.

Spatial Inputs Tab

WAM Grid Options dialog showing the Spatial Inputs tab

WAM uses spatial input for ice cover, currents, and wind. The Spatial Inputs section of the WAM Model Control is used to manage these datasets.

Ice Cover

The ice cover dataset is an editable scalar dataset.

• "Dataset" – After creating the dataset using the Editable Datasets dialog accessed from the Model Control, the dataset will be displayed in the project explorer in a folder named Editable Datasets. Only one ice cover dataset may exist at a time.
• "File" – Rather than using a dataset, specify the file name for ice.

Currents

The currents dataset is specified to be any vector dataset on the current grid.

• "Dataset" – The dataset and time step must both be selected. The selection may be removed by clicking on the Clear button. This deselects the dataset as the specified currents dataset, but it does not remove the dataset from the SMS project.
• "File" – Rather than using a dataset, specify the file name for currents.

Wind

The winds dataset can be set to any vector dataset that belongs to the current grid or another grid whose angle is zero and whose extents cover the current grid.

• "Dataset" – Nested grids may use the wind dataset of the parent grid. The time steps in the dataset used must match those used with the WAM simulation.
• "File" – PBL can be used to create an input wind file for WAM by selecting "WAM input" in the PBL Model Control. The fort.12 file created by PBL may be selected as the wind data. Note: This option is only useful if there is access to the "Planetary Boundary Layer" model which isn't available through SMS.


text:
Paper Reading - Latent Aspect Rating Analysis on Review Text Data

kuniga.me > NP-Incompleteness > Paper Reading - Latent Aspect Rating Analysis on Review Text Data

# Paper Reading - Latent Aspect Rating Analysis on Review Text Data

09 Oct 2020

In [1] Wang, Lu and Zhai present a supervised machine learning model to solve a problem they name Latent Aspect Rating Analysis. To explain the problem, let’s start with some context. Sites like Amazon, TripAdvisor or Audible have a review system where the user provides a rating and a review text.

The overall rating might be too vague, so it’s desirable to break down this rating per what is called aspect. The list of aspects depend on the use case: for logding, it could be room, cleaningless or location; for Audible it has performance and story. The model tries to infer these implicit (latent) ratings from only the overall rating and the review text.

The authors define a model that learns from training data and show promising results with test data. In this post we’ll focus on the theory presented in [1].

## Overview of the solution

To solve the Latent Aspect Rating Analysis problem, first the authors build a correlation matrix between words and aspects by scanning all the texts from the reviews in the training dataset. This matrix is then used in tandem with both the overall and aspect ratings in the training dataset to construct a model which is then used to infer the aspect ratings for the test dataset. The inferred values are then compared to the actual values to assess the quality of the model.

## Definitions

### Aspect and associated keywords

We’ll have a fixed set of $k$ aspects (e.g. for hotels it could be location, cleaningness, price). We then let $A_i$ for $i \in [1, …, k]$ be the set of words associated with an aspect $i$. Note that these sets are not mutually exclusive, so for example “view” could be related to aspect “room” or “location”.

### Word-aspect correlation

For each document $d \in D$, we have $W_d$ which is a $k \times n$ where $W_{dij}$ is the frequency of word $w_j$ for aspect $i$ normalized by the total number of the words in the text in that aspect, that is:

$\sum_{j=1}^n W_{ijd} = 1$ for all documents $d$ and aspects $i$.

### Aspect ratings and weights

The aspect rating for a document $d$, denoted by $s_d$, is a $k$-dimensional vector where $s_{di} \in [r_{min}, r_{max}]$ is the rating for corresponding to aspect $i$. For example, a user might rate a hotel’s location as 4 (out of 5), but its cleaningness as 1.

The aspect weight for a document $d$, denoted by $\alpha_d$ is also a $k$-dimensional vector where $\alpha_{di} \in [0, 1]$ and $\sum_{i=1}^k \alpha_{di} = 1$ and represents the weight the review $d$ puts on aspect $i$. For example, a user might put more importance of location than price.

These are the two quantities the model will try to predict once it’s properly trainted. Note that for a document $d$:

$(1) \quad r_d = \sum_{i=1}^{k} \alpha_{di} s_{di}$

## The Model

### Word-aspect correlation

We’ll gloss over the methodology for building the matrix $W_d$ and present it only at a high level. The idea is to seed each aspect with initial keywords. Then use a clustering algorithm to include more words into each of the aspects based on the reviews texts. Refer to [1] for more details.

### Aspect ratings

The model defines a parameter $\beta$ representing the overall rating associated with a (word-aspect) pair. This can be thought of as the average rating a user would to aspect $i$ solely based on word $j$, so $\beta_{ij} \in [r_{min}, r_{max}]$.

Since $\sum_{j=1}^n W_{ijd} = 1$, the model assumes that for document $d$ and aspect $i$, $s_{di}$ is a linear combination of $\beta_i$ and $W_{di}$:

$(2) \quad s_{di} = \sum_{j=1}^n \beta_{ij} W_{dij}$

### Overal ratings

In theory $r_d$ can be obtained by (1), but to encode uncertainty and allow variance, the model assumes $r_d$ is drawn from a Gaussian distribution, with mean (1) and some variance $\delta$ which will be another parameter in the model.

$r_{d} \sim N(\sum_{i=1}^{k} \alpha_{di} s_{di}, \delta^2)$

Exapanding $s_{di}$ using (2):

$(3) \quad r_{d} \sim N(\sum_{i=1}^{k} \alpha_{di} \sum_{j=1}^{n} \beta_{ij} W_{dij}, \delta^2)$

### Correlated aspect weights

The authors argue that aspect weights are not completely independent (e.g. a preference for cleaninliness is correlated with a preference for room, maybe less so for price).

To model this, we assume $\alpha_{d}$ is drawn from a multi-variate Gaussian distribution:

$\alpha_{d} \sim N(\mu, \Sigma)$

where $\mu$ is a $k$ dimensional vector and $\Sigma$ a $k \times k$ variance matrix encoding the correlation between pairs of aspects.

### Objective function

Let’s bundle the parameters needed to compute $r_d$ into $\Theta = (\mu, \Sigma, delta^2, \beta)$. We then define the likelihood of observing $D$ (i.e. the overall and aspect ratings) as

$p(D \mid \Theta)$

we need to find $\hat \Theta = \mbox{argmax}_\Theta \, p(D \mid \Theta)$.

## Solving the model

We’ll use an interactive algorithm. We start with an initial estimate $\Theta_0$ for $\Theta$.

Because $\alpha_d$ is also considered a random variable, we can estimate it from $\Theta_t$. We then compute $\Theta_{t+1}$ assuming $\alpha_d$ is constant. We keep iterating until $\Theta_t$ converges.

### Estimating aspect weights

Given a rating $r_{d}$, how can we estimate $\alpha_{d}$, that is $p(\alpha_d \mid r_d)$? In particular what is the most probable value of $\alpha_{d}$? We can use the Maximum a Posteriori Estimate (MAP) [2] method to find:

$\hat \alpha_{d} = \mbox{argmax}_{\alpha_d} p(\alpha_d \mid r_d)$

Which we show in Appendix A,

$(4) \quad \hat \alpha_{d} = \mbox{argmax}_{\alpha_d} \bigg[ - \frac{(r_d - \alpha_d^T s_d)^2}{2 \delta^2} -\frac{1}{2} (\alpha_d - \mu)^T \Sigma^{-1} (\alpha_d - \mu) \bigg]$

We can use a non-linear optimization such as L-BFGS to solve this, using the gradient of (4):

$- \frac{(r_d - \alpha_d^T s_d) s_d}{\delta^2} - \Sigma^{-1} (\alpha_d - \mu)$

### Estimating parameters $\mu$ and $\Sigma$

To estimate the $\Theta$ we’ll use Maximum Likelihood Estimate (MLE). Note that since there’s no constraint limiting each of $\mu$, $\Sigma$, $\beta$ and $\delta$, we can estimate them individually.

First we estimate $\mu$ and $\Sigma$ to maximize the likelihood of observing $\alpha$, $p(\alpha_1, \cdots, \alpha_D \mid \mu, \Sigma)$.

We show in Appendix A that if

$\mu_{MLE} = \mbox{argmax}_{\mu} p(\alpha_1, \cdots, \alpha_D \mid \mu, \Sigma)$

then

$\mu_{MLE} = \frac{1}{\mid D \mid} \sum_{d \in D} \alpha_d$

It can be shown [4] that if

$\Sigma_{MLE} = \mbox{argmax}_{\mu} p(\alpha_1, \cdots, \alpha_D \mid \mu, \Sigma)$

then

$\Sigma_{MLE} = \frac{1}{\mid D \mid} \sum_{d \in D} (\alpha_d - \mu_{MLE})(\alpha_d - \mu_{MLE})^{T}$

Summarizing, from a iteractive algorithm perspective, we’re given $\alpha$ computed previously and we can estimate the next iteration $\mu_{t+1}$ and $\Sigma_{t+1}$:

$\mu_{t+1} = \frac{1}{\mid D \mid} \sum_{d = 1}^{\mid D \mid} \alpha_d$ $\Sigma_{t+1} = \frac{1}{\mid D \mid} \sum_{d \in D} (\alpha_d - \mu_{t+1})(\alpha_d - \mu_{t+1})^{T}$

### Estimating parameters $\beta$ and $\delta^2$

Now we estimate $\beta$ and $\delta^2$ to maximize the likelihood of observing $r$, $p(r_1, \cdots, r_D \mid \beta, \delta^2)$.

We show in Appendix A that if:

$\delta_{MLE}^2 = \mbox{argmax}_{\delta^2} p(r_1, \cdots, r_D \mid \beta, \delta^2)$

then

$\delta_{MLE}^2 = \frac{r_d - \alpha_d^T s_d}{\mid D \mid}$

For $\beta$ we need to expand $\alpha_d^T s_d$ since it’s a function of $\beta$, that is $\alpha_d^T s_d = \sum_{i = 1}^{k} \alpha_{di} \beta_i^T W_{di}$. This give us:

$\beta_{MLE} = \mbox{argmax}_{\beta} \sum_{d \in D} \log(\frac{1}{\delta \sqrt{2 \pi}}) - \frac{1}{2} (\frac{r_d - \sum_{i = 1}^{k} \alpha_{di} \beta_i^T W_{di}}{\delta})^2$

Discarding the terms independent of $\beta$:

$\beta_{MLE} = \mbox{argmax}_{\beta} - \sum_{d \in D} (r_d - \sum_{i = 1}^{k} \alpha_{di} \beta_i^T W_{di})^2$

The authors claim the closed form of this optimization requires inverting a $n \times n$, which is costly. The proposal is to also used a non-linear optimization such as L-BFGS like we did for $\hat \alpha_{d}$. The $j$-th component of the gradient of the log-likelihood function above is:

$\frac{\partial \mathcal{L}({\beta})}{\partial \beta_j} = \sum_{d \in D} \big( (r_d - \sum_{i = 1}^{k} \alpha_{di} \beta_i^T W_{di}) \alpha_{dj} W_{dj} \big)$

Summarizing, from a iteractive algorithm perspective, we’re given $\alpha$ computed previously and we can estimate the next iteration $\delta^2_{t+1}$ and $\beta{t+1}$:

$\delta^2_{t+1} = \frac{r_d - \alpha_d^T s_d}{\mid D \mid}$

and

$\beta_{t+1} = \mbox{LBFGS}(\beta_{t}, r, \alpha, s, W)$

## Conclusion

In this post we studied the theory of the paper Latent Aspect Rating Analysis on Review Text Data. I also planned to work on a Python implementation but it will have to wait. It’s the first machine learning paper I can remember reading and it was interesting to see how much calculus is involved. I had past experience with linear (discrete and continuous) optimization, which involves mostly basic linear algebra.

In studying this post I had to watch many basic machine leaning math, via the mathematicakmonk channel. I also realized how rusty I am in regards to basic derivatives operations, so I’ll need a refresher.

## References

• [1] Latent Aspect Rating Analysis on Review Text Data: A Rating Regression Approach
• [2] (ML 6.1) Maximum a posteriori (MAP) estimation - Youtube
• [3] (ML 4.1) Maximum Likelihood Estimation (MLE) - Youtube
• [4] Chapter 13 - The Multivariate Gaussian - Michael I. Jordan

## Appendix A

### Finding $\hat \alpha_{d}$

Here we derive $\hat \alpha_{d}$ as a function of $\Theta$.

$\hat \alpha_{d} = \mbox{argmax}_{\alpha_d} p(\alpha_d \mid r_d)$

Using Bayes’ Rule, we have

$p(\alpha_d \mid r_d) = \frac{p(r_d \mid \alpha_d) p(\alpha_d)}{p(r_d)}$

Since $\log$ is a monotomic function and $p(r_d)$ does’t depend on $\alpha_{d}$, finding $\alpha_{d}$ that maximizes the above is equal to maximizing

$\hat \alpha_{r_d} = \mbox{argmax}_{\alpha_d} \log p(r_d \mid \alpha_d) + \log p(\alpha_d)$

Let’s consider $p(r_d \mid \alpha_d)$ first. We know it’s the Gaussian with mean $\alpha_d^T s_d$ and variance $\delta^2$ (3), so by the definition of a Gaussian,

$p(r_d \mid \alpha_d) = \frac{1}{\delta \sqrt{2 \pi}} \exp(-\frac{1}{2} (\frac{r_d - \alpha_d^T s_d}{\delta})^2)$

Applying log we get,

$(5) \quad \log p(r_d \mid \alpha_d) = \log(\frac{1}{\delta \sqrt{2 \pi}}) - \frac{1}{2} (\frac{r_d - \alpha_d^T s_d}{\delta})^2$

The first term is independent of alpha, so we’re left with

$\frac{(r_d - \alpha_d^T s_d)^2}{2 \delta^2}$

Let’s now look at $p(\alpha_d)$. It’a multi-variate Gaussian, so by definition:

$(G) \quad p(\alpha_d) = \frac{\exp(-\frac{1}{2} (\alpha_d - \mu)^T \Sigma^{-1} (\alpha_d - \mu))}{\sqrt{(2 \pi)^{k} \mid \Sigma \mid}}$

Where $k$ is the dimension of $\alpha_d$, $\mu$ and $\Sigma$ ($k \times k$) and $\mid \Sigma \mid$ is the determinant of $\Sigma$. If we apply $\log$ and discard terms unrelated to $\alpha_d$ we get

$-\frac{1}{2} (\alpha_d - \mu)^T \Sigma^{-1} (\alpha_d - \mu)$

Putting everything together, we want to find

$(4) \quad \hat \alpha_{d} = \mbox{argmax}_{\alpha_d} \bigg[ - \frac{(r_d - \alpha_d^T s_d)^2}{2 \delta^2} -\frac{1}{2} (\alpha_d - \mu)^T \Sigma^{-1} (\alpha_d - \mu) \bigg]$

### Finding $\mu_{MLE}$

We have that

$\mu_{MLE} = \mbox{argmax}_{\mu} p(\alpha_1, \cdots, \alpha_D \mid \mu, \Sigma) = \prod_{d \in D} p(\alpha_d \mid \mu, \Sigma)$.

Recalling $\alpha_d$ is sampled from a multi-variate Gaussian. Sing $\log$ is a monotonically increasing function, we can take the $\log$ on the right side of the equation:

$\quad \mu_{MLE} = \mbox{argmax}_{\mu} -\sum_{d \in D} (\alpha_d - \mu)^T \Sigma^{-1} (\alpha_d - \mu)$

We can take the derivative of with respect to $\mu$ to obtain the gradient:

$\sum_{d \in D} (\alpha_d - \mu) \Sigma^{-1}$

and set the gradient to 0 for which $\mu$ is optimal, and multiply both sides by $\Sigma$ to obtain:

$0 = \sum_{d \in D} (\alpha_d - \mu)$

which implies

$\sum_{d \in D} \mu = D \mu = \sum_{d \in D} \alpha_d$

so

$\mu_{MLE} = \frac{1}{\mid D \mid} \sum_{d \in D} \alpha_d$

### Finding $\delta_{MLE}$

We have that

$\delta_{MLE}^2 = \mbox{argmax}_{\delta^2} p(r_1, \cdots, r_D \mid \beta, \delta^2) = \prod_{d \in D} p(r_d \mid \beta, \delta^2)$

Since $\log$ is a monotomic function, we have,

$\delta_{MLE}^2 = \mbox{argmax}_{\delta^2} \sum_{d \in D} \log p(r_d \mid \beta, \delta^2)$

Recalling $r_d$ is sampled from a univariate Gaussian (3) with some a mean that is a function of $\beta$ (say $f(\beta)$) and variance $\delta^2$ (3), so by the definition of a Gaussian,

$p(r_d \mid \beta, \delta^2) = \frac{1}{\delta \sqrt{2 \pi}} \exp(-\frac{1}{2} (\frac{r_d - f(\beta)}{\delta})^2)$

Applying log,

$\log p(r_d \mid \beta, \delta^2) = \log(\frac{1}{\delta \sqrt{2 \pi}}) - \frac{1}{2} (\frac{r_d - f(\beta)}{\delta})^2$

Putting it back in the original sum:

$\delta_{MLE}^2 = \mbox{argmax}_{\delta^2} \mid D \mid \log(\frac{1}{\delta \sqrt{2 \pi}}) - \frac{1}{2\delta^2} \sum_{d \in D} (r_d - f(\beta))^2$

If we set $y = x^2$, $a = \mid D \mid$, $b = \sqrt(2\pi)$ and $c = \sum_{d \in D} (r_d - f(\beta))^2$ we can put it in a form that is easier to derive:

$a \log(\frac{1}{\sqrt(y) b}) - \frac{c}{2y}$

If we derive on $y$,

$- \frac{a}{2y} + \frac{c}{2y^2}$

Setting to 0, we can solve for $y$ to get:

$y = \frac{c}{a}$

Replacing the terms back:

$\delta_{MLE}^2 = \frac{\sum_{d \in D} (r_d - f(\beta))^2}{\mid D \mid}$


text:
Definition:Pythagorean Triple

Definition

A Pythagorean triple is an ordered triple of positive integers $\tuple {x, y, z}$ such that $x^2 + y^2 = z^2$.

That is, a Pythagorean triple is a solution to the Pythagorean equation.

Primitive Pythagorean Triple

Let $\tuple {x, y, z}$ be a Pythagorean triple such that $x \perp y$ (that is, $x$ and $y$ are coprime).

Then $\tuple {x, y, z}$ is a primitive Pythagorean triple.

Also known as

A Pythagorean triple is also known as a (set of) Pythagorean numbers.

Also see

• Results about Pythagorean triples can be found here.

Source of Name

This entry was named for Pythagoras of Samos.


text: in my Calc 3 CS-oriented class we had a lot of matrix programming homeworks and I would absolutely be able to help OP here! The code is a little long but I'm still a beginner to Java. A quick Java solution for a simple math problem of finding the distance between two points. Everything you want to know about Java. More than just an online equation solver. Below we share the three simple ways to find the volume of a sphere. Check it out. To understand this example, you should have the knowledge of the following Java programming topics: A Quadratic Equation is of the form ax2+bx+c=0 where x2 is x raised to the power 2.Here ,a,b,and c are the respective coefficients of the Quadratic expression. This Java Program To Compute Roots of Quadratic Equation makes use of If – Else Block. Source code fully available. Step 2: – To add a Jframe, right click the project name in the Project window. Java-Linear-Equations-Calculator. $$\frac{dS}{dt} + \frac{dI}{dt} + \frac{dR}{dt} = 0$$ Because this system is normalized, one acquires a specific forecast by multiplying its results by the population size. HandyCalc is an Android OS app that’s designed to be a symbolic calculates system to handle some complicated mathematical features including the capability to solve equation and equation set, define variables, define your own functions, plot graph. By using this website, you agree to our Cookie Policy. Quadratic formula Java You can select the whole java code by clicking the select option and can use it. Make sure you are in the client console and from there enter the equation in the format-“operand1 operator operand2” and press Enter. Uncheck the Main class option from the project tab. Download a Java IDE (short for integrated development environment) such as Netbeans or Eclipse. Original Version 1996-97. Make Calculator. When you click text, the code will be changed to text format. In numerical analysis, fixed-point iteration is a method of computing fixed points of iterated functions. Java … I'm looking to write an app for the sake of writing an app. No special classes or libraries are used with this application. To make a simple calculator in Java Programming which performs the basic four mathematical operations i.e., addition, subtraction, multiplication, and division, use the switch case to identify the input operator to perform required calculation and then display the result on the screen as shown in the following program. Java Calculator Step 1: – Create a blank Java project and name it Java Calculator. The standard form of the quadratic equation is ax² + bx + c = 0 where a, b and c are real and a !=0, x is an unknown variable. The initial parameters include: gravity (-9.81), falling time (10 seconds), as well as initial position and initial velocity of the object (both set to 0 by default). + + 12=0 Write the equation. To run this program, add all the class files and the run.bat file to a folder on your computer. The initial condition is y0=f(x0), and the root x is calculated within the range of from x0 to xn. HandyCalc. Learn Java by Examples: How to solve Simple Quadratic Equation in Java ?.Learn Java by examples. About Modulo Calculator . Create Java gravity calculator, able to determine the object’s final position after falling through the Earth’s atmosphere through the predetermined time. In this Program we are making a simple calculator that performs addition, subtraction, multiplication and division based on the user input. More specifically, given a function defined on real numbers with real values, and given a point in the domain of , the fixed point iteration is. Using the range of BMI, individuals are classified as underweight, normal or overweight. Since the tip calculator is a relatively simple application, you only need to download Java SE (standard edition). tl;dr: Looking for a java library to solve equations. My calculator is built with 5 methods: Method for getting the input from the user called getInput (with String argument called prompt); Method for addition (+) called addition (with 2 String arguments called st1 and st2); Method for subtraction (-) called subtraction (with 2 String arguments st1 and st2); Method for multyplyng (*) called multyplyng (with 2 String arguments st1 and st2) Java Program to Make a Simple Calculator Using switch...case. It is a measure of body mass based on height and weight of an individual. Square both sides of the equation. This version was the first GUI in Swing. \(\normalsize \\ There may be so many ways to represent the code. Step 3: – To the right of the Jframe, there is a Palette with a set of controls on it. In the future I'm going to do some improvements on it, but here it is right now. The Java Math class has many methods that allows you to perform mathematical tasks on numbers. What is a Quadratic Equation? For school (GT Yellowjackets!) I used Complete the square root :D. The example below illustrates how this formula applies to the quadratic equation x2 - 2x - 8. Quadratic Equation Formula Tutorials, Source Codes, SCJP, SCWCD and Ebooks. This calculator would be able to add, subtract, multiply and divide two numbers. One can choose whichever operation one wants to perform. This java program code will be opened in a new pop up window once you click pop-up from the right corner. Answer to the requested equation will be shown in the client console only. In this program, you'll learn to make a simple calculator using switch..case in Java. The following calculator will convert angles between degrees and radians. A rewrite of the older Java applet which was lost many computers ago, only the functions were saved thanks to the web. Math.max(x,y) The Math.max(x,y) method can be used to find the highest value of … This java programming code is used to find the discriminant quadratic equation. Modulo. The Modulo Calculator is used to perform the modulo operation on numbers. Finding the volume of Sphere is quite simple, if you knew the formula, you can do it. \$ java Mortgage Enter Principal: 200000 Enter Yearly Interest Rate: 5.25 Enter Term (years): 30 Payment: 1104.41 Questions answered by this page: Java example for a simple mortgage calculator. Java program to calculate the volume of a sphere. Wolfram|Alpha is a great tool for finding polynomial roots and solving systems of equations. I figured that, as a firmware guy with minimal EE hardware training, a good practice app would be one that acts as a helper for EE projects. Java Program to find Roots of a Quadratic Equation using Else If This Java program allows users to enter three values for a, b, and c. Next, this Java program returns roots of a quadratic equation using Else If Statement . This calculator requires a Java J2ME MIDP 2.0 and CLDC 1.1 compatible mobile phone. It also factors polynomials, … Logic We use a switch case in order to choose a operation.Then we just put simple math formulae for addition, […] It is a term used in Elementary Algebra. BMI stands for Body Mass Index. Calculator online number systems, algebra worksheet inequalities, how to simplify and evaluate. A quadratic equation is an equation of the second degree, meaning it contains at least one term that is squared. You can also create math equations using on the keyboard using a combination of keywords and math autocorrect codes. This is the simplest form of one of the classic problems given to first year Java students. Write a Java program without a graphical user interface that calculates and displays the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Printable algebra pages for kids, math-multipication for 2nd graders, saving notes and formulas to graphic calculator, graphing linear equation ti-89, quadratic inequalities gcse, infinitely solution java calculator. As opposed to the minimum of two numbers, you may be interested in the higher of both. It is overloaded in four versions to handle natural and decimal numbers. A Java console application that calculates a certain type of linear equation based on two points from a line, or can convert one type of linear equation to another. To help you find the maximum of two numbers, you can call the max() method of the Math class from the java.lang package. This program performs addition ,subtraction ,multiplication and division operations on two numbers. Running the Program. Menu driven JAVA program for calculator This is a menu driven JAVA program for calculator. Finally, from the above equation we can calculate the distance between the points: distance = sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)) Now let's move on to the implementation part. 10. Cubic Equation Calculator Quartic Equation Calculator. By Chaitanya Singh | Filed Under: Java Examples. The Standard Form of a Quadratic Equation is ax 2 + bx + c = 0, where a, b, c are constant values which cannot be changed and x is a variable entity. The nature of roots is determined by the discriminant. Suggestions welcome. Calculates the solution y=f(x) of the ordinary differential equation y'=F(x,y) using Euler's method. Lost Compile and Run Matrix Calculator. Let's examine the choice f(x) = 4x 2 – 4x+1. I am trying to make a small tool that transfers a math equation to a Java BigDecimal line. This program finds the solutions of a Quadratic Equation by using Sri Dharacharya Formula in JAVA. Java Program to Make a Calculator using Switch Case. To download Netbeans, go to the Netbeans.org website and press the big orange button on the top right of the page that says Download. Calculator Enter the Equation … Online calculator which allows you to separate the variable to one side of the algebra equation and everything else to the other side,for solving the equation easily. Graph of the equation y = 1/x. Free second order differential equations calculator - solve ordinary second order differential equations step-by-step This website uses cookies to ensure you get the best experience. 3. Today, I made a calculator in JFrame. Basic Math Functions. Use the square root property of equality. This is the meat of the calculator: You can put the equations into a 2-dimensional array and then use basic Java Matrix libraries, such as JAMA, or even hand-code some matrix logic to solve those equations. Finally to terminate the communication, type “bye” (without quotes) and hit enter.


text:
# Difference between revisions of "Entropy"

Entropy is a quantitative measure of the "disorder" in a system. It forms the basis of the second law of thermodynamics, that entropy tends to increase. In other words, the tendency of everything to trend toward greater disorder, in the absence of intelligent intervention.

The second law of thermodynamics states that entropy tends not to decrease over time within an isolated system, defining an isolated system as one in which neither matter nor energy may enter or leave.[1]

Entropy is undeniable and yet creates perhaps insurmountable difficulties for many modern theories of physics. For example, it renders time asymmetric, resulting in an arrow of time that is impossible to reconcile with the theory of relativity. Increasing entropy renders the theory of evolution implausible, because that theory claims that order is increasing. Liberal denial is thus common in ignoring the significance of the increase in disorder.

The entropy of a system only depends on the state the system is currently in. The change in entropy therefore only depends on the initial and final states of that system, and not the path taken by the system to reach that final state.

## Definitions

### Thermodynamic definition

In classical thermodynamics, if a small amount of energy  is supplied to a system from a reservoir held at temperature , the small change in entropy, , is given by

For a measurable change between two states i and f this expression integrates to

where  and  represent the initial and final states of the system.

#### Example of Thermodynamic Entropy

Consider a cup of coffee, of mass  and since it is mostly water, specific heat capacity . We shall assume that both  and  are constant. If we leave the coffee for a while, it shall cool from  to . The change in entropy is, from above:

We can relate the change in heat, , to the change in temperature,  by . Then we can write:

Plugging our numbers we find that the change in entropy is . The change in entropy is negative. This does not disagree with the second law of thermodynamics, as it our cup of coffee is an open system, not an isolated system.

### Statistical mechanics definition 1 (Boltzmann Entropy)

If a system can be arranged in W different ways, the entropy is

where  is Boltzmann's constant.[2]

#### Example of Boltzmann Entropy

This example is based on that found in University Physics with Modern Physics. [2] To see an example of the statistical nature of this law, consider flipping four coins, the outcome of which can be either heads (H) or tails (T). The entropy can be calculated using the above.

ID Combinations Number of Combinations Entropy
1 HHHH 1
2 THHH

HTHH
HHTH
HHHT

4
3 TTHH

THTH
THHT
HTTH
HTHT
HHTT

6
4 TTTH

TTHT
THTT
HTTT

4
5 TTTT 1

Intuitively, the macro-state with the highest entropy (or "disorder") is the third state as it corresponds the macro-state which has the greatest number of micro-states. In the same way that when you throw four coins, you expect to get two heads and two tails, it is not impossible to get four heads. It is only less likely.

### Statistical mechanics definition 2 (Gibbs Entropy)

Label the different states a thermodynamic system can be in by . If the probability of finding the system in state i is , then the entropy is

where  is the Bolzmann constant. This definition is closely related to ideas in information theory, where the definition of information content is very similar to the definition of entropy.

### Entropy in information Theory (Shannon Entropy)

For a discrete random variable, entropy is defined as

For a continuous random variable, the analogous description for entropy, which in this case represents the number of bits necessary to quantize a signal to a desired accuracy, is given by

### Entropy in Quantum Information Theory (Von Neumann Entropy)

In entangled systems, a useful quantity is the Von Neumann Entropy, defined (for a system with density matrix ) by

where Tr() indicates taking the Trace of a matrix (the sum of the diagonal elements). This is a useful measure of entanglement, which is zero for a pure state, and maximal for a fully mixed state.


text:
# How to prove that antipodal points on the Bloch sphere are orthogonal?

I started by assuming two antipodal states $$|(\theta,\psi)\rangle = \cos\dfrac{\theta}{2}|0\rangle + \sin\dfrac{\theta}{2}e^{i\psi}|1\rangle\\ |(\theta+\pi,\psi+\pi)\rangle= \cos\dfrac{\theta+\pi}{2}|0\rangle + \sin\dfrac{\theta+\pi}{2}e^{i(\psi+\pi)}|1\rangle$$ and then try to take the inner product of them. However, the math doesn't check out (as in the result I get $$\ne0$$). What is wrong with my deduction?

In spherical coordinates antipodal point to $$(\theta,\psi)$$ is $$(\theta+\pi,\psi)$$, not $$(\theta+\pi,\psi+\pi)$$
In the most widespread convention, the Bloch sphere uses $$\theta = 0$$ radians latitude to indicate the north pole $$|0\rangle$$, $$\theta = \pi$$ to refer to the south pole $$|1\rangle$$ and $$\theta = \pi/2$$ to refer to the equator, which includes the superpositions $$(1+i)/\sqrt 2$$ and $$(1-i)/\sqrt 2$$ as well as $$i|1\rangle$$ and $$-i|1\rangle$$.
If two great circles are distinct and intersect at a given point they will intersect exactly one other time at the opposite point on the sphere and such that a ray passing through both points will also pass through the center of the sphere. Given this, the difference in latitude between one point and the equator will be of equal magnitude but opposite sign relative to the other, with the equator being at $$\pi/2$$. As such, the latitude of the antipode to $$(\theta,\psi)$$ will be $$\pi-\theta$$. As there are $$2\pi$$ radians of longitude to a sphere, the opposite of a given longitude with be $$\pi+\psi$$.
This being the case, coordinates for the antipode $$(\theta,\psi)$$ is $$(\pi-\theta,\pi+\psi)$$. Trigonometric identities will be sufficient for the rest.


text:
# Re: [NTG-context] parsing issue with \math{} inside natural table

```On Mon, 12 Jul 2010, Brian R. Landy wrote:

```
Thanks for the reply. I'm actually generating TeX from an application that interprets \$ in it's own code and occasionally this causes me trouble in passing the \$ through to TeX to use in math mode (I think they have a bug and am hoping I can get that fixed). So I tend to use \math{} to avoid complications with escaping \$, but was stuck in this case because I couldn't use \$ or \math{}.
```
With expansion problems, sometimes using \noexpand\math{...} works.


text:
# Maximum likelihood covariance estimation of Gaussian

I was reading these notes on matrix calculus http://research.microsoft.com/en-us/um/people/minka/papers/matrix/minka-matrix.pdf

and I could not figure out how to go from equation (30) to (31). Any kind of help is appreciated.

Edit: The oI notation is defined in equation (21).

-
You might also want to look at this: en.wikipedia.org/wiki/Estimation_of_covariance_matrices –  Michael Hardy Oct 22 '11 at 23:43
This is my summary on MLE of mean and covariance, shiyuzhao.files.wordpress.com/2011/10/mle-mean-variance.pdf. –  Shiyu Oct 23 '11 at 2:12
I am mostly interested in this ml estimate derivation as an application example of the constrained differential rule. Equation (30) constrains the differential to be symmetric, but it's not clear how the author moves to (31). –  Freddie Oct 24 '11 at 8:09