pr.probability – How to prove the coupling version of the Donsker’s Invariance Principle?

Donsker’s invariance principle:
Let $$X_1,X_2,…$$ be i.i.d. real-valued random variables with mean 0 and variance 1. We define $$S_0=0$$ and $$S_n= X_1+ … + X_n$$ for $$n geq 1$$. To get a process in continuous time, we interpolate linearly and define for all $$t geq 0$$
$$S_t = S_{(t)}+ (t-(t))(S_{(t)+1}- S_{(t)}).$$
Then we define for all $$t in (0,1)$$
$$S^*_n(t)= frac{S_{nt}}{sqrt{n}}.$$
Let $$C(0,1)$$ be the space of real-valued continuous function defined on $$(0,1)$$ and endow space with the supremumnorm. Then $$(S^*_n(t))_{0 leq t leq 1}$$ can be seen as a random variable taking values in $$C(0,1)$$. Now let $$mu_n$$ be its law on that space of continuous functions and let $$mu$$ be the law of Brownian motion on $$C(0,1)$$. Then the following holds:

Theorem (Donsker): The probability measure $$mu_n$$ converges weakly to $$mu$$, i.e. for every $$F: C((0,1)) rightarrow mathbb{R}$$ bounded and continuous,
$$int F dmu_n rightarrow int F dmu$$
as $$n rightarrow infty$$.

But for a two-dimensional case, the ‘coupling version’ is as following.

$$textbf{‘coupling version’}$$: Fix a square $$S$$ of size $$s$$. Fix $$xin nS$$. Let $$X$$ be a random walk starting from $$x$$ until it exits the square $$S$$ and let $$B$$ be a Brownian motion until it exits $$S$$. For $$forall epsilon>0$$, then there exists $$N>0$$ such that $$nge N$$, one can couple $$X$$ and $$B$$ so that
$$d(X,B)le epsilon n.$$

My questions:
(1) Can we extended the classical Donsker’s to the two-dimensional case?

(2) Is there any reference for the proof of the ‘coupling version’?

java – Difference between Efferent Coupling and Dependency Injection

I am working on research that analyzes dependency injection (DI) in Java projects. The more I read, the more I get confused by DI in relation to other frameworks and even software quality metrics.

I have been recently reading about afferent couplings (Ca), efferent couplings (Ce), and instability (I) with the formula below proposed by Martin Fowler:

I = (Ce/(Ce + Ca))

I noticed that the definition of efferent couplings is that it counts the number of classes the current class depends on. Is that essentially the same as dependency injection definition-wise, or are there more nuances to what is considered a dependency that has the DI framework as opposed to simply a class that the current class depends on?

pr.probability – consequence of “the best coupling” of two SDEs with different diffusion matrices

My question comes form a potion of the long review paper, which is attached below

In the set-up, $$sigma_1$$ and $$sigma_2$$ are possibly different, constant diffusion matrices. To my knowledge, if we take the (“cheap”) synchronous coupling $$B^1_t = B^2_t$$, then we can estimate $$mathbb{E}(|X^1_t – X^2_t|^2)$$ using for instance Ito isometry or BDG inequality. However, what will be the error estimate $$mathbb{E}(|X^1_t – X^2_t|^2)$$ under this new (“the best”) coupling? The reference paper FH16 published on The Annals of Probability is overwhelming long and it is also too technical for me. Thanks for any help!

javascript – Is My Controller too Coupling?

To put it in context, I had the assignment to do for a job interview in Node.JS, which basically making CRUD with products (and it have certain conditions).
The assignment was well-understandable, and I have succeeded to make this task.
After the company reviewed my code, I’ve got rejected because one of the main reasons (along with unit test not done, code not properly commented etc… ) was

• “Huge coupling of the controller” and Model is in ProductService.

I am actually confused with this sentence because I have used the Services and Repository Pattern, and separate all of the concerns, as follows (Folder structure):

• Controllers Responsible for controlling the flow of the application execution
• Models Database ORM object
• Repositories handling table data via models
• Services responsible for business logic

Product Router (import to my index router)

const router = require('koa-router')();
const ProductController = require('../../app/controllers/product.controller');
const AuthMiddleware = require('../../middleware/authValidation')

// router.get('location', ErpApiController.getLocation)

router.get('/', ProductController.searchAllProducts);                                             // GET /api/v1/products/
router.get('code/:code', ProductController.searchProductByCode);                                  // GET /api/v1/products/code/:code
router.get('name/:name', ProductController.searchProductByName);                                  // GET /api/v1/products/name/:name
router.get('brand/:brand', ProductController.searchProductByBrand);                               // GET /api/v1/products/brand/:brand
router.get('category/:category', ProductController.searchProductByCategory);                      // GET /api/v1/products/brand/:brand

Here is my Product Controller

const productService = require("../services/product.service");

exports.searchAllProducts = async (ctx) => {
const result = await productService.getAllProducts();
ctx.body = result;
}

exports.searchProductByCode = async (ctx) => {
const result = await productService.getAllProductByCode(ctx.params.code)
ctx.body = result;
}

exports.searchProductByName = async (ctx) => {
const result = await productService.getAllProductByName(ctx.params.name)
ctx.body = result;
}
....

Product Repository

const models = require("../models");
const Sequelize = require("sequelize");

exports.fetchAllProduct = async () => {
return await models.Products.findAll({
include: (
// {
//   model: models.Brands,
//   as: "brand",
//   attributes: { exclude: ('id', 'description', 'category_ID', 'createdAt', 'updatedAt') },
// },
{
model: models.ProductSizes,
as: "sizes",
attributes: { exclude: ('id', 'description', 'product_code', 'createdAt', 'updatedAt') },
},
{
model: models.Categories,
as: "category",
attributes:  ('name'),
through: {
attributes: (),
}
},
),
});
}

exports.fetchProductByCode = async (code) => {
return models.Products.findOne({
where: {
code: code,
},
include: (
{
model: models.ProductSizes,
as: "sizes",
attributes: { exclude: ('description', 'product_code', 'createdAt', 'updatedAt') },
},
{
model: models.Categories,
as: "category",
attributes:  ('name'),
through: {
attributes: (),
}
},
),
})
}

Product Service file

const dateFormat = require("../../utils/dateUtil");
const productRepo = require("../repositories/product.repository");
const productSizeRepo = require("../repositories/productSize.repository");
const productCateRepo = require("../repositories/productCategory.repository");
const categoryRepo = require("../repositories/category.repository");
const _ = require("lodash");

exports.getAllProducts = async () => {
return await productRepo.fetchAllProduct();
}

exports.getAllProductByCode = async (ctxcode) => {
const data = await productRepo.fetchProductByCode(ctxcode)

if(!data) {
return {
code: "204",
success: true,
data: (),
};
}

const products = ();

products.push({
code: data.code,
name: data.name,
description: data.description,
img_url: data.img_url,
product_url: data.product_url,
brand_name: data.brand_name,
color: data.color,
start_period: dateFormat.dateFormat(data.start_period),
end_period: dateFormat.dateFormat(data.end_period),
isActive: data.isActive,
sizes: data.sizes,
categories: data.category
})

return {
code: "200",
success: true,
count: products.length,
message: "",
data: products,
};
}

exports.getAllProductByName = async (name) => {
const data = await productRepo.fetchProductByName(name)

if(!data) {
return {
code: "204",
success: true,
data: (),
};
}

const products = ();
for(let i =0; i < data.length; i++)
{
products.push({
code: data(i).code,
name: data(i).name,
description: data(i).description,
img_url: data(i).img_url,
product_url: data(i).product_url,
brand_name: data(i).brand_name,
color: data(i).color,
start_period: dateFormat.dateFormat(data(i).start_period),
end_period: dateFormat.dateFormat(data(i).end_period),
isActive: data(i).isActive,
sizes: data(i).sizes,
categories: data(i).category
})
}

return {
code: "200",
success: true,
data: products,
};

}

I would like to know your thought on it and how can I improve the architecture

architecture – Duplication of data vs loose coupling

Currently, I’m working with a health management application, let’s call it application A. It’s a partly prebuilt generic application that we are extending on. The main entities are journals, events, and persons(patients/medical staff/external contacts).
The user can use the application through a React app to see medical journals with the most necessary data about a person’s medical condition and historical medical-related events. It’s also possible to create new events in the medical record. Several data grids show information about the leading entities in the applications, for example, name, age, address, event data, event category, journal name/no, etc.

In the same corporation, we also have an internal API that exposes person-related data to several applications. This API is a large data register containing data for several million persons, addresses, etc. Most of them are not related to application A since they don’t have a medical journal and properly never will. This database can, on rare occasions, receive updates from national registers. This could, for example, be the address of a person that can change. Sometimes columns for all persons have also changed when data are represented in a new way. The API’s responsibility is to expose the latest data related to a person for several applications.

Currently, what has been done is that all persons are being copied to application A from the API, even if they don’t have a medical journal. Then several scheduled jobs fetch all new data every half hour which can be problematic since these can take a very long time to process, and it’s just a pain to keep them in sync. Currently, there is also some confusion with which application is the authority of the person-data.

Now we have two options to mitigate the problem we are facing. We have come up with proposals.

The first proposal is to hold only an ID of the person, and every time we need to show a page that shows the medical condition, we have to call the API to get the name because the name is present on that page. We also have to develop solutions to joining data that users can use for the data grids. Since most tables use service side filtering with graphQL, this is problematic if we need data from multiple sources. If the API goes down, then application A will not work. The good thing about this solution is that the person-related data is only stored in one place.

The second solution is only to have person-data in application A that are connected to a medical journal. The data is then checked asynchronously for updates when the user tries to access personal information on specfic pagers. If there are new data, the user will get notified, and the updates will then be transfered and stored in application A’s database. In this solution, the application can still work if the API is down for some reason. And the problems with the data grids are not a problem since all the relevant data that should be showed in a data grid are stored in application A’s database. The downside is, though, that some data are copied.

As far as I have read, duplication in, for example, a microservice architecture can be wrong, and others say it can be a tool you can use to decouple the different services, for example, here Microsoft’s microservice guide. I’m more into the second solution since I think the services are too coupled in the first solution, and the services can be too chatty, and even though it is the “same” entity, they live in different contexts. But I still have a feeling of unsureness.

Does anyone have any advice on how to deal with that? Or maybe is there another way to solve problems like this?

Anemic Domain Model VS coupling and dependencies- design conflict – need an Object oriented advice

Some coupling must exist, otherwise a program would not be able to do anything (if nothing knows about anything, no calls can be made). This kind of coupling certainly exists in your “manager” classes. The thing that’s bad is coupling that makes your code hard to change, and most of it is kind of accidental.

You start with certain best practices, and a certain way of doing things; this is OK to get you off the ground, but as the project evolves, accidental coupling creeps in, and in a big part because of inertia – people blindly following generic “best practices” and “established conventions” way beyond the point of their usefulness, without stopping to rethink/redesign before it’s all a complicated mess. Best practices, conventions, linters, style checkers – that all has it’s place, but none of it automatically gives you good code. Nobody wants to write bad code, however, bad code, in large part, gets written very systematically.

So, when it comes to bad coupling, you have this kind of coupling even within your “manager” classes, as well as in and between classes that use them – it’s just not as obvious until a change causes you to modify 20 different files. They are coupled in subtle ways – like depending on a specific data format, on a specific field being there, on calling order, on things needing to match or follow the same pattern in different places, etc.

The thing that you want to do is not to eliminate coupling completely, but to minimize it and control it, and be strategic about it.

“but then we have very strong coupling and dependency for Customer with PaperBoy”

Sure, but, putting a type name in a constructor parameter list makes that type explicitly a part of the public interface of that class. It explicitly states what depends on what. Being explicit is good, and if this coupling is deliberate, and if Customer and PaperBoy are fairly small and focused classes, and not these large procedural bags of functions, than that’s a way of controling coupling.

Note that on a most basic level, the Customer class doesn’t depend on the internal implementation of PaperBoy, just on its public interface (the set of public methods, including their parameter and return types). Behind that, you can change how PaperBoy is implemented – you can change its internal code, its internal data structures, you can pull out parts of PaperBoy into a separate class, you can do all kinds of things – without affecting Customer.

If you need more flexibility than that, maybe you can derive a subtype of PaperBoy, or perhaps make the constructor parameter type be an interface instead of a concrete class.

“and changes in paperboy will cause changes in Customer”

The most likely cause of that is that you’re treating PaperBoy as just a data structure, and your Customer is pulling data out of PaperBoy to manipulate it itself, instead of telling PaperBoy what to do (by calling a method – almost in a fire and forget style). What’s happening there is that you’re not defining/constraining the way Customer talks to PaperBoy – you’re not thinking carefully (or at all) about the public interface of PaperBoy, you’re just saying “Here’s some data, hope the format doesn’t change! (wink, wink)”. This problem is not solved by having “manager” classes – changes in the data structures they depend on will have the same effect.

“if i will need my Customer in other project”

Don’t get this the wrong way, I’m by no means trying to talk down to you or anything like that, but:

When people say “reusable”, it really means the ability to use the same code/function/object in a different context: perhaps in a test, perhaps in a different part of the same project, or as a thing you can plug your newly developed component into. Or, if the function or class or module (or whatever) turns out to be more generally useful, perhaps in a different project, or maybe in a library.

Think about what you need for something simple like a function to be reusable. You need it to have well defined inputs and outputs. These are often data, but note that some of those inputs (and even the output) may be a functions themselves (look at the IEnumerable.Select method in C#, or Array.map in JavaScript). You need the function to not depend on obscure global variables. It’s even more reusable if it has no side effects at all. It’s similar with objects: well-defined inputs and outputs; they can take other objects (or functions, for that matter) as parameters, that they can then call; they need to be protective of their internals, but also need to limit what they know about the rest of the system.

When you can’t easily reuse something in a different context, it’s not because it’s a class coupled to some other class, it’s because it wasn’t designed to be reusable.

Now, I’m not saying that the only way to deal with this is OOP; however, whatever paradigm you’re using, these same principles apply (it’s just that their expression may take a different form). Also note that I’m not advocating for (or against) purity: some aspects of different paradigms can be mixed and can complement each other nicely. So I’m not necessarily saying that your domain model must not be anemic – just that that you have to control the coupling; if it happens in functions that work on anemic data structures, then you have to control it there.

I’ve been thinking recently, the usual advice regarding going from an anemic domain model to a proper OO design is to put behavior into those data structures; however, I think is not necessarily immediately obvious how to go about this change, even to experienced designers, but they can find their way by relying on their experience and design skills. But for someone who designed themselves into an anemic domain model, the path from one point to the other might be completely obscured.

Instead, maybe a better way to approach things is to look where your behavior is now, inside your “manager” classes, and try to decouple that. That’s where your objects hide. Keep treating your domain model as inputs and outputs for the time being, but in your procedural code, try to find groups of things that are not really related to other things, and isolate them. Find groups of lines that have a comment on top explaining what they do – extract each into a meaningfully named function. Reduce conceptual duplication (code doing the same thing, even though it’s written slightly differently) – extract those into methods. Find those member variables that are used to track things internally, but are only used by some methods. That looks like a class with some state; combine those and pull out a class.

Notice when a change causes you to modify several files. Try to understand why, and see if you can restructure things so that this proliferation doesn’t happen, especially if the kind of change that caused it is a common one – if there’s a history of similar change requests, and you can expect more in the future. Maybe some of the steps above will make such restructuring easier.

Keep doing this over time, gradually arriving to a more decoupled OO system. And remember, it’s OK for a class to be small – they should be small, and focused on a specific responsibility. Small means less opportunity for coupling, but doesn’t prevent your code from doing “bigger” things – objects achieve this through collaboration. Small and focused, with a well defined public interface and the associated rules, is what’s reusable.

testing – Change architecture design to API to reduce coupling

Change breaks things. Hiding change limits how far that breaking goes and makes changing easier.

If you want to look at this in the most abstract way: this is simply indirection.

A famous aphorism of Butler Lampson goes: “All problems in computer science can be solved by another level of indirection” (the “fundamental theorem of software engineering”). This is often deliberately mis-quoted with “abstraction layer” substituted for “level of indirection”. An often cited corollary to this is, “…except for the problem of too many layers of indirection.”

wikipedia.org – Indirection

Which is simply a way to say, sure you can do this. But it aint free. It comes with a cost.

When you say:

Here is the part I don’t understand. I just don’t see an API reducing all those dependencies, for me an API just provides endpoints to be called from outside the application… How come that all that coupling is going to automatically dissapear if I change my backend to be connected within an API?

You are 100% right. Not a single dependency has been eliminated. The users have exactly the same needs that they had before.

What’s different now is the expression of those needs (what you’d look at to learn what they are) is no longer mixed together with service implementation details and scattered over many desperate services. Separating the implementation and the interface isn’t simply to make testing or polymorphism easier. It also makes reading code easier. A reviewer of your code now has a tidy place to go to learn exactly what user needs you’re offering to satisfy.

And from the user perspective there is less coupling simply because they don’t know what is satisfying their needs.

I’m just too blind to see how it would work… I’d love to understand it because I’ve been using TDD for two years now and I have suffered that pain he talks about whenever I need to refactor my tests just because we perform a clean up in the backend.

TDD causes early pain. Technical debt causes late pain. So long as the early pain is cheaper than the late pain you got a bargain.

But that doesn’t mean there isn’t some learning to do. TDD can be done in very messed up ways. From locking down things to where change is actually harder with TDD (test against interfaces not implementation details to avoid most of this) to testing requirements that either don’t exist anymore or never should have existed in the first place (map tests to requirements if names don’t make it obvious).

However, just because you have to refactor tests doesn’t mean you did something wrong. Sometimes it means a requirement changed. Sometimes it means you’ve restructured the code. TDD never promised that tests never change.

In fact, there is a rigorous methodology for refactoring tests. Michael Feathers gave us refactoring against the red bar. Apparently he got it from Elizabeth Keogh. I’ve talked about it before. It walks you through steps to change a unit test so that the code under test can’t sneak away from you.

Which is nice, but again, it isn’t free. Change comes at a cost.

architecture – Change achitecture design to API to reduce coupling

I just don’t see an API reducing all those dependencies

Because it doesn’t. Uncle Bob is wrong there. Let me list some reasons for this:

The very first thing is, Uncle Bob doesn’t seem to think in terms of design trade-offs. It seems to be always in absolutes, like the UI must always be separated from the business, database and all other technologies must all be separated from each other, etc. Just take a look at Uncle Bob’s code to see how unmaintainable that is.

Architecture is heavily dependent on the requirements. That means you can’t make a trade-off “in general” without even looking at what that application is for.

Second, introducing an additional abstraction is not free. Putting a box in a diagram might be easy, but you’re bringing a lot of complexity on board. This is also not acknowledged.

Third, it doesn’t even decouple anything necessarily. Uncle Bob leaves out all the details about that one additional box. Like, is that API a data-oriented API, like Uncle Bob usually defines them? Because it is, both sides have to know what that data is and how it works. That would mean that if the “services” change, the “users” have to change anyway, because of the changed semantics and/or protocol. What if the workflow changes, the meaning of some data elements, etc.?

He might mean, that you don’t exactly need to know which service you need to call, because you’re calling a central thing. That might help you in some situations, but that’s a pretty small part of the whole thing.

In general though, I agree with the conclusions. TDD will not get you a good design automatically, it is not a design practice. It is still all up to us.

stochastic calculus – reflection coupling of Brownian and Levy’s characterization

My question comes from page 3 of this manuscript, in which the authors mentioned that if $$B_t$$ is a $$d$$-dimensional Brownian motion and we define $$dB’_t = (mathrm{Id} – 2e_te^intercal_t),dB_t,$$ where $$e_t$$ is defined to be $$X_t/|X_t|$$ for some $$X_t neq mathbf{0}$$ and $$mathbf{0}$$ otherwise. They mentioned that this is the reflection coupling of two Brownian motions and that Levy’s characterization ensures that $$B’_t$$ is also a Brownian motion. May I know is there any intuitive explanation for the name “reflection coupling”? Also, how does the Levy’s characterization theorem applies to $$B’_t$$? Thank you very much!

design – Achieving loose coupling

My scenario involves the following classes:

I have three types of Peer that creates a RegistryType instance because its methods are needed. Same for Identity class. Each PeerType need 0 or all the RegistryType methods in order to accomplish their work. Every Peer is then run by creating a Server instance that makes use of one of the methods defined by the Peer by passing a callback.

What I’m trying to achieve is to solve the tight coupling between the PeerType classes and the RegistryType class. My idea is to define a general Peer class from which every type inherits and define the dependency with the RegistryType there.

The target language is Python so either constructing the Peers with a RegistryType or a Registry is ok.

Even after defining a new Peer class to encapsulate the RegistryType I reckon the two classes are still tight coupled so (maybe) a solution would be to pass a RegistryType instance to the Peer class instead of constructing it inside its constructor.

I hope to receive some great suggestions. Thanks.